设计模式回顾系列文章:主要针对工作中常用常见的设计模式进行整理、总结,同时分享以供大家拍砖。
------------------------------------------------
工厂方法模式(FactoryMethod)
定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。
适用于:
当一个类不知道它所必须创建的对象的类的时候,当一个类希望由它的子类来指定它所创建的对象的时候。
程序实现:
接口:
public interface Fruit{
public void grow();
public void harvest();
public void plant();
}
实现:
public class BigApple implements Fruit{
private int treeAge;
public void grow(){
System.out.println("BigApple is growing!");
}
public void harvest(){
System.out.println("BigApple has been harvested!");
}
public void plant(){
System.out.println("BigApple has been planted!");
}
public int getTreeAge() {
return treeAge;
}
public void setTreeAge(int treeAge) {
this.treeAge = treeAge;
}
}
public class BigOrange implements Fruit {
private int number;
public void grow() {
System.out.println("BigOrange is growing!");
}
public void harvest() {
System.out.println("BigOrange has been harvest!");
}
public void plant() {
System.out.println("BigOrange has been harvest!");
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
public class SmallApple implements Fruit{
private int treeAge;
public void grow(){
System.out.println("SmallApple is growing!");
}
public void harvest(){
System.out.println("SmallApple has been harvested!");
}
public void plant(){
System.out.println("SmallApple has been planted!");
}
public int getTreeAge() {
return treeAge;
}
public void setTreeAge(int treeAge) {
this.treeAge = treeAge;
}
}
public class SmallOrange implements Fruit {
private int number;
public void grow() {
System.out.println("SmallOrange is growing!");
}
public void harvest() {
System.out.println("SmallOrange has been harvest!");
}
public void plant() {
System.out.println("SmallOrange has been harvest!");
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
一共四种水果:大苹果、大橙子、小苹果、小橙子,下面是抽象水果工厂:
public abstract class FruitFactory {
public abstract Fruit factory(String factory);
public Fruit order(String type){
Fruit fruit=factory(type);
fruit.plant();
fruit.grow();
fruit.harvest();
return fruit;
}
}
具体水果工厂:
public class AppleFactory extends FruitFactory{
public Fruit factory(String color){
if("big".equals(color)){
return new BigApple();
}else if("small".equals(color)){
return new SmallApple();
}else{
return null;
}
}
}
public class OrangeFactory extends FruitFactory{
public Fruit factory(String type){
if("big".equals(type)){
return new BigOrange();
}else if("small".equals(type)){
return new SmallOrange();
}else{
return null;
}
}
}
最后是客户端调用代码:
public static void main(String[] args){
FruitFactory appleFactory=new AppleFactory();
FruitFactory orangeFactory=new OrangeFactory();
Fruit fruit=appleFactory.order("big");
System.out.println(fruit.getClass().getName()+"\n");
fruit=appleFactory.order("small");
System.out.println(fruit.getClass().getName()+"\n");
fruit=orangeFactory.order("big");
System.out.println(fruit.getClass().getName()+"\n");
fruit=orangeFactory.order("small");
System.out.println(fruit.getClass().getName()+"\n");
}
其实像IoC容器里面就大量使用了工厂模式。另外,由于现在依赖注入概念和容器的普及,需要工厂模式实现的工作其实都可以交给容器去处理了。
分享到:
相关推荐
.wkpf,.wkpf1{ font-family:"微软雅黑"; font-size:13px; line-height:30px; text-decoration:none; color:#818183;}1599次浏览 评价:好中差 概述...这就是要说的FactoryMethod模式了。意图定义一个用户创建
C++的设计模式之工厂方法模式(Factory Method) C++的设计模式之工厂方法模式(Factory Method) C++的设计模式之工厂方法模式(Factory Method) C++的设计模式之工厂方法模式(Factory Method)
设计模式C++学习之工厂方法模式(Factory Method)
C++设计模式课件8_Factory Method_工厂方法.pdf
C#面向对象设计模式纵横谈(5):Factory Method 工厂方法模式(创建型模式) (Level 300)
文章地址:https://blog.csdn.net/mazhiyuan1981/article/details/110196482
2、工厂方法模式(Factory Method Pattern) 用意:让每个具体工厂生产具体产品
C#面向对象设计模式纵横谈(5):Factory Method 工厂方法模式(创建型模式) 视频教程,主讲人李建忠
很简单的一个例子 博文链接:https://schnauzer.iteye.com/blog/111889
JAVA设计模式(01):创建型-工厂模式【简单工厂模式】(Simple Factory)
C#面向对象设计模式纵横谈:Factory Method 工厂方法模式(创建型模式)
FACTORY METHOD(工厂方法)
NULL 博文链接:https://lym6520.iteye.com/blog/698013
NULL 博文链接:https://zhouhaitao.iteye.com/blog/1003702
Java设计模式 - 简单工厂 & 工厂方法(构建工厂)源码:FactoryModel - 详情:https://blog.csdn.net/Dream_Weave/article/details/83989879
2. 工厂方法模式(Factory Method) 3. 抽象工厂模式(Abstract Factory) 这三种模式从上到下逐步抽象,并且更具一般性。还有一种分类法,就是将简单工厂模式看为工厂方法模式的一种特例,两个归为一类。下面是使用...
GOF在《设计模式》一书中将工厂模式分为两类:工厂方法模式(Factory Method)与抽象工厂模式(Abstract Factory)。将简单工厂模式(Simple Factory)看为工厂方法模式的一种特例,两者归为一类。 两者皆可,在...
Java常用设计模式(SingleTon、FactoryMethod、AbstractFactory)