posts - 51, comments - 17, trackbacks - 0, articles - 9
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

java模式之工厂模式

Posted on 2007-03-29 13:18 chenweicai 阅读(285) 评论(0)  编辑  收藏

简单工厂模式(缺点:每增加一个具体产品时 ,就要修改工厂方法,工厂方法负责了所有具体产品的创建)

举个例子:
------------------
public interface Fruit {

 void grow();
 
 void harvest();
 
 void plant();
}
-------------------
public class Apple implements Fruit {

 private int treeAge;
 
 public void grow() {
  // TODO Auto-generated method stub
  System.out.println("Apple is growing...");
  
 }

 public void harvest() {
  // TODO Auto-generated method stub
  System.out.println("Apple has been harvested.");
 }

 public void plant() {
  // TODO Auto-generated method stub
  System.out.println("Apple has been planted.");
  
 }

 public int getTreeAge() {
  return treeAge;
 }

 public void setTreeAge(int treeAge) {
  this.treeAge = treeAge;
 }
 
}
-------------------
public class Grape implements Fruit {

 private boolean seedless;
 
 
 
 public boolean isSeedless() {
  return seedless;
 }

 public void setSeedless(boolean seedless) {
  this.seedless = seedless;
 }

 public void grow() {
  // TODO Auto-generated method stub
  System.out.println("Grape is growing...");
 }

 public void harvest() {
  // TODO Auto-generated method stub
  System.out.println("Grape has been harvested.");
 }

 public void plant() {
  // TODO Auto-generated method stub
  System.out.println("Grape has been planted.");
  
 }

 
}


---------------------------
public class Strawberry implements Fruit {

 public void grow() {
  // TODO Auto-generated method stub
  System.out.println("Strawberry is growing...");
 }

 public void harvest() {
  // TODO Auto-generated method stub
  System.out.println("Strawberry has been harvested.");
 }

 public void plant() {
  // TODO Auto-generated method stub
  System.out.println("Strawberry has been planted.");
 }
}

-------------------------
public class FruitGardener {

 //静态工厂方法
 public static Fruit factory(String which) throws BadFruitException{
  if(which.equalsIgnoreCase("apple")){
   return new Apple();
  }
  else if(which.equalsIgnoreCase("strawberry")){
   return new Strawberry();
  }
  else if(which.equalsIgnoreCase("grape")){
   return new Grape();
  }
  else{
   throw new BadFruitException("Bad fruit request");
  }
 }
}

---------------------------
public class BadFruitException extends Exception {

 public BadFruitException(String msg){
  super(msg);
 }
}


--------------------------
public class Client {

 public static void main(String[] args){
  
  try{
   Fruit apple = (Fruit)FruitGardener.factory("Apple");
   System.out.println("apple is class: " + apple.getClass().getName());
   apple.plant();
   apple.grow();
   apple.harvest();
   System.out.println();
   
   Fruit grape = (Fruit)FruitGardener.factory("grape");
   System.out.println("grape is class: " + grape.getClass().getName());
   grape.plant();
   grape.grow();
   grape.harvest();
   System.out.println();
   
   Fruit strawberry = (Fruit)FruitGardener.factory("strawberry");
   System.out.println("strawberry is class: " + strawberry.getClass().getName());
   strawberry.plant();
   strawberry.grow();
   strawberry.harvest();
   
  }catch(BadFruitException e){
   e.printStackTrace();
  }
 }
}


2 工厂方法模式:解决了简单工厂模式的缺点, 将每一个具体产品的创建工作交给对应的具体工厂角色去解决

举个例子:
---------------------
public interface FruitGardener {

 public Fruit factory();
}


---------------------

public class AppleGardener implements FruitGardener{

 public Fruit factory() {
  return new Apple();
 }
}

-----------------

public class GrapeGardener implements FruitGardener{

 public Fruit factory() {
  return new Grape();
 }
}


----------------------

public class StrawGardener implements FruitGardener{

 public Fruit factory() {
  return new Strawberry();
 }
}

-----------------
public class Client {

 private static FruitGardener applegardener, grapegardener, strawgardener;
 private static Fruit apple, grape, strawberry;
 
 public static void main(String[] args){
  applegardener = new AppleGardener();
  apple = applegardener.factory();
  System.out.println("apple is class: " + apple.getClass().getName());
  apple.plant();
  apple.grow();
  apple.harvest();
  System.out.println();
  
  grapegardener = new GrapeGardener();
  grape = grapegardener.factory();
  System.out.println("grape is class: " + grape.getClass().getName());
  grape.plant();
  grape.grow();
  grape.harvest();
  System.out.println();

  strawgardener = new StrawGardener();
  strawberry = strawgardener.factory();
  System.out.println("strawberry is class: " + strawberry.getClass().getName());
  strawberry.plant();
  strawberry.grow();
  strawberry.harvest();
 }
}


3 抽象工厂模式:解决多产品簇问题

举个例子:

-------------------
public interface Fruit {

// public String getName();
}

public class NorthernFruit implements Fruit{

 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public NorthernFruit(String name) {
  super();
  // TODO Auto-generated constructor stub
  this.name = name;
 }
 
 
}



public class TropicalFruit implements Fruit{

 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public TropicalFruit(String name) {
  super();
  // TODO Auto-generated constructor stub
  this.name = name;
 }
 
 
}



public interface Veggie {

}


public class TropicalVeggie implements Veggie{
 
 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public TropicalVeggie(String name) {
  super();
  // TODO Auto-generated constructor stub
  this.name = name;
 }
 
 

}


public class NorthernVeggie implements Veggie{

 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public NorthernVeggie(String name) {
  super();
  // TODO Auto-generated constructor stub
  this.name = name;
 }
 
 
}

-------------------------

public interface Gardener {

 public Fruit createFruit(String name);
 
 public Veggie createVeggie(String name);
}

public class NorthernGardener implements Gardener {

 public Fruit createFruit(String name) {
  return new NorthernFruit(name);
 }

 public Veggie createVeggie(String name) {
  return new NorthernVeggie(name);
 }

 
}



public class TropicalGardener implements Gardener {

 public Fruit createFruit(String name) {
  return new TropicalFruit(name);
 }

 public Veggie createVeggie(String name) {
  return new TropicalVeggie(name);
 }

 
}


public class Client {

 private static Gardener tropicalgardener ,northerngardener;
 private static Fruit northernfruit, tropicalfruit;
 private static Veggie northernveggie, tropicalveggie;
 
 public static void main(String[] args){
  tropicalgardener = new TropicalGardener();
  tropicalfruit = tropicalgardener.createFruit("tropicalfruit");
  //System.out.println(tropicalfruit.getName());
  tropicalveggie = tropicalgardener.createVeggie("tropicalveggie");
  
  northerngardener = new NorthernGardener();
  northernfruit = northerngardener.createFruit("northernfruit");
  northernveggie = northerngardener.createVeggie("northernveggie");
 }
 
}


只有注册用户登录后才能发表评论。


网站导航: