你的位置:首页 > Java教程

[Java教程]Java设计模式——工厂方法和抽象工厂方法


工厂方法(Factory Method)模式的意义是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中。核心工厂类不再负责产品的创建,这样核心类成为一个抽象工厂 角色,仅负责具体工厂子类必须实现的接口,这样进一步抽象化的好处是使得工厂方法模式可以使系统在不修改具体工厂角色的情况下引进新的产品。

例如:

 1 //交通工具必须实现的接口 2 public interface Moveable { 3   public void run(); 4 } 5  6 //交通工具1.car 7 public class Car implements Moveable{ 8  9   @Override10   public void run() {11     // TODO Auto-generated method stub12     System.out.println("Car----------------");13     14   }15 16 }17 18 //交通工具2.plane19 public class Plane implements Moveable{20 21   @Override22   public void run() {23     // TODO Auto-generated method stub24     System.out.println("Plane----------------");25   }26 27 }28 29 //作为工厂必须实现的接口30 public interface Factory {31   public Moveable create();32 }33 34 //具体的交通工具工厂CarFactory35 public class CarFactory implements Factory{36 37   @Override38   public Moveable create() {39     // TODO Auto-generated method stub40     41     return new Car();42   }43 44 }45 46 //具体的交通工具工厂PlaneFactory47 public class PlaneFactory implements Factory{48 49   @Override50   public Moveable create() {51     // TODO Auto-generated method stub52     return new Plane();53   }54 55 }

测试1:

1 public class Test {2   public static void main(String args[]){3     Factory fac = new PlaneFactory();4     Moveable mov = fac.create();5     mov.run();6   }7 }

 

输出1:

Plane----------------

测试2:

1 public class Test {2   public static void main(String args[]){3     Factory fac = new CarFactory();4     Moveable mov = fac.create();5     mov.run();6   }7 }

输出2:

Car----------------

 

 

在上述例子中,工厂方法使我们能够定制任意交通工具的类型和生产过程,假设我们有了新的交通工具vihicle

//交通工具3.vihiclepublic class Vihicle implements Moveable{  @Override  public void run() {    // TODO Auto-generated method stub    System.out.println("Vihicle----------------");   }}

实现VihicleFactory类:

1 public class VihicleFactory implements Factory{2 3   @Override4   public Moveable create() {5     // TODO Auto-generated method stub6     return new Vihicle();7   }8 9 }

在生产过程中,我们只需new一个VihicleFactory并调用create()方法即可:

1 public class Test {2   public static void main(String args[]){3     Factory fac = new VihicleFactory();4     Moveable mov = fac.create();5     mov.run();6   }7 }

输出:

Vihicle----------------

在工厂方法中,每当我们有了新的Product需要生产时,我们就必须实现对应的ProductFactory,这样一来,随着Product数量的增多,往往容易造成ProductFactory泛滥,若我们想要生产十几种产品就要相应的实现十几个具体的工厂,这显然是不科学的,而抽象工厂方法正好解决了这个问题

例:

 1 //食物接口 2 public interface Food { 3   public void printName(); 4 } 5  6 //食物产品1.apple 7 public class Apple implements Food{ 8  9   @Override 10   public void printName() { 11     // TODO Auto-generated method stub 12     System.out.println("this is apple"); 13   } 14  15 } 16  17 //食物产品2.banana 18 public class Banana implements Food{ 19  20   @Override 21   public void printName() { 22     // TODO Auto-generated method stub 23     System.out.println("this is banana"); 24   } 25  26 } 27  28 //汽车接口 29 public interface Vehicle { 30   public void run(); 31 } 32  33 //汽车产品1.benz 34 public class Benz implements Vehicle{ 35  36   @Override 37   public void run() { 38     // TODO Auto-generated method stub 39     System.out.println("Benz--------------"); 40   } 41  42 } 43  44  45 //汽车产品2.bwm 46 public class BWM implements Vehicle{ 47  48   @Override 49   public void run() { 50     // TODO Auto-generated method stub 51     System.out.println("BWM--------------"); 52   } 53  54 } 55  56 //武器接口 57 public interface Weapon { 58   public void shot(); 59 } 60  61  62 //武器产品1.AK47 63 public class AK47 implements Weapon{ 64   @Override 65   public void shot(){ 66     System.out.println("AK47-------------"); 67   } 68 } 69  70 //武器产品2.M4 71 public class M4 implements Weapon{ 72  73   @Override 74   public void shot() { 75     // TODO Auto-generated method stub 76     System.out.println("M4--------------"); 77   } 78  79 } 80  81 //工厂接口 82 public interface AbstractFactory { 83   public Vehicle createVehicle(); 84   public Weapon createWeapon(); 85   public Food   createFood(); 86 } 87  88 //工厂1:能够生产BWM、AK47、Apple 89 public class Factory1 implements AbstractFactory{ 90  91   @Override 92   public Vehicle createVehicle() { 93     // TODO Auto-generated method stub 94     return new BWM(); 95   } 96  97   @Override 98   public Weapon createWeapon() { 99     // TODO Auto-generated method stub100     return new AK47();101   }102 103   @Override104   public Food createFood() {105     // TODO Auto-generated method stub106     return new Apple();107   }108 109 }110 111 //工厂2:Benz、M4、Banana112 public class Factory2 implements AbstractFactory{113 114   @Override115   public Vehicle createVehicle() {116     // TODO Auto-generated method stub117     return new Benz();118   }119 120   @Override121   public Weapon createWeapon() {122     // TODO Auto-generated method stub123     return new M4();124   }125 126   @Override127   public Food createFood() {128     // TODO Auto-generated method stub129     return new Banana();130   }131 132 }

测试1:

public class Test {  public static void main(String args[]){    AbstractFactory abs=new Factory1();    Food fo = abs.createFood();    Vehicle ve = abs.createVehicle();    Weapon we = abs.createWeapon();    fo.printName();    ve.run();    we.shot();  }}

输出1:

this is appleBWM--------------AK47-------------

测试2:

public class Test {  public static void main(String args[]){    AbstractFactory abs=new Factory2();    Food fo = abs.createFood();    Vehicle ve = abs.createVehicle();    Weapon we = abs.createWeapon();    fo.printName();    ve.run();    we.shot();  }}

输出2:

1 this is banana2 Benz--------------3 M4--------------

抽象工厂的优点在于能够生产一系列可能存在关联的产品,但是,当我们需要在抽象工厂中添加新的产品时,几乎所有的工厂类都要修改

图片来自http://www.cnblogs.com/devinzhang/archive/2011/12/19/2293160.html

图片来自http://www.cnblogs.com/devinzhang/archive/2011/12/19/2293160.html

 

这样看来:

工厂模式生产一类产品(一个产品等级结构),来自同一个接口或抽象类

抽象工厂生产多类可能存在关联的产品(多个产品等级结构),来自多个接口或抽象类