最近一周在学习设计模式。。出发点是,之前用nesc语言(类似C语言 的一种传感器上的语言)编写协议栈的时候,发现很难维护,很难增加新的功能。于是就联想到,看看面向对象的设计模式。一来是给自己充电。二来是想看能否对以后在新平台下重新开发协议栈有一些设计上的作用。
我看的design pattern explained这本书。作者不只是讲解设计模式,还讲解了作者的一些心得,怎么发现问题推导出的这些设计模式。。以及一些面向对象的一些更好的思想。。
比如:发现变化点;封装变化点;优先使用对象组合,而不是继承;松耦合;等等
另外在实现一些设计模式的时候,如bridge模式, strategy模式,decorator模式,observer模式的时候,也对以前C++的一些语法和编程机制的理解更深入了一些。比如为什么要使用虚函数等。
学习了书中的一部分呢模式。
这本书中的设计模式,facade模式,adapter模式,singleton模式是很容易懂,也很容易编程实现的模式。
bridge模式, strategy模式,decorator模式,observer模式 是相对需要理解的模式。
还有一些模式,没来得及学习。
下面部分简单实现的源代码。其中有一些是值得以后自己注意的地方
/*bridge模式;adapter模式;
class Drawing
{
public:
virtual void drawLine()
{
cout<<"draw line"<<endl;
}
virtual void drawCircle()
{
cout<<"draw circle"<<endl;
}
};
class preM1Drawing
{
public:
void M1drawLine()
{
cout<<"present M1 drawLine"<<endl;
}
void M1drawCircle()
{
cout<<"present M1 drawCircle"<<endl;
}
};
class V1Drawing:public Drawing
{
preM1Drawing *ppreM1;
public:
V1Drawing(preM1Drawing *p)
{
ppreM1=p;
}
void drawLine()
{
ppreM1->M1drawLine();
}
void drawCircle()
{
ppreM1->M1drawCircle();
}
};
class Shape
{
public:
Drawing *draw_obj;
public:
virtual void drawing()
{
cout<<"Shape draw"<<endl;
}
Shape(Drawing *obj)
{
draw_obj=obj;
}
};
class _Rectangle : public Shape
{
public:
_Rectangle(Drawing *obj):Shape(obj)
{
}
void drawing()
{
draw_obj->drawLine();
}
};
class _Circle: public Shape
{
public:
_Circle(Drawing *obj):Shape(obj)
{
}
void drawing()
{
draw_obj->drawCircle();
}
};
void main()
{
preM1Drawing * ppreM1=new preM1Drawing;
Drawing *pdraw=new V1Drawing(ppreM1);
Shape *pRec=new _Rectangle(pdraw);
pRec->drawing();
Shape *pCir=new _Circle(pdraw);
pCir->drawing();
}*/
/*
//总结:观察者模式。和windows事件通知,消息循环机制,非常的相似。。另外,这里还对observer运用的strategy模式
class _strategy
{
public:
virtual void update()
{
};
};
class s1strategy:public _strategy
{
public:
void update()
{
cout<<"using s1 strategy update observer"<<endl;
}
};
class s2strategy:public _strategy
{
public:
void update()
{
cout<<"using s2 strategy update observer"<<endl;
}
};
class _observer
{
public:
virtual void update()
{
s->update();
}
_observer(_strategy *p)
{
s=p;
}
_strategy *s;
};
class v1observer:public _observer
{
public:
void update()
{
cout<<"v1类型观察者 ";
s->update();
}
v1observer(_strategy *p):_observer(p)
{
}
};
class v2observer:public _observer
{
public:
void update()
{
cout<<"v2类型观察者 ";
s->update();
}
v2observer(_strategy *p):_observer(p)
{
}
};
class _subject
{
public:
void attach(_observer *p)
{
list[count++]=p;
cout<<"one observer attach"<<endl;
}
void detach()
{}
void notify()
{
for(int i=0;i<count;i++)
{
list[i]->update();
}
}
_subject()
{
for(int i=0;i<10;i++)
{
list[i]=NULL;
}
count=0;
}
_observer* list[10];
int count;
};
void main()
{
_strategy *s1=new s1strategy();
_observer *obs=new v1observer(s1);//第一种观察者
//也可以创建以策略2,3等不同策略的不同类型v2,v3等的观察者。/
//这里我是把strategy模式用于observer对象上,而不是subject。设计模式解析书上说是用于subject对象上,没有太明白
_observer *obs10=new v2observer(s1);//第二种观察者
_strategy *s2=new s2strategy();
_observer *obs100=new v2observer(s2);//第三种观察者
_subject *sub=new _subject;
sub->attach(obs);//但是对于subject对象而言,它不用管你什么策略,什么类型,只需要你用观察者指针注册,然后它有时候notify就行
sub->notify();
sub->attach(obs10);
sub->attach(obs100);
sub->notify();
}*/
/*
//singleton模式
class singleton
{
public:
static singleton* getInstance()
{
if(instance==NULL)
{
instance=new singleton;
cout<<"instance created"<<endl;
return instance;
}
cout<<"instance been used"<<endl;
return instance;
}
void singletonFun()
{
cout<<"do singletonFun"<<endl;
}
private:
singleton()//private的构造函数
{
};
static singleton *instance;
};
singleton * singleton::instance =NULL;
void main()
{
singleton *obj=singleton::getInstance();
obj->singletonFun();
singleton *p=singleton::getInstance();
obj->singletonFun();
}*/
#include <iostream>
using namespace std;
class _component
{
public:
virtual void fun()
{}
};
class v1component:public _component
{
public:
void fun()
{
cout<<"v1component do"<<endl;
}
};
class _decorator:public _component
{
public:
_component *pc;
void fun()
{
pc->fun();
}
_decorator(_component *t)
{
pc=t;
}
};
class a1decorator:public _decorator
{
public:
a1decorator(_component *t):_decorator(t)
{
}
void fun()
{
pc->fun();
cout<<"add a1 "<<endl;
}
};
void main()
{
_component *p=new v1component;
a1decorator *a=new a1decorator(p);//用a1类型的装饰对象去装饰v1类型的component;核心就是a1类型这个装饰对象包含v1类型的component的指针为自己的一个成员
a->fun();
}