UML软件工程组织

Pattern Tips 之六
作者:温昱(选自松耦合空间)
Command,Memento,Iterator。都是抽象出一个高聚合的Servant Class。

剩下的其它模式。Interpreter,Flyweight,Singleton,Chain of Respondsibility。

----------------------------------Iterator----------------------------

●Tip 1:关键字。Cursor,Aggregate。

●Tip 2:图。



可以看到,为了达到“将Aalgorithm从Data分离出来”的目的,代价是1.5 个对象:新增Iterator,Aggregate增加CreateIterator()操作(这样可以使Client仅依赖于Aggregate这个Interface)。

●Tip 3:实现和使用。讨论模板和CreateIterator()操作。。。external iterator。。。internal iterator。。还有STL。。。MFC的EnumChildWindow()。。

●Tip 4:优点。Iterators simplify the Aggregate interface. Iterator's traversal interface obviates the need for a similar interface in Aggregate, thereby simplifying the aggregate's interface。

●Tip 5:支持变化。Separating the traversal mechanism from the List object lets us define iterators for different traversal policies without enumerating them in the List interface。More than one traversal can be pending on an aggregate。图中的黄色Class就是假想后来扩充的。

●Tip 6:局限性。Notice that the iterator and the list are coupled, and the client must know that it is a list that's traversed as opposed to some other aggregate structure.



----------------------------------Chain of Respondsibility----------------------------

●Tip 1:关键字。。

●Tip 2:图。

可以看到,就是著名的“自包含”(Self-contain)。

●Tip 3:实现。

MFC中,CCmdTarget的子类们,实现不同的OnWndMsg()来实现Chain of Respondsibility。

在MFC中,message struct就是Command Class的例子,它沿着CCommandTarget链传递,直至被处理。

----------------------------------Chain of Respondsibility and Composite----------------------------

chain of Respondsibility and composite

Chain of Responsibility is often applied in conjunction with Composite.
There, a component's parent can act as its successor.


There are two possible ways to implement the successor chain: 
Define new links (usually in the Handler, but ConcreteHandlers could define them instead). 
Use existing links. 

Often the component-parent link (composite-children link) is used for a Chain of Responsibility.
It saves you from defining links explicitly. 

But if the structure doesn't reflect the chain of responsibility your application requires, 
then you'll have to define redundant links. 

HelpHandler base class has a var member named 
class HelpHandler {
    public:
        HelpHandler(HelpHandler* s) : _successor(s) { }
        virtual void HandleHelp();
    private:
        HelpHandler* _successor;
    };


but MFC's CCommandTarget has no var member, but use code in OnWndMsg()
----------------------------------发现模式----------------------------

数据驱动模式:不是对接口编程,也不是对实清b编程,而是对数据编程;而且,特殊在,这个对象就是纯数据对象;想一下业务建模时的业务实体,有点味道;再想一下抽象在设计模式中的意义,可以说数据对象是抽象对象,或者将一个对象拆成二个对象。

                                      上一页     下一页

 

版权所有:UML软件工程组织