解释器嘛,就是个翻译,比如有道词典什么的,哈哈。

解释器模式 Interpreter

题义:给定一个语言后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器。客户端可以使用这个解释器来解释这个语言中的句子。解释器模式将描述怎样在有了一个简单的文法后,使用模式设计解释这些语句。在解释器模式里面提到的语言是指任何解释器对象能够解释的任何组合。在解释器模式中需要定义一个代表文法的命令类的等级结构,也就是一系列的组合规则。每一个命令对象都有一个解释方法,代表对命令对象的解释。命令对象的等级结构中的对象的任何排列组合都是一个语言。

问题:

A class of problems occurs repeatedly in a well-defined and well-understood domain. If the domain were characterized with a “language”, then problems could be easily solved with an interpretation “engine”.

意图:

  • 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.

阅读全文

今天忽然看到几篇博客,一个说 Servlet 是单例的,一个说不是单例的,于是自己试下。

废话不说上代码:

web.xml

1
2
3
4
5
6
7
8
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.xdnote.test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>

阅读全文

设计模式有个特点,就是一看模式的命名,就大致知道是干什么的,使用场景也可能会和名称上的动作相关。

命令行模式 Command

命令模式把一个请求或者操作封装到一个对象中。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。命令模式允许请求的一方和发送的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否执行,何时被执行以及是怎么被执行的。系统支持命令的撤消。

问题:

Need to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.

意图:

  • Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
  • Promote “invocation of a method on an object” to full object status
  • An object-oriented callback

阅读全文

责任链模式,个人感觉就是一个有序的协作模式,(PS:不知道无序的协作模式可不可以算做是责任链模式)

责任链模式 Chain

在责任链模式中,很多对象由每一个对象对其下家的引用而接起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。客户并不知道链上的哪一个对象最终处理这个请求,系统可以在不影响客户端的情况下动态的重新组织链和分配责任。处理者有两个选择:承担责任或者把责任推给下家。一个请求可以最终不被任何接收端对象所接受。

问题:

There is a potentially variable number of “handler” or “processing element” or “node” objects, and a stream of requests that must be handled. Need to efficiently process the requests without hard-wiring handler relationships and precedence, or request-to-handler mappings.

意图:

  • Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
  • Launch-and-leave requests with a single processing pipeline that contains many possible handlers.
  • An object-oriented linked list with recursive traversal.

阅读全文

看名字就知道是干嘛的了。

代理模式 Proxy

看名字就能开解了。就是A可以访问B不能访问C,B能访问C,所以B把A的请求给C,把C的响应给A,顺便加点广告什么的,跳墙、VPN之类的都是如此。

问题:

You need to support resource-hungry objects, and you do not want to instantiate such objects unless and until they are actually requested by the client.

意图:

  • Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
  • Wrap a complicated subsystem with a simpler interface

阅读全文

运用共享技术有效的支持大量细粒度的对象。 有点像对象可以变的单例。

享元模式 Flyweight

问题:

Designing objects down to the lowest levels of system “granularity” provides optimal flexibility, but can be unacceptably expensive in terms of performance and memory usage.

意图:

  • Use sharing to support large numbers of fine-grained objects efficiently.
  • The Motif GUI strategy of replacing heavy-weight widgets with light-weight gadgets.

阅读全文

这是任何编程里面使用最多的一个模式了!只要有松耦合思想,就有外观模式!

外观模式 Facdade

为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。。

问题:

A segment of the client community needs a simplified interface to the overall functionality of a complex subsystem.

意图:

  • Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
  • Wrap a complicated subsystem with a simpler interface

阅读全文

装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象

装饰模式 Decorator

问题:

You want to add behavior or state to individual objects at run-time. Inheritance is not feasible because it is static and applies to an entire class…

意图:

  • Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
  • Client-specified embellishment of a core object by recursively wrapping it.
  • Wrapping a gift, putting it in a box, and wrapping the box.

阅读全文