`
Ivan_Pig
  • 浏览: 382111 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

DesignPattern学习-----对Factory的迷惑

阅读更多
    我对工厂不明白的地方就是工厂方法和抽象工厂之间到底有什么区别。

    对于简单工厂深入浅出DesignPattern说,简单工厂不算是个设计模式,只算个写法。不过照这样推论,其实所有的设计模式都是一种写法罢了。

    工厂方法模式定义了一个创建对象的接口,但由子类决定实例化的类是哪个。工厂方法让类把实例化推迟到了子类。(这句话我看了n遍,就是没看明白!说得莫名其妙的)

    再看抽象工厂,提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。 (更是莫名其妙!)

    看下代码。工厂方法。
   
//产品 Plant接口 
public interface Plant { } 
//具体产品PlantA,PlantB 
public class PlantA implements Plant {  
  public PlantA () {   
    System.out.println("create PlantA !");  
  }  
  public void doSomething() {   
    System.out.println(" PlantA do something ...");  
  } 
} 
public class PlantB implements Plant {  
  public PlantB () {   
    System.out.println("create PlantB !");  
  }  
  public void doSomething() {   
    System.out.println(" PlantB do something ...");  
  } 
} 
// 抽象工厂方法 
public interface AbstractFactory {  
  public Plant createPlant(); 
} 
//具体工厂方法 
public class FactoryA implements AbstractFactory {  
  public Plant createPlant() {   
    return new PlantA();  
  } 
} 
public class FactoryB implements AbstractFactory {  
  public Plant createPlant() {   
    return new PlantB();  
  } 
} 
//调用工厂方法 
public Client { 
  public method1() {
    AbstractFactory instanceA = new FactoryA(); 
    instanceA.createPlant();
    AbstractFactory instanceB = new FactoryB(); 
    instanceB.createPlant();
  } 
}


    再看抽象工厂。
// 产品 Plant接口    
public interface Plant { }//标志接口  
//具体产品PlantA,PlantB    
public class PlantA implements Plant {    
  
 public PlantA () {    
  System.out.println("create PlantA !");    
 }    
  
 public void doSomething() {    
  System.out.println(" PlantA do something ...");    
 }    
}    
public class PlantB implements Plant {    
 public PlantB () {    
  System.out.println("create PlantB !");    
 }    
  
 public void doSomething() {    
  System.out.println(" PlantB do something ...");    
 }    
}    
// 产品 Fruit接口    
public interface Fruit { }    
//具体产品FruitA,FruitB    
public class FruitA implements Fruit {    
 public FruitA() {    
  System.out.println("create FruitA !");    
 }    
 public void doSomething() {    
  System.out.println(" FruitA do something ...");    
 }    
}    
public class FruitB implements Fruit {    
 public FruitB() {    
  System.out.println("create FruitB !");    
 }    
 public void doSomething() {    
  System.out.println(" FruitB do something ...");    
 }    
}    
// 抽象工厂方法    
public interface AbstractFactory {    
 public Plant createPlant();    
 public Fruit createFruit();    
}    
//具体工厂方法    
public class FactoryA implements AbstractFactory {    
 public Plant createPlant() {    
  return new PlantA();    
 }    
 public Fruit createFruit() {    
  return new FruitA();    
 }    
}    
public class FactoryB implements AbstractFactory {    
 public Plant createPlant() {    
  return new PlantB();    
 }    
 public Fruit createFruit() {    
  return new FruitB();    
 }    
}   


    区别在哪里?和明显,
(1)第二段代码比第一段代码多了一个接口及相应的实现类。
(2)且这个方法能同时创建多个对象。除了工厂方法里面的createPlant(),还有ceateFruit()。
其他,好像没区别了吧?

     这么相近的模式,干嘛非要解释成这样?!
     我对抽象工厂这么描述行不行?
     抽象工厂模式定义了一个创建多个相关联或相互依赖对象的接口,但由子类决定实例化的类是哪个。
     或者我对工厂方法这么描述行不行?
     工厂方法,提供一个创建一个对象的接口,而无需指定它们具体的类。
     这么描述,看了以后应该还是很迷惑,但是你了解了其中一个模式后,就能知道另一个模式的概样了,而不会像接触一个完全陌生的模式那样迷茫了。

     从简单工厂,到工厂方法,再到抽象工厂,应该是一个逐步添加功能的过程。
     简单工厂:只有一个工厂,只能实例化一种对象。
     工厂方法:一个抽象工厂,派生多个具体工厂,而每个具体工厂只能实例化一种对象。
     抽象工厂:一个抽象工厂,派生多个具体工厂,每个具体工厂还能实例化多种对象。

     现在,个人认为自己终于对工厂有了一个认识,至少不迷茫了。很郁闷,干嘛喜欢用这么让人郁闷的语句,解释原来挺简单的东西。


参考文章及代码来源:http://ldjsyl.iteye.com/blog/178177
http://ldjsyl.iteye.com/blog/184640
2
0
分享到:
评论
1 楼 airu 2009-02-12  
抽象是关键。
抽象工厂还可以继续抽象。
多思考很有意思。

相关推荐

Global site tag (gtag.js) - Google Analytics