跳至主要内容

设计模式 笔记

文章请参考:
http://caterpillar.onlyfun.net/Gossip/DesignPattern/index.html

-> 表示类的继承

1.factory
f -> concreateF
p->concreatP

f.createP

concreateF.createP(return new concreateP)

2.abstract factory:
af
af->cf1
af->cf2
apa->pa1
apa->pa2
apb->pb1
apb->pb2
cf1.createA(return new pa1)
cf1.createB(return new pb1)
cf2.createA(return new pa2)
cf2.createB(return new pb2)

3.singleton
A
A::instance(){ return this.instance or this.instance=new A}

使用:
a=A::instance()

4.builder
最简单的例子:
   1: //Client同时充当了Director的角色
   2: StringBuilder builder = new StringBuilder();
   3: builder.Append("happyhippy");
   4: builder.Append(".cnblogs");
   5: builder.Append(".com");
   6: //返回string对象:happyhippy.cnblogs.com
   7: builder.ToString(); 
builder模式主要是有个build part的过程,这个过程叫director,它build的参数,顺序不同都是不同的结果,而abstract factory没有这个过程
这句话解释的最好:
客户端调用:director负责发布命令,builder才负责真正的生产,所以向builder要产品而不是director
//调用   
public class Client {   
    public static void main(String[] args) {   
        Builder builder = new ConcreteBuilder();   
        Director director = new Director( builder );    
        director.construct(); //builder负责真正的生产   
      Product product = builder.getResult();  //向builder要产品而不是向director要产品 
 } 
是想builder要产品,更换不同的director, builder能够得到不同的产品
1、需要生成的产品对象有复杂的内部结构。
2、需要生成的产品对象的属性相互依赖,建造者模式可以强迫生成顺序。
3、当复杂对象的创建应该独立于该对象的组成部分和装配方式时

但是有个问题是这个builder就必须以传址的方式传递给director。

5. prototype
就是
P::clone
p1=p.clone();
复制本身。

结构型模式

1.bridge模式
对象分析和设计中其实基于两个原则:
a.一个类只有一个引起它变化的原因
b.使用组合而不是继承来实现功能需求

实现方式:
就是当类有两个以上的强烈变化的原因的时候,使用birdge而非继承,避免类的数量过于膨胀。
AbstractRoad
AbstractRoad->SpeedWay 
AbstractRoad->Street 
AbstractP 
AbstractP->Man
AbstractP->Woman
AbstractV
AbstractV::run()
AbstractV::road
AbstractV::P
AbstractV->Car
AbstractV->Bus
car.p=Man
car.road=SpeedWay
car.run()
or b
us.p=woman
bus.road=street
bus.run()
这是一个简单的例子
2. Adapter 模式
就我理解是把一个interface 转化为另外一个interface,一定是interface而不是类。

Adapter,表示遇到的问题是接口不匹配。
Proxy,表示遇到的问题是主体逻辑与附加逻辑(持久化、网络传输等)纠缠。
这两句话解释了很多问题。
3. Decorator 模式
interface Meal {getContent(), price()}
Meal->FriedChiken
Meal->FiredHamburger
Meal-> abstract SideDish
SideDish->DishOne
SideDish->DishTwo
Meal meal=DishOne(FriedChicken);
meal.getContent()
meal.price()
in dishone, the getContent call the getcontent of friedChicken and combine the result.
the same as price. by this way we can add functions or make modification without hieritage.

4. composite 模式:
interface P: play()
P->Frame
P->PList:list
add Frame in Plist:list
and call Frame.play() in Plist.play for each list element.

5. flyweight 模式:
把也许会重复生成的小object 放在一个静态hash表里(可以通过factory模式来实现这个hash表)。create一个小object时,先看这个hash表里有没有,没有就生成一个并在hash表里添加这个object的指针,如果有就不重新生成,直接从hash表里取出,前提是这个object并不会被其他的程序所更改。
6.facade模式
如果你的函数中需要调用很多库的类,那么你的函数就会与库有很高的耦合度,如果库有变化,那么你就需要改写每一处使用这些库的地方,facade模式就是抽象出一个service,让service提供一些函数来实现对库的操作或者是组合内容,这样你的函数中就只需要调用service的函数,好处是库变化是,你只需要更改你的service,而不用更改你每一处用到库的地方。不好是你又加了一层复杂度,有时候会变得更加难以理解。
7. proxy模式
比如这个:
Proxy
在原始逻辑上添加一层而已。

行为模式

1.Template模式
将父类作为骨架,将实作的实现放到子类来填充,
2. Chaine of responsiblity 模式
具体用例请参见文章开始的引文,我的理解是这样的:
如果你有一个procedure,比如这种:

if a then
  do a 
else if b then 
  do b
else if 
....

endif
这种情况下,如果你的处理流程改了,需要增加或者更改顺序,(比如a ,b ...上有语义上的包含关系,更改顺序会使得结果截然不同),那么你都需要重新更改这段程序,而且如果这种关系比较复杂,那么更改的时候会比较痛苦。
这是後通过构造一个统一的基础虚类,然后把不同的操作实作为衍生类,然后通过他们之间的调用顺序来实现不同的执行顺序,避免了大量的if then,语义上看起来也比较清晰
h=new a(new b(new c()));
h.handle()
就解决了,而且也把不同的处理流程完全分开了,使得程序的耦合性降低。

3. command 模式

这个模式很常见,我自己都常常在用,比如说在spoop中的那个controller class, command 传入以后, 调用CommandAction,将receiver放在传回的值当中或者用JsonResponse中,


这个uml图就很好解释了这个关系,receiver不一定要封装在command中,也可以注入

4. strategy 模式

这个模式的核心思想和template很像

strategy就是:
      Server server = new Server(9999, new EchoService());
        server.start();
template是:
     GuessGame game = new ConsoleGame();
     game.go();

strategy 是传入不同的组件来实现不同的服务,template是用不同的子类来实现通过父类定义好的模板。

5. observer 模式

这个就是消息分发机制的抽象,当subject 管理一个obeserver列表,当subject的状态改变时,通知列表里的obersever,obersever此时执行update().

Mediator 模式


这个图说的很明白,就好比需要一个中间人来处理一群共事的同时之间的关系,使得团队各个成员的工作彼此独立,交互更有弹性。(比如更换成员,比如更改联系方式)

visitor 模式

将工作流程与操作物件的类型的特定结构分离,将特定结构放在vistor中,这样方便随时修改也改善了耦合关系。

评论

此博客中的热门博文

jquery on 的问题

return false  from  within a jQuery event handler  is effectively the same as calling both  e.preventDefault and  e.stopPropagation  on the passed  jQuery.Event object. e.preventDefault()  will prevent the default event from occuring,  e.stopPropagation()  will prevent the evet from bubbling up and  return false  will do both. Note that this behaviour differs from  normal  (non-jQuery) event handlers, in which, notably,  return false   does  not  stop the event from bubbling up . 所以on前半部分的selector必须是静态的。

chrome extension Error: attempting to use a disconnected prot object

if you get this error: Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:236 chromeHidden.Port.dispatchOnDisconnect miscellaneous_bindings:236 Uncaught Error: Attempting to use a disconnected port object miscellaneous_bindings:58 PortImpl.postMessage miscellaneous_bindings:58 responseCallback miscellaneous_bindings:143 xhr.onreadystatechange That means you have make some mistake as this discussed: This is caused when a connection get closed. For example if you open a tab that has the content_script injected, it opens a connection, the tab is closed, and then the background_page tries to pass a message. It will fail because the tab is no longer active to receive the message. In your case I would guess that as tabs close and new tabs open you are attempting to post messages with the old tabId instead of creating a new connection to the new tab. I would recommend reading through the  long-lived connections s

工作合同 , 离职,工会 相关

转自战法: 平常受主管和同事歧视和欺负,准备把他们告到 Prud'hommes, 所以最近和工会的人走的比较近,学会如何 monter un dossier, 和一些保护自己的方法,特别想跟大家分享。 1. Il faut acter 也就是说,如果同事对你有行为或言语上的 Harcèlement, 当下不要过激,但是一定要给主管发一封邮件。 邮件可以保存下来作为证据。 邮件是 Daté 的,可以记录时间日期。 只要邮件是你发的,对方回不回都无所谓,因为你已经公开表示了不满。 如果是言语骚扰,邮件里一定要引用同事骚扰你的原话。 邮件不要有攻击性,你的请求一定是合理的,但是让对方很难回复。比如我主管不批我的RTT,因为他说“RTT不能半天半天的批”,于是工会的哥们帮我起草了一封邮件,就说“我这个月想提交RTT申请的时候,惊讶地发现四月份的申请被拒了。我不明白,全公司的员工都可以请半天的RTT,为什么我不行?而且你已经批了我三月份两个半天RTT的申请。“ 2. Prise d'acte 如果公司没有很好的履行合同,没有支付工资,同事对你有骚扰,甚至是暴力,等等,你就可以立刻终止你的合同。 这个很厉害,不要轻易使用。因为如果法官判定你的请求不合理,就要赔偿公司 Préavis 的钱。 相关链接: http://vosdroits.service-public.fr/F24409.xhtml http://www.journaldunet.com/management/pratique/contrats/4643/la-prise-d-acte.html http://www.juritravail.com/Actualite/prise-acte-rupture-par-salarie/Id/2136  (prise d'acte 和 démission 的区别) 3. 保存邮件 最好是把你所有的邮件都保存在U盘上,备份在自己家的电脑,以免公司突然给你来个 mise à pied, 你就没办法带走任何证据了。但是千万不要大张旗鼓地做,要不然公司就会怀疑你要做什么对公司不好的事。 4. 最有,一些有用链接: http://vosdroits.service-public.fr/particuliers/N19806.xh