factory method pattern singleton pattern abstract factory pattern
目录 厂方法模式 (factory method pattern) 单例模式(singleton pattern) 抽象 厂模式 (abstract factory pattern) 生成器模式 (builder pattern) 原型模式(prototype pattern)
Connection conn=Driver. Manager. get. Connection( 厂方法模式实例(4): 框架 JDK ◦ 参见教材 P 70. ◦ JDBC Connection conn = Driver. Manager. get. Connection(String url)
单例模式 Singleton: a class that has only one instance 11
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) 12
Restricting objects, continued One way to avoid creating objects: use static methods instead ◦ Math, System, JOption. Pane ◦ is this a good alternative choice? Why or why not? n Problem: lacks flexibility n n Example: static methods can't be passed as an argument to a method, nor returned Problem: cannot be extended n Example: static methods can't be subclassed and overridden like a singleton's could be 13
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 get. Instance() 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 14
Singleton sequence diagram 15
Singleton example consider a singleton class Random. Generator that generates random numbers public class Random. Generator { private static Random. Generator gen = new Random. Generator(); public static Random. Generator get. Instance() { return gen; } private Random. Generator() {}. . . } possible problem: always creates the instance, even if it isn't used 16
Singleton example 2 variation: don't create the instance until needed // Generates random numbers. public class Random. Generator { private static Random. Generator gen = null; public static Random. Generator get. Instance() { if (gen == null) { gen = new Random. Generator(); } return gen; } } . . . What could go wrong with this version? 17
Singleton example 3 variation: solve concurrency issue by locking // Generates random numbers. public class Random. Generator { private static Random. Generator gen = null; public static synchronized Random. Generator get. Instance() { if (gen == null) { gen = new Random. Generator(); } return gen; } } . . . Is anything wrong with this version? 18
Singleton example 4 variation: solve concurrency issue without unnecessary locking // Generates random numbers. public class Random. Generator { private static Random. Generator gen = null; } public static Random. Generator get. Instance() { if (gen == null) { synchronized (Random. Generator. class) { // must test again -- can you see why? // sometimes called test-and-set (TTS) if (gen == null) { gen = new Random. Generator(); } } } return gen; } 19
抽象 厂(Abstract Factory) Purpose Provide an interface for creating families of related or dependent objects without specifying their concrete class • The factory determines the actual concrete type of object to be created, and it is here that the object is actually Applicabil created (in C++, for instance, by the new operator). ity However, the factory only returns an abstract pointer to the created concrete object.
生成器模式(Builder) Intent To separate the construction of a complex object from its representation, so that the same construction process can create different representations. • The construction processes are independent from the Applica representations of the concrete objects bility • Construction allows the difference representations of concrete objects
原型模式(Prototype) Intent Using prototype to construct objects, and copy these prototypes • avoid subclasses of an object creator in the client application, like Applica the abstract factory pattern does. • avoid the inherent cost of creating a new object in the standard way bility (e. g. , using the 'new' keyword) when it is prohibitively expensive for a given application.
Q 1: Summarize the differences among the five creational design patterns? Difference between copy constructor and prototype pattern?
- Slides: 24