创建型设计模式
目录 工厂方法模式 (factory method pattern) 单例模式(singleton pattern) 抽象工厂模式 (abstract factory pattern) 生成器模式 (builder pattern) 原型模式(prototype pattern)
定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。 工厂方法模式 意图 定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。 适用性 当一个类不知道它所必须创建的对象的类的时候。 当一个类希望由它的子类来指定它所创建的对象的时候。 当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候。 OOP中,经常会遇到对象创建和初始化。主要是上述的两种情况 在第一种情况中,对象比较复杂,包含很多的初始化代码,因此应该将这些代码放到一个Factory类中,充当类工厂,以减少重复的对象创建代码。典型的例子是数据库的链接创建(演示示例图) 。 第二种情况也较为常见,OOP中的一个重要概念就是类的继承。派生于同一个基类的对象往往都是具备相似的功能,仅仅是具体的实现根据不同环境而异,因此创建这些对象的时候就需要根据不同的情况(或者输入参数)而异。将这些代码移入专门的Factory类同样对减低代码重复有非常明显的作用。典型例子是XMLDB的DAO对象创建(演示示例图)
工厂方法模式实例: 采用new的if-else设计
工厂方法模式实例(2): 工厂方法模式
工厂方法模式实例(3): 客户端代码
Singleton: a class that has only one instance 单例模式 Singleton: a class that has only one instance
Singleton pattern singleton: an object that is the only object of its type ensures that a class has at most one instance provides a global access point to that instance takes responsibility of managing that instance away from the programmer (illegal to construct more instances) provide accessor method that allows users to see the (one and only) instance possibly the most known / popular design pattern! (this should tell you something)
Restricting objects, continued One way to avoid creating objects: use static methods instead Math, System, JOptionPane is this a good alternative choice? Why or why not? Problem: lacks flexibility Example: static methods can't be passed as an argument to a method, nor returned Problem: cannot be extended Example: static methods can't be subclassed and overridden like a singleton's could be
Implementing Singleton make constructor(s) private so that they can not be called from outside declare a single static private instance of the class write a public getInstance() or similar method that allows access to the single instance possibly protect / synchronize this method to ensure that it will work in a multi-threaded program
Singleton sequence diagram
Singleton example consider a singleton class RandomGenerator that generates random numbers public class RandomGenerator { private static RandomGenerator gen = new RandomGenerator(); public static RandomGenerator getInstance() { return gen; } private RandomGenerator() {} ... possible problem: always creates the instance, even if it isn't used
Singleton example 2 variation: don't create the instance until needed // Generates random numbers. public class RandomGenerator { private static RandomGenerator gen = null; public static RandomGenerator getInstance() { if (gen == null) { gen = new RandomGenerator(); } return gen; ... What could go wrong with this version?
Singleton example 3 variation: solve concurrency issue by locking // Generates random numbers. public class RandomGenerator { private static RandomGenerator gen = null; public static synchronized RandomGenerator getInstance() { if (gen == null) { gen = new RandomGenerator(); } return gen; ... Is anything wrong with this version?
Singleton example 4 variation: solve concurrency issue without unnecessary locking // Generates random numbers. public class RandomGenerator { private static RandomGenerator gen = null; public static RandomGenerator getInstance() { if (gen == null) { synchronized (RandomGenerator.class) { // must test again -- can you see why? // sometimes called test-and-test-and-set (TTS) gen = new RandomGenerator(); } return gen;
抽象工厂(Abstract Factory) Purpose Provide an interface for creating families of related or dependent objects without specifying their concrete class Applicability The factory determines the actual concrete type of object to be created, and it is here that the object is actually created (in C++, for instance, by the new operator). However, the factory only returns an abstract pointer to the created concrete object. 虚线代表创建,因为客户端只和AbstractFactory,AbstractProductA, AbstractProductB相识,因此当有新产品加入时,只需加入这些具体的产品以及相应的具体工厂实现即可,客户端代码不需要修改。
抽象工厂示例 创建支持多视感风格标准的用户界面工具包: 支持多种视感标准; 不同标准之间具有可移植性;
生成器模式(Builder) Intent To separate the construction of a complex object from its representation, so that the same construction process can create different representations. Applicability The construction processes are independent from the representations of the concrete objects Construction allows the difference representations of concrete objects 复杂对象有多个子对象,但是子对象的构建过程是一样的,只是每个子对象的表示不同,这样每个子对象的具体表示封装在具体生成器中,Director可以调用抽象类的构建过程接口。当有新类型的子对象时,只需要动态得创建相应的生成器并把该生成器引用传给Director,而无需改变Director的代码。
Using prototype to construct objects, and copy these prototypes Intent Using prototype to construct objects, and copy these prototypes Applicability avoid subclasses of an object creator in the client application, like the abstract factory pattern does. avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is prohibitively expensive for a given application. 最大的优点是允许只通过客户注册原型实例就可以讲一个新的具体产品类并入系统。比其他创建型模式更为灵活。
Q1: Summarize the differences among the five creational design patterns? Difference between copy constructor and prototype pattern?