目录
- 单例模式(singleton)
- 构建模式(Builder)
- 原型模式(Prototype)
- 工厂方法模式(Factory)
- 抽象工厂模式(Abstract Factory)
**
一、5种创建型模型
**
1.1 单例模式(singleton)
简介:
单例模式特别简单,目的是为类创建唯一的对象。
下面是单例模式的一种实现方式(单例模式的5种实现方式见,传送门):
public class Singleton { private volatile static Singleton singleton = null; //volatile关键字private Singleton(){} //私有化构造函数,使其不可继承public static Singleton getInstance() {if(singleton == null) {synchronized (Singleton.class) {if(singleton == null) {//double check,保证效率、安全性singleton = new Singleton();}}}return singleton;}
}
1.2 构建模式(Builder)
简介:
将一个复杂对象的构建和表现分离,使得不同的构建过程可以创建不同的表示
类图:
代码如下:
public class Product {/*** 定义一些关于产品的操作*/private String part1;private String part2;public String getPart1() {return part1;}public void setPart1(String part1) {this.part1 = part1;}public String getPart2() {return part2;}public void setPart2(String part2) {this.part2 = part2;}}
public interface Builder {public void buildPart1();public void buildPart2();public Product retrieveResult();}
public class ConcreteBuilder implements Builder {private Product product = new Product();/*** 产品零件建造方法1*/@Overridepublic void buildPart1() {//构建产品的第一个零件product.setPart1("编号:9527");}/*** 产品零件建造方法2*/@Overridepublic void buildPart2() {//构建产品的第二个零件product.setPart2("名称:XXX");}/*** 产品返还方法*/@Overridepublic Product retrieveResult() {return product;}
}
public class Director {/*** 持有当前需要使用的建造器对象*/private Builder builder;/*** 构造方法,传入建造器对象* @param builder 建造器对象*/public Director(Builder builder){this.builder = builder;}/*** 产品构造方法,负责调用各个零件建造方法*/public void construct(){builder.buildPart1();builder.buildPart2();}
}
public class Client {public static void main(String[]args){Builder builder = new ConcreteBuilder();Director director = new Director(builder);director.construct();Product product = builder.retrieveResult();System.out.println(product.getPart1());System.out.println(product.getPart2());}}
1.3 原型模式(Prototype)
简介:
原型模式要求对象实现一个可以“克隆”自身的接口,这样就可以通过复制一个对象实例来创建新的实例。通过原型来创建对象,就不需要关心实例本身的类型,只要实现了克隆自身的方法,就可以通过这 个方法获取新的对象,而无需new
原型模式有两种表现形式:1)简单形式 2)登记形式
类图:
代码如下:
public interface Prototype{public Prototype clone();public String getName();public void setName(String name);
}
public class ConcretePrototype1 implements Prototype {private String name;public Prototype clone(){ConcretePrototype1 prototype = new ConcretePrototype1();prototype.setName(this.name);return prototype;}public String toString(){return "Now in Prototype1 , name = " + this.name;}@Overridepublic String getName() {return name;}@Overridepublic void setName(String name) {this.name = name;}
}
public class ConcretePrototype2 implements Prototype {private String name;public Prototype clone(){ConcretePrototype2 prototype = new ConcretePrototype2();prototype.setName(this.name);return prototype;}public String toString(){return "Now in Prototype2 , name = " + this.name;}@Overridepublic String getName() {return name;}@Overridepublic void setName(String name) {this.name = name;}
}
public class PrototypeManager {/*** 用来记录原型的编号和原型实例的对应关系*/private static Map<String,Prototype> map = new HashMap<String,Prototype>();/*** 私有化构造方法,避免外部创建实例*/private PrototypeManager(){}/*** 向原型管理器里面添加或是修改某个原型注册* @param prototypeId 原型编号* @param prototype 原型实例*/public synchronized static void setPrototype(String prototypeId , Prototype prototype){map.put(prototypeId, prototype);}/*** 从原型管理器里面删除某个原型注册* @param prototypeId 原型编号*/public synchronized static void removePrototype(String prototypeId){map.remove(prototypeId);}/*** 获取某个原型编号对应的原型实例* @param prototypeId 原型编号* @return 原型编号对应的原型实例* @throws Exception 如果原型编号对应的实例不存在,则抛出异常*/public synchronized static Prototype getPrototype(String prototypeId) throws Exception{Prototype prototype = map.get(prototypeId);if(prototype == null){throw new Exception("您希望获取的原型还没有注册或已被销毁");}return prototype;}
}
public class Client {public static void main(String[]args){try{Prototype p1 = new ConcretePrototype1();PrototypeManager.setPrototype("p1", p1);//获取原型来创建对象Prototype p3 = PrototypeManager.getPrototype("p1").clone();p3.setName("张三");System.out.println("第一个实例:" + p3);//有人动态的切换了实现Prototype p2 = new ConcretePrototype2();PrototypeManager.setPrototype("p1", p2);//重新获取原型来创建对象Prototype p4 = PrototypeManager.getPrototype("p1").clone();p4.setName("李四");System.out.println("第二个实例:" + p4);//有人注销了这个原型PrototypeManager.removePrototype("p1");//再次获取原型来创建对象Prototype p5 = PrototypeManager.getPrototype("p1").clone();p5.setName("王五");System.out.println("第三个实例:" + p5);}catch(Exception e){e.printStackTrace();}}
}
1.4 工厂方法模式(Factory)
简介:
工厂方法的用意是定义一个创建对象的工厂接口,将实际创建工作推迟到子类中。
类图:(工厂与抽象工厂)
代码如下:
interface Product {}class ProductA implements Product {}class ProductB implements Product {}abstract class Factory {public abstract Product makeProduct();}class FactoryA extends Factory {@Overridepublic Product makeProduct() {return new ProductA();}}class FactoryB extends Factory {@Overridepublic Product makeProduct() {return new ProductB();}}public class Client {public static void main(String[] args) {Factory factory = new FactoryA();Product product = factory.makeProduct();//对象为:com.obob.test.ProductA@15db9742System.out.println(product);}}