• Java设计模式——抽象工厂模式


    抽象工厂模式也是创建模式,可以把它理解成创建工厂的工厂,这种模式也是我们经常使用的。
    在抽象工厂中的接口是用来创建工厂的,每个生成的工厂又都可以按照工厂模式创建其他对象。

    举例说明:

    1. 创建Shape接口及其实现类Circle、Square、RoundedRectangle、RoundedSquare
    2. 创建抽象工厂类AbstractFactory
    3. 创建工厂类ShapeFactory,它继承AbstractFactory抽象类
    4. 创建工厂生成类FactoryProducer
    5. 创建AbstractFactoryPatternDemo类,用来测试抽象工厂模式

    1、创建接口Shape

    public interface Shape {
       void draw();
    }
    

    2、创建接口Shape实现类

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

    3、创建抽象类AbstractFactory

    public abstract class AbstractFactory {
       abstract Shape getShape(String shapeType) ;
    }
    

    4、创建抽象类AbstractFactory的实现类

    public class ShapeFactory extends AbstractFactory {
       @Override
       public Shape getShape(String shapeType){    
          if(shapeType.equalsIgnoreCase("RECTANGLE")){
             return new Rectangle();         
          }else if(shapeType.equalsIgnoreCase("SQUARE")){
             return new Square();
          }  
          return null;
       }
    }
    
    public class RoundedShapeFactory extends AbstractFactory {
       @Override
       public Shape getShape(String shapeType){    
          if(shapeType.equalsIgnoreCase("RECTANGLE")){
             return new RoundedRectangle();         
          }else if(shapeType.equalsIgnoreCase("SQUARE")){
             return new RoundedSquare();
          }  
          return null;
       }
    }
    

    5、创建工厂生成类FactoryProducer

    public class FactoryProducer {
       public static AbstractFactory getFactory(boolean rounded){   
          if(rounded){
             return new RoundedShapeFactory();         
          }else{
             return new ShapeFactory();
          }
       }
    }  

    6、测试

    public class AbstractFactoryPatternDemo {
       public static void main(String[] args) {
          //get shape factory
          AbstractFactory shapeFactory = FactoryProducer.getFactory(false);
          //get an object of Shape Rectangle
          Shape shape1 = shapeFactory.getShape("RECTANGLE");
          //call draw method of Shape Rectangle
          shape1.draw();
          //get an object of Shape Square 
          Shape shape2 = shapeFactory.getShape("SQUARE");
          //call draw method of Shape Square
          shape2.draw();
          //get shape factory
          AbstractFactory shapeFactory1 = FactoryProducer.getFactory(true);
          //get an object of Shape Rectangle
          Shape shape3 = shapeFactory1.getShape("RECTANGLE");
          //call draw method of Shape Rectangle
          shape3.draw();
          //get an object of Shape Square 
          Shape shape4 = shapeFactory1.getShape("SQUARE");
          //call draw method of Shape Square
          shape4.draw();
          
       }
    }

    7、结果

    Inside Rectangle::draw() method.
    Inside Square::draw() method.
    Inside RoundedRectangle::draw() method.
    Inside RoundedSquare::draw() method.
  • 相关阅读:
    Unity3d发布IOS(包含u3d自带IAP内购)的流程-小白篇(四)-Xcode配置发布部分
    U3D获取IOS设备所在时区、是否安装指定APP、判断真机还是模拟器
    Unity3d发布IOS(包含u3d自带IAP内购)的流程-小白篇(三)-u3d配置ios内购部分
    Unity3D 粒子系统
    Unity 3D 之Playerprefs
    物理公式
    C#之Lambda表达式
    C#中的Stack
    C#中的as(转)
    (转) Unity3D常用代码收集总结
  • 原文地址:https://www.cnblogs.com/iceriver315/p/16074139.html
Copyright © 2020-2023  润新知