zoukankan      html  css  js  c++  java
  • 抽象工厂模式

    抽象工厂模式也是一种工厂模式,只是它生产的对象是工厂。

    代码演示

    第1步:创建Shape的接口

    Shape.java

    1 public interface Shape {
    2    void draw();
    3 }

    第2步:创建实现相同接口的具体类

    Rectangle.java

    1 public class Rectangle implements Shape {
    2 
    3    @Override
    4    public void draw() {
    5       System.out.println("Inside Rectangle::draw() method.");
    6    }
    7 }

    Square.java

    1 public class Square implements Shape {
    2 
    3    @Override
    4    public void draw() {
    5       System.out.println("Inside Square::draw() method.");
    6    }
    7 }

    Circle.java

    1 public class Circle implements Shape {
    2 
    3    @Override
    4    public void draw() {
    5       System.out.println("Inside Circle::draw() method.");
    6    }
    7 }

    第3步:创建一个Colors接口

    Color.java

    1 public interface Color {
    2    void fill();
    3 }

    第4步:创建实现相同接口的具体类

    Red.java

    1 public class Red implements Color {
    2 
    3    @Override
    4    public void fill() {
    5       System.out.println("Inside Red::fill() method.");
    6    }
    7 }

    Green.java

    1 public class Green implements Color {
    2 
    3    @Override
    4    public void fill() {
    5       System.out.println("Inside Green::fill() method.");
    6    }
    7 }

    Blue.java

    1 public class Blue implements Color {
    2 
    3    @Override
    4    public void fill() {
    5       System.out.println("Inside Blue::fill() method.");
    6    }
    7 }

    第5步:创建实现相同接口的具体类

    AbstractFactory.java

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

    第6步:创建实现相同接口的具体类

    创建工厂类,根据给定信息扩展AbstractFactory以生成具体类的对象

    ShapeFactory.java

     1 public class ShapeFactory extends AbstractFactory {
     2 
     3    @Override
     4    public Shape getShape(String shapeType){
     5 
     6       if(shapeType == null){
     7          return null;
     8       }
     9 
    10       if(shapeType.equalsIgnoreCase("CIRCLE")){
    11          return new Circle();
    12 
    13       }else if(shapeType.equalsIgnoreCase("RECTANGLE")){
    14          return new Rectangle();
    15 
    16       }else if(shapeType.equalsIgnoreCase("SQUARE")){
    17          return new Square();
    18       }
    19 
    20       return null;
    21    }
    22 
    23    @Override
    24    Color getColor(String color) {
    25       return null;
    26    }
    27 }

    ColorFactory.java

     1 public class ColorFactory extends AbstractFactory {
     2 
     3    @Override
     4    public Shape getShape(String shapeType){
     5       return null;
     6    }
     7 
     8    @Override
     9    Color getColor(String color) {
    10 
    11       if(color == null){
    12          return null;
    13       }        
    14 
    15       if(color.equalsIgnoreCase("RED")){
    16          return new Red();
    17 
    18       }else if(color.equalsIgnoreCase("GREEN")){
    19          return new Green();
    20 
    21       }else if(color.equalsIgnoreCase("BLUE")){
    22          return new Blue();
    23       }
    24 
    25       return null;
    26    }
    27 }

    第7步:创建工厂生成器/生产器类,通过传递如ShapeColor等信息来获取工厂

    FactoryProducer.java

     1 public class FactoryProducer {
     2    public static AbstractFactory getFactory(String choice){
     3 
     4       if(choice.equalsIgnoreCase("SHAPE")){
     5          return new ShapeFactory();
     6 
     7       }else if(choice.equalsIgnoreCase("COLOR")){
     8          return new ColorFactory();
     9       }
    10 
    11       return null;
    12    }
    13 }

    第8步:使用FactoryProducer来获取AbstractFactory,以便通过传递类型等信息来获取具体类的工厂

    AbstractFactoryPatternDemo.java

     1 public class AbstractFactoryPatternDemo {
     2    public static void main(String[] args) {
     3 
     4       //get shape factory
     5       AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
     6 
     7       //get an object of Shape Circle
     8       Shape shape1 = shapeFactory.getShape("CIRCLE");
     9 
    10       //call draw method of Shape Circle
    11       shape1.draw();
    12 
    13       //get an object of Shape Rectangle
    14       Shape shape2 = shapeFactory.getShape("RECTANGLE");
    15 
    16       //call draw method of Shape Rectangle
    17       shape2.draw();
    18 
    19       //get an object of Shape Square 
    20       Shape shape3 = shapeFactory.getShape("SQUARE");
    21 
    22       //call draw method of Shape Square
    23       shape3.draw();
    24 
    25       //get color factory
    26       AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
    27 
    28       //get an object of Color Red
    29       Color color1 = colorFactory.getColor("RED");
    30 
    31       //call fill method of Red
    32       color1.fill();
    33 
    34       //get an object of Color Green
    35       Color color2 = colorFactory.getColor("Green");
    36 
    37       //call fill method of Green
    38       color2.fill();
    39 
    40       //get an object of Color Blue
    41       Color color3 = colorFactory.getColor("BLUE");
    42 
    43       //call fill method of Color Blue
    44       color3.fill();
    45    }
    46 }

    第9步:验证输出,结果如下

    1 Inside Circle::draw() method.
    2 Inside Rectangle::draw() method.
    3 Inside Square::draw() method.
    4 Inside Red::fill() method.
    5 Inside Green::fill() method.
    6 Inside Blue::fill() method.
  • 相关阅读:
    SqlServer查询优化方法
    关于导入excel问题
    修改SQL数据库中表字段类型时,报“一个或多个对象访问此列”错误的解决方法
    软件架构之我见
    算法-插入排序
    算法-快速排序
    WCF系列 Restful WCF
    WCF系列 基础概念
    cocos2dx-是男人就坚持20s 练手项目
    nodejs 聊天室简单实现
  • 原文地址:https://www.cnblogs.com/wangzhisdu/p/8142722.html
Copyright © 2011-2022 走看看