Stereotype mode

Builders mode

Builder mode the benefits and advantages of
using the Builder pattern will inevitably lead to code and write it twice SETTER methods related attributes, it looks a bit thankless. However, it should see is that usability and readability client code has been greatly improved. At the same time, the number of arguments constructor significantly reduced call is very intuitive.
Another advantage is that the method Builder, Builder parameters can be adjusted during the creation of a plurality of objects when a single builder constructs can also be varied depending on the objects. It's like I'm getting respected in order to "change" should be "status quo." Builder mode is particularly suitable for many of those classes the number of attributes, I think that there is no need to set the present value does not need to pass parameters (setting null).
Builder mode at the same time improve code readability, use the IDE provides code completion feature is also easier. Builder modes are detailed in Josh Bloch's Effective Java 2nd second edition brings in a greater advantage to use with the constructor.

The costs and disadvantages of Builder mode
using the Builder pattern is sure to increase the amount of code. In addition, although the client code readability significantly improved, but the attendant client code more verbose. I still hold this view: Compared to increase the number of parameters, the same type of mix parameters, optional parameters in terms of the increase, improve code readability more valuable.
Builder will increase a class code, which means that developers in the class to increase property sometimes forget builder to add support to the property. To overcome this problem, I will usually nest builder to the class, so you can easily see which related builder needs to be updated. Despite the risk to forget still exists, but it is like to forget the risk of new properties to increase class toString (), equals (Object) , hashCode () , or other class-based method is the same for all properties.
Builder In my implementation, I will transfer the properties to customer needs with the Builder constructor rather than a set method. The advantage of this is that an object can always be a complete instantiation, rather than supplement with additional properties set method call is completed when the developer is instantiated. This also reflects the benefits of immutability brings. However, it in turn can cause its own set of attributes method reduces readability.

Summary
When building object, if you encounter a class has a lot of parameters - in which many of the same parameter types and number of parameters can be empty, I prefer Builder mode to complete. When a small number of parameters, and they are all different types must appear, by adding code to achieve Builder are often unable to reflect its superiority. In this case, it is desirable conventional method is to call the constructor. Again, if you do not remain the same, then use the no-argument constructor call the appropriate set method it.

Personal understanding: Construction of the model, so that the respective set value set by rewriting method look clearer. If the parameter set is not much recommended, too many seem too long is not recommended, but difficult to read.

Reference article http://www.importnew.com/6605.html

Simple plant
simple factory pattern is a factory class typically use static method to return a different object instances with different parameters received.
Do not modify the code, it can not be extended

Personal understanding: the so-called simple factory that the use of an extra class, and create a return based on different parameters into different (to achieve the same interface) object methods in the class. And the extra class is the factory class.
Benefits: Creating users to access regardless of which method chosen to create an object, based on mass participation can be achieved naturally.
Disadvantages: can not be expanded (not to comply with the open - closed principle), if adding a new realization of the different classes of the same interface must be modified method extra class (increase else if).

Write pictures described here

Factory method
Factory is to provide a factory class for each product. To create different products by different instances of factory instance.
In the same hierarchical structure, increase support any product.

Personal understood: the absence of an extra class to the required classes to obtain the actual (multiple), but by the actual requirements of each class has a corresponding additional class, these additional classes (particularly plant) to achieve the same an interface (abstract factory). Every new addition will increase the actual class implements a public interface of extra class.
Benefits: just create compliant concrete factory and implement the abstract factory interface, you can use. Without having to modify the existing code (observe open - closed principle)
downside: the user to determine their own new category. For each additional class of products will increase a product factory, increasing the amount of additional development.
Write pictures described here

Abstract Factory
Abstract Factory is to deal with the concept of the product family. For example, every car company may be at the same time the production of cars, trucks, buses, so each plant should have a method to create cars, trucks and buses.
Product family to deal with the concept was born, adding new product line is very easy, but can not add new products

个人理解:工厂方法创建的是一个一维层度上的。假如实际所需的类(比如奔驰,大众)又分成多个种类(轿车,货车,客车),那么通过工厂方法创建了实际所需类(产品族),再通过简单工厂创建第二层分类(产品类)就是一种抽象工程

区别
简单工厂 : 用来生产同一等级结构中的任意产品。(对于增加新的产品,无能为力)
工厂方法 :用来生产同一等级结构中的固定产品。(支持增加任意产品)
抽象工厂 :用来生产不同产品族的全部产品。(对于增加新的产品,无能为力;支持增加产品族)

