设计模式小结

所有模式

     创建型模式,共五种:工厂方法模式、抽象工厂模式单例模式建造者模式原型模式
结构型模式,共七种:适配器模式装饰器模式代理模式外观模式桥接模式组合模式享元模式
    行为型模式,共十一种:策略模式模板方法模式观察者模式迭代子模式责任链模式命令模式备忘录模式状态模式访问者模式中介者模式解释器模式

    补充模式:空对象模式


---------------------------   创建型模式   ------------------------------

1、工厂方法、抽象工厂 - 创建型模式    http://blog.csdn.net/paincupid/article/details/43803497
0)简单工厂模式(Simple Factory):
简单工厂模式是类的创建模式,又叫做静态工厂方法(Static Factory Method)模式。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。
        简单工厂模式是由一个具体的类去创建其他类的实例,父类是相同的,父类是具体的。
1)工厂方法模式(Factory Method): 工厂方法模式是有一个抽象的父类定义公共接口,子类负责生成具体的对象,这样做的目的是将类的实例化操作延迟到子类中完成。
Define an interface for creating an object,but let the subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses  翻译,"定义一个创建对象的接口,但是让子类来觉得该实例化那个类。工厂方法让一个类推迟实例化至它的子类中。"

2、抽象工厂模式(Abstract Factory): 抽象工厂模式提供一个创建一系列相关或相互依赖对象的接口,而无须指定他们具体的类。它针对的是有多个产品的等级结构。而工厂方法模式针对的是一个产品的等级结构。
Provide an interface for creating families of related or dependent objects without specifying their concrete classes
翻译,"为创建一组相关或相互依赖的对象提供一个接口,无需指定它们的具体类"。

3、单例模式    http://blog.csdn.net/paincupid/article/details/46689193
Ensure that only one instance of a class is created.
Provide a global point of access to the object.
Singleton单例模式目的在于确保一个类只有唯一的一个实例,并且这个唯一实例只有一个全局访问点。它确保类被实例一次,所有这个类的请求都指向这个唯一的实例。

4、建造者模式(builder pattern) - 创建型模式    http://blog.csdn.net/paincupid/article/details/43865915
Separate the construction of a complex object from its representation so that the same construction process can create different representations. 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

5、原型模式    http://blog.csdn.net/paincupid/article/details/46860283
specifying the kind of objects to create using a prototypical instance
   creating new objects by copying this prototype
不是基本类型,所以成员变量不会被拷贝,需要我们自己实现深拷贝。
   深拷贝与浅拷贝问题中,会发生深拷贝的有java中的8中基本类型以及他们的封装类型,另外还有String类型。其余的都是浅拷贝。
   使用原型模式创建对象比直接new一个对象在性能上要好的多,因为Object类的clone方法是一个本地方法,它直接操作内存中的二进制流,特别是复制大对象时,性能的差别非常明显。
   使用原型模式复制对象不会调用类的构造方法。因为对象的复制是通过调用Object类的clone方法来完成的,它直接在内存中复制数据,因此不会调用到类的构造方法。不但构造方法中的代码不会执行,甚至连访问权限都对原型模式无效。

   利用序列化实现深度拷贝。把对象写到流里的过程是串行化(Serilization)过程,又叫对象序列化,而把对象从流中读出来的(Deserialization)过程叫反序列化。应当指出的是,写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面,因此在Java语言里深复制一个对象,常常可以先使对象实现Serializable接口,然后把对象(实际上只是对象的一个拷贝)写到一个流里,再从流里读出来便可以重建对象。

----------------------------  结构型模式   --------------------------- 

6、适配器模式    http://blog.csdn.net/paincupid/article/details/46884927
Convert the interface of a class into another interface clients expect.
Adapter lets classes work together, that could not otherwise because of incompatible interfaces.
将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作。
类适配器使用对象继承的方式,是静态的定义方式;而对象适配器使用对象组合的方式,是动态组合的方式。
    
7、装饰者模式 - 结构型模式 http://blog.csdn.net/paincupid/article/details/44038877
动态地将责任附加到对象上,若要扩展对象,装饰者模式提供了比继承更弹性的替代方案。
装饰者和被装饰对象有相同的超类型。
OutputStream,InputStream,Reader,Writer等都用到了Decorator模式

8、代理模式(结构型模式)  http://blog.csdn.net/paincupid/article/details/43926757
对其他对象提供一种代理以控制对这个对象的访问。(Provide a surrogate or placeholder for another object to control access to it)。代理模式也叫做委托模式。
比较:代理则控制对对象的访问;装饰者为对象添加一个或多个功能;适配器为它所适配的对象提供了一个不同的接口

9、外观模式 - 结构型模式,也称之为门面模式。 http://blog.csdn.net/paincupid/article/details/44164411
为子系统中的一组接口提供一个一致的接口, Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
Provide a unified interface to a set of interface in a subsystem.Facade defines a higher-lever interface that make the subsystem easier to use.

10、桥连接模式 - 结构型模式     http://blog.csdn.net/paincupid/article/details/43538887
将抽象部分与实现部分分离,使它们都可以独立的变化。[The intent of this pattern is to decouple abstraction from implementation so that the two can vary independently.]
桥接模式的做法是把变化部分抽象出来,使变化部分与主类分离开来,从而将多个维度的变化彻底分离。最后,提供一个管理类来组合不同维度上的变化,通过这种组合来满足业务的需要

