设计模式(8):组合模式 Composite

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

组合模式 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//场景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('不匹配');
}
阅读更多

设计模式(7):桥接模式 Bridge

将抽象部分与它的实现部分分离,使它们都可以独立地变化。

桥接模式 Bridge

问题:

“Hardening of the software arteries” has occurred by using subclassing of an abstract base class to provide alternative implementations. This locks in compile-time binding between interface and implementation. The abstraction and implementation cannot be independently extended or composed..

意图:

  • Decouple an abstraction from its implementation so that the two can vary independently.
  • Publish interface in an inheritance hierarchy, and bury implementation in its own inheritance hierarchy.
  • Beyond encapsulation, to insulation.
阅读更多

设计模式(6):适配器模式

将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.

适配器模式

问题:

An “off the shelf” component offers compelling functionality that you would like to reuse, but its “view of the world” is not compatible with the philosophy and architecture of the system currently being developed.

意图:

  • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
  • Wrap an existing class with a new interface.
  • Impedance match an old component to a new system.
阅读更多

设计模式(5):原型模式 Prototype

在JavaScript中,通常使用prototype关键字实现面向对象。但在设计模式中,简单点说就是复制。其实入门时学习的那个什么排序,用一个temp变量复制另一个值用于对换也是一种相当迷你的原型模式。

原型模式 Prototype

问题:

Application “hard wires” the class of object to create in each “new” expression.

意图:

  • Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
  • Co-opt one instance of a class for use as a breeder of all future instances.
  • The new operator considered harmful.
阅读更多

设计模式(4):构造者模式 Builder

如果一个对象由多个元素组成,而且这些元素可组成不同的对象,用构造者模式去分离出这个对象的构造过程。

构造者模式 Builder

问题

An application needs to create the elements of a complex aggregate. The specification for the aggregate exists on secondary storage and one of many representations needs to be built in primary storage.

意图:

  • Separate the construction of a complex object from its representation so that the same construction process can create different representations.
  • Parse a complex representation, create one of several targets.
阅读更多

设计模式(3):单例模式 Singleton

单例模式,最大使用,最大范围,什么,你的项目没用?功能主治:大量减小new运算,降低性能开销。

单例模式 Singleton

问题:

Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.

意图:

  • Separate the construction of a complex object from its representation so that the same construction process can create different representations.
  • Parse a complex representation, create one of several targets
阅读更多

设计模式(2): 工厂方法模式 Factory Method

工厂模式可以理解为简单工厂的修复版,增加要生产的对象的时候可以不用修改的你工厂,只用增加相应的生产类就行了,符合设计模式的那个什么什么规范。

工厂方法模式 Factory Method

问题:

A framework needs to standardize the architectural model for a range of applications, but allow for individual applications to define their own domain objects and provide for their instantiation.

意图:

  • Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
  • Defining a “virtual” constructor.
  • The new operator considered harmful.
阅读更多