Anywhere

水中苇
随笔 - 28, 文章 - 3, 评论 - 0, 引用 - 0
数据加载中……

Template模式.------strategy模式.

在父类中定义方法,将具体实现放到方法中.
class father
{
 
public:
   
void method() //方法
   {
     fun1();
     fun2();
   }

 
protected:
   
virtual void fun1()=0//细节1
   virtaul void fun2()=0//细节2
}
;
class child:public father
{
  
private:                //实现细节.
    void fun1()
     
{}
    
void fun2()
     
{}
}

调用代码:
 father 
*pc=new child;
 pc
->method();
 
缺点:代码复用性不强.
strategy模式采用的是类组合的方式实现.
class strategy
{
 
public:
   
virtual void operation()=0;   
}
;
class child:public strategy
{
  
public;
     
void operation()
      
{}
}
;
class X
{
  
public:
    
void method()
     
{p->operation();}
  
private:
     strategy 
*p;
}
;

posted on 2009-08-12 09:27 三水寿 阅读(185) 评论(0)  编辑 收藏 引用 所属分类: 设计模式学习笔记----行为模式


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理