Write pictures described here

参考文章
http://blog.csdn.net/lingfengtengfei/article/details/12374469
http://blog.csdn.net/superbeck/article/details/4446177
https://www.cnblogs.com/zhangchenliang/p/3700820.html

定义:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
Write pictures described here

原型模式主要用于对象的复制,它的核心是就是类图中的原型类Prototype。Prototype类需要具备以下两个条件:
实现Cloneable接口。在java语言有一个Cloneable接口,它的作用只有一个,就是在运行时通知虚拟机可以安全地在实现了此接口的类上使用clone方法。在java虚拟机中,只有实现了这个接口的类才可以被拷贝,否则在运行时会抛出CloneNotSupportedException异常。
重写Object类中的clone方法。Java中,所有类的父类都是Object类,Object类中有一个clone方法,作用是返回对象的一个拷贝,但是其作用域protected类型的,一般的类无法调用,因此,Prototype类需要将clone方法的作用域修改为public类型。
原型模式是一种比较简单的模式,也非常容易理解,实现一个接口,重写一个方法即完成了原型模式。在实际应用中,原型模式很少单独出现。经常与其他模式混用,他的原型类Prototype也常用抽象类来替代。

原型模式的优点及适用场景
使用原型模式创建对象比直接new一个对象在性能上要好的多,因为Object类的clone方法是一个本地方法,它直接操作内存中的二进制流,特别是复制大对象时,性能的差别非常明显。
使用原型模式的另一个好处是简化对象的创建,使得创建对象就像我们在编辑文档时的复制粘贴一样简单。
因为以上优点,所以在需要重复地创建相似对象时可以考虑使用原型模式。比如需要在一个循环体内创建对象,假如对象创建过程比较复杂或者循环次数很多的话,使用原型模式不但可以简化创建过程,而且可以使系统的整体性能提高很多。
原型模式的注意事项
使用原型模式复制对象不会调用类的构造方法。因为对象的复制是通过调用Object类的clone方法来完成的,它直接在内存中复制数据,因此不会调用到类的构造方法。不但构造方法中的代码不会执行,甚至连访问权限都对原型模式无效。还记得单例模式吗?单例模式中,只要将构造方法的访问权限设置为private型,就可以实现单例。但是clone方法直接无视构造方法的权限,所以,单例模式与原型模式是冲突的,在使用时要特别注意。
深拷贝与浅拷贝。Object类的clone方法只会拷贝对象中的基本的数据类型,对于数组、容器对象、引用对象等都不会拷贝,这就是浅拷贝。如果要实现深拷贝,必须将原型模式中的数组、容器对象、引用对象等另行拷贝。

个人理解:原型模式较为简单,即为原对象的拷贝 对象越复杂使用clone越快,否则则是new方法快
参考文章
http://blog.csdn.net/zhengzhb/article/details/7393528

定义:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样就可以将该对象恢复到原先保存的状态
Write pictures described here

备忘录模式的结构
发起人:记录当前时刻的内部状态,负责定义哪些属于备份范围的状态,负责创建和恢复备忘录数据。
备忘录:负责存储发起人对象的内部状态,在需要的时候提供发起人需要的内部状态。
管理角色:对备忘录进行管理,保存和提供备忘录。

备忘录模式的优缺点和适用场景
备忘录模式的优点有:
当发起人角色中的状态改变时,有可能这是个错误的改变,我们使用备忘录模式就可以把这个错误的改变还原。
备份的状态是保存在发起人角色之外的,这样,发起人角色就不需要对各个备份的状态进行管理。
备忘录模式的缺点:
在实际应用中,备忘录模式都是多状态和多备份的,发起人角色的状态需要存储到备忘录对象中,对资源的消耗是比较严重的。
如果有需要提供回滚操作的需求,使用备忘录模式非常适合,比如jdbc的事务操作,文本编辑器的Ctrl+Z恢复等。

参考文章
https://www.cnblogs.com/java-my-life/archive/2012/06/06/2534942.html
https://www.cnblogs.com/java-my-life/archive/2012/06/06/2534942.html
http://blog.csdn.net/zhengzhb/article/details/7697549

Published 21 original articles · won praise 6 · views 30000 +

Guess you like

Origin blog.csdn.net/soulonlyhlh/article/details/78634320