创建型模式--工厂方法模式(Factory Method)
2007-06-17 19:08
375 查看
Factory Method:Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
工厂方法模式又叫虚拟构造子(Virtual Constructor)模式。
一:引入
Simple Factory在一定程度上支持OCP,但并没有完全支持OCP,其一缺点为当有新的产品加入到系统中时还必须修改工厂类。
public interface FruitFactory ...{
public Fruit createFruit();
}
public class AppleFactory implements FruitFactory ...{
public Fruit createFruit()
...{
return new Apple();
}
}
public class Client...{
public static Fruit getEatFruit()
...{
Fruit fruit=null;
FruitFactory fruitFactory=new AppleFactory();
fruit=fruitFactory.createFruit();
fruit.pick();
fruit.peel();
return fruit;
}
public static void main(String args[])
...{
getEatFruit();
}
}
//这样有新的产品加入时,只要加入对应的Factory,不用修改Simple Factory 中创建部分的代码
二:结构
三:实际应用
EJB技术构架中
Context ctx=new InitContext();
EmployeeHome home=(EmployeeHome )ctx.lookup("Employee");//得到Concrete Factory (EmployeeHome)
Employee emp=home.create(1001,"andy","Smith");//创建Concrete Product(Employee)
emp.setTel("010-1234343");
四:适用情形
Use the Factory Method pattern when
a class can't anticipate the class of objects it must create.
a class wants its subclasses to specify the objects it creates.
classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
接口稳定,创建的具体类不可预料,经常变;
当然如果这个类十分稳定,就没有必要用Factory模式,如String类,十分稳定,没有其它的子类。
参考文献:
1:阎宏,《Java与模式》,电子工业出版社
2:Eric Freeman & Elisabeth Freeman,《Head First Design Pattern》,O'REILLY
3:GOF ,《designpatterns-elements.of.reuseable.object-oriented.software》
工厂方法模式又叫虚拟构造子(Virtual Constructor)模式。
一:引入
Simple Factory在一定程度上支持OCP,但并没有完全支持OCP,其一缺点为当有新的产品加入到系统中时还必须修改工厂类。
public interface FruitFactory ...{
public Fruit createFruit();
}
public class AppleFactory implements FruitFactory ...{
public Fruit createFruit()
...{
return new Apple();
}
}
public class Client...{
public static Fruit getEatFruit()
...{
Fruit fruit=null;
FruitFactory fruitFactory=new AppleFactory();
fruit=fruitFactory.createFruit();
fruit.pick();
fruit.peel();
return fruit;
}
public static void main(String args[])
...{
getEatFruit();
}
}
//这样有新的产品加入时,只要加入对应的Factory,不用修改Simple Factory 中创建部分的代码
二:结构
三:实际应用
EJB技术构架中
Context ctx=new InitContext();
EmployeeHome home=(EmployeeHome )ctx.lookup("Employee");//得到Concrete Factory (EmployeeHome)
Employee emp=home.create(1001,"andy","Smith");//创建Concrete Product(Employee)
emp.setTel("010-1234343");
四:适用情形
Use the Factory Method pattern when
a class can't anticipate the class of objects it must create.
a class wants its subclasses to specify the objects it creates.
classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
接口稳定,创建的具体类不可预料,经常变;
当然如果这个类十分稳定,就没有必要用Factory模式,如String类,十分稳定,没有其它的子类。
参考文献:
1:阎宏,《Java与模式》,电子工业出版社
2:Eric Freeman & Elisabeth Freeman,《Head First Design Pattern》,O'REILLY
3:GOF ,《designpatterns-elements.of.reuseable.object-oriented.software》
相关文章推荐
- 设计模型之创建型模式之工厂方法模式(Factory Method)
- 创建型模式之工厂方法模式(FactoryMethod)
- Factory Method 工厂方法模式(创建型模式)
- JAVA设计模式(01_1):创建型-工厂模式【工厂方法模式】(Factory Method)
- [导入]C#面向对象设计模式纵横谈(5):Factory Method 工厂方法模式(创建型模式).zip(10.09 MB)
- 创建型模式:工厂方法模式(Factory Method)--分析优缺点
- 四.创建型设计模式——Factory Method Pattern(工厂方法模式)
- 设计模式(2)の创建型の工厂方法模式(Factory Method)の多个工厂方法模式
- 创建型模式5:工厂方法模式(Factory Method)
- 设计模式 (5)——工厂方法模式(Factory Method,创建型)
- 创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。 工厂方法模式(Factory Method) 工厂方法模式分为三种:普通工厂模式 多个工厂方法模式 静态工
- 工厂方法模式(Factory Method)(对象创建型模式)
- 设计模式(1)の创建型の工厂方法模式(Factory Method)の(普通|简单)工厂模式
- Java设计模式(2)——创建型模式之工厂方法模式(Factory Method)
- JAVA设计模式(01):创建型-工厂模式【工厂方法模式】(Factory Method)
- JAVA设计模式(1) —<创建型>工厂方法模式(Factory Method)
- 设计模式学习(创建型模式)—工厂方法模式(Factory Method)
- JAVA设计模式(01):创建型-工厂模式【工厂方法模式】(Factory Method)
- 笔记5:Factory Method 工厂方法模式(创建型模式)
- JAVA设计模式(24):创建型-工厂模式【工厂方法模式】(Factory Method)