0%

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

命令行模式 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.
阅读全文 »

将对象组合成树形结构以表示“部分-整体”的层次结构。“组合” 使得用户对单个对象和组合对象的使用具有一致性,对于乘积比较复杂的数据结构使用。

组合模式 composite

问题:

Application needs to manipulate a hierarchical collection of “primitive” and “composite” objects. Processing of a primitive object is handled one way, and processing of a composite object is handled differently. Having to query the “type” of each object before attempting to process it is not desirable.

意图:

  • Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
  • Recursive composition
  • “Directories contain entries, each of which could be a directory.”
  • 1-to-many “has a” up the “is a” hierarchy.
阅读全文 »

经常使用到正则,没想到都这么熟了有时候还要翻资料,这里小小记录下下。

正则并不难,关键是多练,其实老实来说正则的性能并不是很高,关键是多练。以下要背熟于心,不要给我说用到的时候再百度一下什么的:

字符 意义
|转义,匹配有意义字符的本身时使用(一些程序自带转义要多打一个,不解释)
. 匹配除换行符以外的任意字符
\w 匹配字母或数字或下划线或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束
\W 匹配任意不是字母,数字,下划线,汉字的字符
\S 匹配任意不是空白符的字符
\D 匹配任意非数字的字符
\B 匹配不是单词开头或结束的位置
[^x] 匹配除了x以外的任意字符
[^abc] 匹配除了abc这几个字母以外的任意字符
* 重复零次或更多次
+ 重复一次或更多次
? 重复零次或一次
{n} 重复n次
{n,} 重复n次或更多次
{n,m} 重复n到m次

上面只是最最最基本的,最常用的,想用点高级功能可以再去找资料。这里给点示例代码

先是javaScript里面使用正则

//场景1:只需要得到是否匹配,使用test
var result = /MSIE [\w.]+/.test('Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS124307)');
if(result ){
    alert('ie');
}
else{
    alert('不匹配');
}

//场景2:除了要知道是否匹配,还要提出字符串
var result = /(MSIE) ([\w.]+)/.exec('Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS124307)');
if(result ){
    console.log(result[1]);
    console.log(result[2]);
}else{
    alert('不匹配');
}
阅读全文 »