Design Patterns Abstract Factory pattern

Abstract factory pattern
  Objects created by the factory, there is a link between each other, is the need to match each other, 
  and a simple factory does not solve the problem of matching the interdependence between objects

  A reasonable solution is to use the abstract factory design pattern: create a series of related or interdependent interfaces , 
  
internal interfaces in strict accordance with related or dependent objects to create a relationship,
  do not accumulate other methods do not use or non-affiliated method

composition

AbstractFactory: abstract factory, the factory provides a unified interface.

ConcreteFactory: Concrete Factory, to create specific examples of products affiliated with

AbstractProduct: a unified product interfaces

ConcreteProduct: Specific products

Advantages: Decoupling

Disadvantages: not easy to expand

 

  1 public class Client {
  2     public static void main(String[] args) {
  3         ConcreteFactory1 a = new ConcreteFactory1();
  4         CPUApi cpu = a.createCpu();
  5         MainBoardApi mainBoard = a.createMainBoard();
  6         cpu.calculate();
  7         mainBoard.installCpu();
  8 
  9     }
 10 }
 11 
 12 
 13 //CPU的模拟方法
 14 public interface CPUApi {
 15     public void calculate();
 16 }
 17 
 18 //主板模拟方法
 19 public interface MainBoardApi {
 20     public void installCpu();
 21 }
 22 
 23 //cpu实现
 24 public class IntelCpu implements CPUApi {
 25     private int pins = 0;
 26 
 27     public IntelCpu(int pins) {
 28         // TODO Auto-generated constructor stub
 29         this.pins = pins;
 30     }
 31 
 32     @Override
 33     public void calculate() {
 34         // TODO Auto-generated method stub
 35         System.out.println("INTEL CPU 针脚数:" + pins);
 36     }
 37 }
 38 
 39 public class AMDCpu implements CPUApi {
 40     private int pins = 0;
 41 
 42     public AMDCpu(int pins) {
 43         // TODO Auto-generated constructor stub
 44         this.pins = pins;
 45     }
 46 
 47     @Override
 48     public void calculate() {
 49         // TODO Auto-generated method stub
 50         System.out.println("AMD CPU针脚数:" + pins);
 51     }
 52 }
 53 
 54 //主板实现
 55 public class GAMainBoard implements MainBoardApi {
 56     private int cpuHoles = 0;
 57 
 58     public GAMainBoard(int cpuHoles) {
 59         // TODO Auto-generated constructor stub
 60         this.cpuHoles = cpuHoles;
 61     }
 62 
 63     @Override
 64     public void installCpu() {
 65         // TODO Auto-generated method stub
 66         System.out.println("GA MainBoard 插孔数:" + cpuHoles);
 67     }
 68 
 69 }
 70 
 71 public class MSIMainBoard implements MainBoardApi {
 72     private int cpuHoles = 0;
 73 
 74     public MSIMainBoard(int cpuHoles) {
 75         // TODO Auto-generated constructor stub
 76         this.cpuHoles = cpuHoles;
 77     }
 78 
 79     @Override
 80     public void installCpu() {
 81         // TODO Auto-generated method stub
 82         System.out.println("MSI MainBord 插孔数:" + cpuHoles);
 83     }
 84 
 85 }
 86 
 87 //抽象工厂
88  public  interface AbstractFactory {
 89      public CPUApi createCpu ();
 90  
91 is      public MainBoardApi createMainBoard ();
 92  }
 93  
94  // abstract factory embodied 
95  public  class ConcreteFactory1 the implements AbstractFactory {
 96  
97      // linked between 
98      @Override
 99      public CPUApi createCpu () {
 100          // the TODO Auto-Generated Method Stub 
101          return  new new amdcpu (1156 );
 102     }
103 
104     @Override
105     public MainBoardApi createMainBoard() {
106         // TODO Auto-generated method stub
107         return new MSIMainBoard(1156);
108     }
109 }
110 
111 public class ConcreteFactory2 implements AbstractFactory {
112 
113     //两者之间有联系
114     @Override
115     public CPUApi createCpu() {
116         // TODO Auto-generated method stub
117         return new AMDCpu(939);
118     }
119 
120     @Override
121     public MainBoardApi createMainBoard() {
122         // TODO Auto-generated method stub
123         return new MSIMainBoard(939);
124     }
125 
126 }

 

Guess you like

Origin www.cnblogs.com/loveer/p/11273161.html