设计模式(二)——创建型模式

单例模式

1.私有的构造函数
2.私有的静态的全局变量
3.公有的静态的方法

public class Singleton {

    private static Singleton singleton;

    private Singleton(){
    }

    public static synchronized Singleton getSingleton(){
        if(singleton == null){
            singleton = new Singleton();
        }
        return singleton;
    }
}

建造者模式

1,私有化双构造方法
2,包装双构造提供对象初始和终结
3,不同属性不同方法去构造

package net.coding.demo;

public class Test {
    private  String id;
    private  String name;
    private  String code;
    private  String number;


    public Test id(String id) {
        this.id = id;
        return this;
    }
    public Test name(String name) {
        this.name = name;
        return this;
    }
    public Test code(String code) {
        this.code = code;
        return this;
    }   
    public Test number(String number) {
        this.number = number;
        return this;
    } 


    private Test(){
       super();
    }

    private Test(Test builder){
        this.id=builder.id;
        this.name=builder.name;
        this.code=builder.code;
        this.number=builder.number;
    }

    public static Test init(){
        return new Test();
    }
        
    public Test build(){
        return new Test(this);
    }

        
  public static void main(String[] args) {
      Test test =   Test.init()
                        .id("1")
                        .code("2")
                        .name("3")
                        .number("4")
                        .build();
      System.out.print(test);
  }
}

工厂模式

1,工厂接口约束方法
2,实体类实现工厂接口
3,工厂创建类基于给定信息生成实体类


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 class ShapeFactory {
    
   //使用 getShape 方法获取形状类型的对象
   public Shape getShape(String shapeType){
      if(shapeType == null){
         return null;
      }        
      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();
      } else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }
      return null;
   }
}

抽象工厂模式

1,多个工厂接口约束方法
2,抽象工厂类为工厂接口类对象生产提供方法
3,实体类实现工厂接口方法
4,工厂创建类实现抽象工厂类,基于给定的信息生成实体类的对象


public interface Shape {
   void draw();
}

public interface Color {
   void fill();
}

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


public class Factory 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
    public 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;
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值