抽象工厂模式

抽象工厂模式,即工厂的工厂模式,它用于创建其它工厂。它同样是基于创建模式的。在抽象工厂模式中,有一个接口负责创建与对象相关的工厂,且不需要指明这些对象所属的类。当该接口创建出工厂之后,该工厂就可以使用工厂模式中提及的方式进行使用。

【实现】

  • 首先,创建两个接口,一个用于创建图形,另一个用于创建颜色,
  • 其次,分别为上述两个接口创建实现类
  • 再次,创建一个抽象的工厂,该工厂内置两个方法,一个用于获取图形,一个用于获取颜色
  • 最后,定义图形工厂与颜色工厂,且这两个具体的工厂继承自上步中的抽象工厂并实现相关方法

类图如下所示:

针对上面的类图,可以这样理解,一个消费者,它想要正方形,想要红色,想要绿色,想要圆形,经过简单的分组,发现正方形与长方形都是图形,红色与绿色都是颜色,

此时借鉴普通的工厂模式,可以设计一个图形工厂,依据消费者指定的图形类型给出相应的图形,再设计出一个颜色工厂,也是依据颜色类型给出相应的颜色。

此时就有了两个工厂,而如果让消费者直接操作这两个工厂的话,岂不麻烦,而工厂模式再次可以解决这个问题,即图形工厂也是工厂,颜色工厂也是工厂,这里再创建

一个工厂的工厂,那不就完活了么。于是再创建一个抽象类(用接口也可以),并声明获取图形与获取颜色的两个抽象方法,然后在工厂的工厂中提供获取工厂的方法。

最终,消费者可以通过工厂的工厂获取所需的工厂,然后调用该工厂获取图形或获取颜色的方法以获取最终的实体对象。

 

【代码实现】

public interface Shape {
   void draw();
}
public class Square implements Shape {

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

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

 

public interface Color {
   void fill();
}
public class Red implements Color {

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

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

 

public abstract class AbstractFactory {
   abstract Color getColor(String color);
   abstract Shape getShape(String shape) ;
}
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;
   }
}
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;
   }
}
public class Factory_of_Factory {
   public static AbstractFactory getFactory(String choice){
      if(choice.equalsIgnoreCase("SHAPE")){
         return new ShapeFactory();
      } else if(choice.equalsIgnoreCase("COLOR")){
         return new ColorFactory();
      }
      return null;
   }
}

 

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

      //get shape factory
      AbstractFactory shapeFactory = Factory_of_Factory.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 = Factory_of_Factory.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();
   }
}

 

/***************************************外星人乔丹拍板时间***********************************************/

抽象工厂模式,就是工厂模式的工厂模式

posted @ 2014-07-14 21:43  欧文-6-6-6  阅读(249)  评论(0编辑  收藏  举报