JDK detailed design patterns (a) Create Type

1. singleton pattern (the Singleton) : there is only one instance of the disclosure provide external access point

  (1) NumnerFormat only one instance

  (2) Runtime (JVM running on the system, the system can obtain information Runtime): and Runtime are singletons, starving formula

0. static factory (Factory's) : according to the parameters passed to the method used to construct an example, create an instance of Foreign hide details

  ( Static factory does not belong to 23 design patterns, but listed here in order to understand)

  (1) Integer.valueOf () Integer class to create a packaging according to the value of the generic type Integer

  (2) Class.forName () to create an instance of reflection according fully qualified class name

2. Factory Method pattern (Factory's Method,) : abstract factory, abstract achieve loose coupling products, the implementation class decide what products output

  Collection Collection.iterator collection implements Iteatorable interface inside an internal iterator class, the method may be obtained by the iterator Iterator () (this method is that factory method)

  Collection collection on behalf of the factory, the iterator Iterator representative of the product

  Method Collection representative of the abstract factory interface producing such products for Iterator (), returns an abstract interface to the product and specific collection class implements this interface (specifically List, concrete Set peer entity type), produced Iterator is to achieve product interface Iterator class specific production of any specific product by the realization of what concrete factory

 

 

 3 abstract factory pattern (the Abstract Factory's). : Abstract factory interface to a variety of products

(1) java.sql package creation Statement Connection

 JDBC的使用中,加载驱动后使用DriverManager.gerConnection获取的是Connection的实例,Connection是连接接口,而后由Connection创建用以执行不同sql语句的Statement,预编译PreparedStatement和用以执行存储过程的CallableStatement.这时Connection相当于抽象工厂接口,可生产多种抽象产品。

  Connection就成为了抽象工厂,而Statement,PreparedStatement,CallableStatemen是多种抽象产品,抽象工厂可生产多种抽象产品,而对应的实例工厂MysqlCollectionImpl生产实力产品Impl

 

 

 4.建造者模式(Builder):构造一个实例需要大量的工作,所以将构建逻辑提到了单独的类中

  (1)解析XML时构建总的document需要遍历所有节点,包括注释文本等  ,建造过程繁琐,就就将它分离出来

 

 

5.原型模式(Prototype):克隆,深拷贝,浅拷贝

  (1)Object.clone / Cloneable

  

Guess you like

Origin www.cnblogs.com/autism-dong/p/12150658.html