简短的理解设计模式

本文介绍了23种设计模式,包括Adapter、Facade、Composite等结构型模式;Singleton、Builder等创建型模式;Observer、Mediator等行为型模式。每种模式都简述了其目的和应用场景,帮助读者更好地理解和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Adapter:to provide the interface that a client expects while using the services of a class with a different interface.

接口的转换, 将一个类的接口转换成客户希望的另外一个接口。Java中的WindowAdapter,就是这个,熟悉吧。

Facade:to provide an interface that makes a subsystem easy to use.
为子系统中的一组接口提供一个一致的界面, 定义了一个高层接口, 这个接口使得这一子系统更加容易使用。我的感觉就是,对外屏蔽所有的底层细节。用户只需要了解这个facade接口就可以了,其他的我全部不关心。

Composite: to let clients treat individual obects and compositions of objects uniformly.
将对象组合成树形结构表示'整体-部分'的层次结构。Composite使得用户对单个对象和组合对象的使用更具有一致性。就是定义一个统一的接口,对整体与部分都通过这个接口来访问。程序看起来比较轻爽。

Bridge:to decouple an abstraction from the implementation of its abstract operations, so that the abstraction and its implementation can vary independently.
将抽象部分与它的实现部分分离,使它们都可以独立地变化。这个不是很好理解。。。汗。。贴两篇其他文章:http://www.jdon.com/designpatterns/bridge.htmhttp://smartfool.javaeye.com/blog/74659

Singleton: to ensure that a class has only instance and to provide a global point of access to it.
保证一个类仅有一个实例, 并提供一个访问它的全局访问点。就是一个类只能实例化一个对象。有恶汉,懒汉模式之分。

Observer: to define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified so that they can react to the change.
定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。这个模式相当的有用,就有点像那个消息通讯机制,有消息来了就通知你,没消息就啥都不管。Java中的Observer已经实现了这个模式。

Mediator: to define an object that encapsulates how a set of objects interact; this promotes loose coupling, keeping the objects from referring to one another explicitly, and lets you vary their interaction independently.
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。增加一个中介的对象,这个中介的对象与其他所有的对象进行交互,其他对象之间就无需交互了。

Proxy: to control access to an object by providing a surrogate, or placeholder, for it.
为其他对象提供一种代理以控制对这个对象的访问。想一下网关,很相似。

Chain of responsibility: to avoid coupling the sender of a request to its receiver, by giving more than one object a change to handle the request.
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。感觉想数据包的路由过程。。。

Flyweight: to use sharing to support large numbers of fine-grained objects efficiently.
运用共享技术有效地支持大量细粒度的对象。目的是,通过共享来达到减少内存消耗的目的,与cache不同,cache是利用局部性原理(时间局部性或者空间局部性)来提高查询命中率。

Builder: to move the construction logic for an object outside the class to be instantiated.
将一个复杂对象的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。还在晕乎,这个。

Factory Method: to let a class developer define the interface for creating an object while retaining control of which class to instantiate.
定义一个用于创建对象的接口,而让子类来决定实例化哪一个类,从而使一个类的实例化延迟到子类,给子类带来了灵活性。感觉工厂模式违反了OCP,如果增加一个类,那么必须修改工厂类。但工厂类作为整个产品系列的中心,就必须知道每个类,类扩展了,自然要修改这个。

AbstractFactory: to allow the creation of families of related or dependent objects.
提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类

Prototype: to provide new objects by copying an example rather than by bringing forth new, uninitialized instance of a class.
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。Java的clone函数,就是这个模式的实现。

Memento: to provide storage and restoration of an object's state.
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可以将该对象恢复到原先保存的状态。感觉,就像是一个备份的过程。

Template method: to implement an algorithm in a method, deferring the definition of some steps of the algorithm so that other classes can redefine them.
使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。 Java抽象类,本身就是一个模板方法。模板方法的关键是:继承。与strategy其实有点类似。

State: to distribute state-specific logic across classes that represent an object's state.
允许一个对象在其内部状态改变时改变它的行为。貌似实现TCP这个比较有用,下次仔细研究下。

Strategy: to encapsulate alternative approaches, or strategies, in separate classes that each implement a common operation.
Strategy模式允许多种策略共存,而代码不会混乱。策略模式也可以实现模式选择逻辑和策略本身相分离。Strategy模式的意图在于把可选的策略或方案封装在不同的类中,并在这些类中实现一个共同的操作。与模板方法不同的是,通过对象组合解决问题,而非继承。

Command: to encapsulate a request in an object.将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化。这个相当有用。感觉,就像是从同一类型的对象中抽象出一个接口来,同类型的对象都要实现这个接口。只不过,不同对象,其实现方式不一样而已。而在操作的时候,可以通过相同的操作来操作所有类似的对象,而无需知道它们的细节。

Interpreter: to let you compose executable objects according to a set of composition rules that you define.
给定一个语言,定义它的文法的一种表示,并定义一种解释器,这个解释器使用该表示来解释语言中的句子

Decorator: to let you compose new variationsof an operation at runtime.
动态地给一个对象添加一些额外的职责。Java的IO流,很多都用了这个模式。如 PrintStream pbs = new PrintStream( new BufferedOutputStream( new FileOutputStream( "test.bat" ) ) );(也许写错了单词)

Iterator: to provide a way to access the elements of a collection sequentially.
提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。就是为了实现对象遍历。C++与JAVA的遍历器都属此类吧。

Visitor: to let you define a new operation for a hierarchy without changing the hierarchy classes.
表示一个作用于某对象结构中的各元素的操作。这个还晕乎中。

 

以上英文摘自《JAVA设计模式手册》

我对设计模式的理解是:

达到 对修改封闭,扩展开放 的目的

面向接口编程,或者面向抽象编程

优先使用对象组合,而不是类继承

寻找可能的变化并对象并进行抽象

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值