MFC中,App/CCtrlView/CListView/CListCtrl/Column and Item分别是Director/Abstract
Builer/Concrete Builer/Product/Part。
●Tip 4:优点。Finer control over the assembling process。
控制的“粒度”比较精细体现在Director和Builder的分离上(想一下Abstract
Factory是“construct products in one shot”的),Builder的职责是Create
a part and assemble it to product,Director的职责是Control
builder's assembling process。
Maze* MazeGame::CreateMaze( Mazebuilder & builder )
{
builder.BuildMaze(); //create the Product
builder.BuildRoom(1); //create 3 Parts of Product and assemble them to the Product
builder.BuildRoom(2);
builder.BuildDoor(1, 2);
return builder.GetMaze();
}
●Tip 4:优点。算法集中,易于维护。Related behavior
isn't spread over the classes defining the object structure; it's
localized in a visitor。
●Tip 5:支持变化。Visitor makes adding new operations easy。图中的黄色Class就是假想后来扩充的。
●Tip 6:局限性。
只能用于 The classes defining the object structure rarely
change, but you often want to define new operations over the
structure。 因为 Most of these operations will need to treat
nodes that represent assignment statements differently from nodes
that represent variables or arithmetic expressions。所以 Adding
new ConcreteElement classes is hard。所以 If the object structure
classes change often, then it's probably better to define the
operations in those classes。
Visitor and Element之间是紧耦合。Breaking encapsulation.
Visitor's approach assumes that the ConcreteElement interface is
powerful enough to let visitors do their job. As a result, the
pattern often forces you to provide public operations that access an
element's internal state, which may compromise its encapsulation.
可以看到,首先,concreteListener调用concreteBroadcaster.AddListener(
this )“订阅”消息;然后某时,concreteBroadcaster调用Broadcast()“发布”消息,已订阅消息的concreteListener的ListenToMessage()被调用执行。
●Tip 3:实现和使用。
When the dependency relationship between Broadcasters and Listeners
is particularly complex, an object that maintains these
relationships might be required. We call such an object a
ChangeManager. ChangeManager is an instance of the Mediator pattern.
Observer pattern appears in Smalltalk
Model/View/Controller (MVC)。。。
●Tip 4:支持变化。The
Observer pattern lets you vary subjects and observers independently.
All a subject knows is that it has a list of observers, each
conforming to the simple interface of the abstract Observer class。注意图中和Framework内的对象相关的依赖都是良性依赖,被标成绿色,正是这些良性依赖使得Application中的Changeable性高的对象能够比较自由地变化。
●Tip 3:实现和使用。Colleague passes a reference to itself
as an argument to ColleagueChanged() to let the Mediator identify
the Colleague that changed。
MFC中的Dialog是典型的Mediator,其所有control都向它报告消息。
●Tip 4:优点。It centralizes control. That can help clarify
how objects interact in a system. The Mediator pattern trades
complexity of interaction for complexity in the mediator.Colleagues
send and receive requests from a Mediator object. The mediator
implements the cooperative behavior by routing requests between the
appropriate colleague(s).
●Tip 5:支持变化。It decouples colleagues. A mediator
promotes loose coupling between colleagues. You can vary and reuse
Colleague and Mediator classes independently。可以改Mediator::ColleagueChanged()的具体实现,以实现不同的“更新”策略;也可以增加Colleague,Mediator只需做少许相应改动。改动在图中用黄色标出。