11、组合模式:结构型模式 http://blog.csdn.net/paincupid/article/details/44178215
允许你将对象组合成树形来表达结构来表现“整体/部分”层次结构。组合能让用户以一致的方式处理个别对象及对象组合。
包含其他组件的组件为组合对象;不包含其他组件的组件为叶节点对象。

12、享元模式 - 结构型模式 http://blog.csdn.net/paincupid/article/details/46896653
The intent of this pattern is to use sharing to support a large number of objects that have part of their internal state in common where the other part of state can vary.
运用共享技术有效地支持大量细粒度的对象.
享元模式的作用在于节省内存开销,对于系统内存在的大量相似的对象,通过享元的设计方式,可以提取出可共享的部分,将其只保留一份进而节省大量的内存开销。

享元模式的本质是:分离和共享。分离的是对象状态中变与不变的部分,共享的是对象中不变的部分。

--------------------------    行为型模式   ---------------------------------
13、策略模式 - 行为模式    http://blog.csdn.net/paincupid/article/details/46897043
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
定义一系列的算法,把每一个算法封装起来, 并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。也称为政策模式(Policy)。

14、模板方法 - 行为模式     http://blog.csdn.net/paincupid/article/details/46900891

模板方法Gof的定义是:在一个方法里定义算法的骨架,将一些步骤延迟到其子类。
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.
Template Method lets subclasses redefine certain steps of an algorithm without letting them to change the algorithm's structure.

模板方法模式中,抽象类的模板方法应该声明为final的。因为子类不能覆写一个被定义为final的方法。从而保证了子类的逻辑永远由父类所控制。子类必须实现的方法定义为abstract。而普通的方法(无final或abstract修饰)则称之为钩子。

15、观察者模式 - 行为模式 http://blog.csdn.net/paincupid/article/details/46941917
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
    
16、迭代模式 - 行为模式 http://blog.csdn.net/paincupid/article/details/44943831
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.(它提供一种方法访问一个容器对象中各个元素,而又不需要暴露该对象的内部细节)

 17、责任链模式 - 行为模式    http://blog.csdn.net/paincupid/article/details/46943547
 It avoids attaching the sender of a request to its receiver, giving this way other objects the possibility of handling the request too.
The objects become parts of a chain and the request is sent from one object to another across the chain until one of the objects will handle it.
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
    
 18、命令模式 - 行为模式    http://blog.csdn.net/paincupid/article/details/46947957
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或者记录请求日志,以及支持可撤消的操作。命令模式又称为动作(Action)模式或事务(Transaction)模式。
encapsulate a request in an object
allows the parameterization of clients with different requests
allows saving the requests in a queue
命令模式的本质是: 封装请求

19、备忘录模式 - 行为模式    http://blog.csdn.net/paincupid/article/details/46983185

备忘录模式: 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样就可以将该对象恢复到原先保存的状态。
The intent of this pattern is to capture the internal state of an object without violating encapsulation and thus providing a mean for restoring the object into initial state when needed.

备忘录模式又叫做快照模式(Snapshot Pattern)或Token模式,是对象的行为模式。
备忘录对象是一个用来存储另外一个对象内部状态的快照的对象。备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捕捉(Capture)住,并外部化,存储起来,从而可以在将来合适的时候把这个对象还原到存储起来的状态。备忘录模式常常与命令模式和迭代子模式一同使用。
    
20、状态模式 - 行为模式    http://blog.csdn.net/paincupid/article/details/46984077
状态模式:当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。 Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况。把状态的判断逻辑转移到表示不同的一系列类当中,可以把复杂的逻辑判断简单化。


21、访问者模式 - 行为模式    http://blog.csdn.net/paincupid/article/details/46988559
访问者模式:访问者模式是对象的行为模式。访问者模式的目的是封装一些施加于某种数据结构元素之上的操作。一旦这些操作需要修改的话,接受这个操作的数据结构则可以保持不变。 Represents an operation to be performed on the elements of an object structure.    Visitor lets you define a new operation without changing the classes of the elements on which it operates.

22、中介者模式 - 行为模式    http://blog.csdn.net/paincupid/article/details/46991371
中介者模式:定义一个中介对象来封装系列对象之间的交互。中介者使各个对象不需要显示地相互引用,从而使其耦合性松散,而且可以独立地改变他们之间的交互。
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
中介者模式的本质在于“封装交互”    中介者模式的目的,就是封装多个对象的交互,这些交互的多在中介者对象里实现。    只要是实现封装对象的交互,就可以使用中介者模式,不必拘泥于模式结构。

23、解释器模式 - 行为模式    http://blog.csdn.net/paincupid/article/details/46991977
解释器模式: 给定一个语言,定义它的文法的一种表示,并定义一种解释器,这个解释器使用该表示来解释语言中的句子。Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.    Map a domain to a language, the language to a grammar, and the grammar to a hierarchical object-oriented design    

24、空对象模式 - 行为模式

空对象模式: 提供一个对象,作为一个缺少类型对象的代理。Provide an object as a surrogate for the lack of an object of a given type.


转载请注明:http://blog.csdn.net/paincupid/article/details/46993573


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值