鍍金池/ 教程/ Java/ Java抽象工廠模式
Java前端控制器模式
Java工廠設(shè)計(jì)模式
Java抽象工廠模式
Java觀察者模式
Java門面模式(或外觀模式)
Java備忘錄模式
Java MVC模式
Java單例模式
Java傳輸對(duì)象模式
Java迭代器模式
Java責(zé)任鏈模式
Java命令模式
Java原型模式
Java解釋器模式
Java適配器模式
Java狀態(tài)模式
Java中介者模式(Mediator Pattern)
Java攔截過濾器模式
Java策略模式
Java組合模式
Java業(yè)務(wù)代理模式
Java裝飾模式
Java模板模式
Java橋接模式
Java過濾器模式(條件模式)
Java享元模式(Flyweight Pattern)
Java建造者(Builder)模式
Java設(shè)計(jì)模式
Java空對(duì)象模式
Java數(shù)據(jù)訪問對(duì)象模式
Java訪問者模式
Java組合實(shí)體模式
Java服務(wù)定位器模式

Java抽象工廠模式

抽象工廠模式是一個(gè)超級(jí)工廠,用來創(chuàng)建其他工廠。 這個(gè)工廠也被稱為工廠的工廠。 這種類型的設(shè)計(jì)模式屬于創(chuàng)建模式,因?yàn)榇四J教峁┝藙?chuàng)建對(duì)象的最佳方法之一。

在抽象工廠模式中,接口負(fù)責(zé)創(chuàng)建相關(guān)對(duì)象的工廠,而不明確指定它們的類。 每個(gè)生成的工廠可以按照工廠模式提供對(duì)象。

實(shí)現(xiàn)實(shí)例

我們將創(chuàng)建一個(gè)ShapeColor接口并實(shí)現(xiàn)這些接口的具體類。在下一步中,將創(chuàng)建一個(gè)抽象工廠類AbstractFactory。在每個(gè)工廠類ShapeFactoryColorFactory定義都是擴(kuò)展自AbstractFactory。創(chuàng)建工廠創(chuàng)建者/生成器類 - FactoryProducer。

AbstractFactoryPatternDemo這是一個(gè)演示類,使用FactoryProducer來獲取AbstractFactory對(duì)象。 它會(huì)將信息(CIRCLE / RECTANGLE / SQUARE)傳遞給AbstractFactory以獲取所需的對(duì)象類型。 它還將信息(用于ColorRED / GREEN / BLUE)傳遞給AbstractFactory以獲取所需的對(duì)象類型。

第1步

創(chuàng)建Shape的接口。

Shape.java

public interface Shape {
   void draw();
}

第2步

創(chuàng)建實(shí)現(xiàn)相同接口的具體類。

Rectangle.java

public class Rectangle implements Shape {

   @Override
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}

Square.java

public class Square implements Shape {

   @Override
   public void draw() {
      System.out.println("Inside Square::draw() method.");
   }
}

Circle.java

public class Circle implements Shape {

   @Override
   public void draw() {
      System.out.println("Inside Circle::draw() method.");
   }
}

第3步

創(chuàng)建一個(gè)Colors接口,如下所示

Color.java

public interface Color {
   void fill();
}

第4步

創(chuàng)建實(shí)現(xiàn)相同接口的具體類。

public class Red implements Color {

   @Override
   public void fill() {
      System.out.println("Inside Red::fill() method.");
   }
}

Green.java

public class Green implements Color {

   @Override
   public void fill() {
      System.out.println("Inside Green::fill() method.");
   }
}

Blue.java

public class Blue implements Color {

   @Override
   public void fill() {
      System.out.println("Inside Blue::fill() method.");
   }
}

第5步

創(chuàng)建實(shí)現(xiàn)相同接口的具體類。

AbstractFactory.java

public abstract class AbstractFactory {
   abstract Color getColor(String color);
   abstract Shape getShape(String shape) ;
}

第6步

創(chuàng)建實(shí)現(xiàn)相同接口的具體類。

創(chuàng)建工廠類,根據(jù)給定信息擴(kuò)展AbstractFactory以生成具體類的對(duì)象。

ShapeFactory.java

public class ShapeFactory extends AbstractFactory {

   @Override
   public Shape getShape(String shapeType){

      if(shapeType == null){
         return null;
      }

      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();

      }else if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();

      }else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }

      return null;
   }

   @Override
   Color getColor(String color) {
      return null;
   }
}

ColorFactory.java

public class ColorFactory extends AbstractFactory {

   @Override
   public Shape getShape(String shapeType){
      return null;
   }

   @Override
   Color getColor(String color) {

      if(color == null){
         return null;
      }        

      if(color.equalsIgnoreCase("RED")){
         return new Red();

      }else if(color.equalsIgnoreCase("GREEN")){
         return new Green();

      }else if(color.equalsIgnoreCase("BLUE")){
         return new Blue();
      }

      return null;
   }
}

第7步

創(chuàng)建工廠生成器/生產(chǎn)器類,通過傳遞如ShapeColor等信息來獲取工廠

FactoryProducer.java

public class FactoryProducer {
   public static AbstractFactory getFactory(String choice){

      if(choice.equalsIgnoreCase("SHAPE")){
         return new ShapeFactory();

      }else if(choice.equalsIgnoreCase("COLOR")){
         return new ColorFactory();
      }

      return null;
   }
}

第8步

使用FactoryProducer來獲取AbstractFactory,以便通過傳遞類型等信息來獲取具體類的工廠。

AbstractFactoryPatternDemo.java

public class AbstractFactoryPatternDemo {
   public static void main(String[] args) {

      //get shape factory
      AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

      //get an object of Shape Circle
      Shape shape1 = shapeFactory.getShape("CIRCLE");

      //call draw method of Shape Circle
      shape1.draw();

      //get an object of Shape Rectangle
      Shape shape2 = shapeFactory.getShape("RECTANGLE");

      //call draw method of Shape Rectangle
      shape2.draw();

      //get an object of Shape Square 
      Shape shape3 = shapeFactory.getShape("SQUARE");

      //call draw method of Shape Square
      shape3.draw();

      //get color factory
      AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

      //get an object of Color Red
      Color color1 = colorFactory.getColor("RED");

      //call fill method of Red
      color1.fill();

      //get an object of Color Green
      Color color2 = colorFactory.getColor("Green");

      //call fill method of Green
      color2.fill();

      //get an object of Color Blue
      Color color3 = colorFactory.getColor("BLUE");

      //call fill method of Color Blue
      color3.fill();
   }
}

第9步

驗(yàn)證輸出,結(jié)果如下 -

Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside Red::fill() method.
Inside Green::fill() method.
Inside Blue::fill() method.