疯狂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 @Override

  10 public void run() {

  11 // TODO Auto-generated method stub

  12 System.out.println("Car----------------");

  13

  14 }

  15

  16 }

  17

  18 //交通工具2.plane

  19 public class Plane implements Moveable{

  20

  21 @Override

  22 public void run() {

  23 // TODO Auto-generated method stub

  24 System.out.println("Plane----------------");

  25 }

  26

  27 }

  28

  29 //作为工厂必须实现的接口

  30 public interface Factory {

  31 public Moveable create();

  32 }

  33

  34 //具体的交通工具工厂CarFactory

  35 public class CarFactory implements Factory{

  36

  37 @Override

  38 public Moveable create() {

  39 // TODO Auto-generated method stub

  40

  41 return new Car();

  42 }

  43

  44 }

  45

  46 //具体的交通工具工厂PlaneFactory

  47 public class PlaneFactory implements Factory{

  48

  49 @Override

  50 public Moveable create() {

  51 // TODO Auto-generated method stub

  52 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.vihicle

  public 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 @Override

  4 public Moveable create() {

  5 // TODO Auto-generated method stub

  6 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 stub

  100 return new AK47();

  101 }

  102

  103 @Override

  104 public Food createFood() {

  105 // TODO Auto-generated method stub

  106 return new Apple();

  107 }

  108

  109 }

  110

  111 //工厂2:Benz、M4、Banana

  112 public class Factory2 implements AbstractFactory{

  113

  114 @Override

  115 public Vehicle createVehicle() {

  116 // TODO Auto-generated method stub

  117 return new Benz();

  118 }

  119

  120 @Override

  121 public Weapon createWeapon() {

  122 // TODO Auto-generated method stub

  123 return new M4();

  124 }

  125

  126 @Override

  127 public Food createFood() {

  128 // TODO Auto-generated method stub

  129 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 apple

  BWM--------------

  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 banana

  2 Benz--------------

  3 M4--------------

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