//工厂模式
#include <iostream>
using namespace std;
static const double PI = 3.14;
class Shape {
public:
virtual double Area() = 0;
virtual ~Shape() {}
};
class Rectangle : public Shape {
public:
Rectangle(int width, int height);
double Area();
private:
int _width;
int _height;
};
class Circle : public Shape {
public:
Circle(int radius);
double Area();
private:
int _radius;
};
class Creator {
public:
virtual Shape* CreateShape() = 0;
virtual ~Creator() {};
};
class RectCreator : public Creator {
public:
Shape* CreateShape() {
Rectangle *rect = new Rectangle(10, 20);
return rect;
}
};
class CircleCreator : public Creator {
public:
Shape* CreateShape() {
Circle* circle = new Circle(10);
return circle;
}
};
void Create(Creator* pCreator) {
Shape* pShape = pCreator->CreateShape();
pShape->Area();
delete pShape;
pShape = NULL;
}
int main(int argc, const char * argv[]) {
RectCreator rs;
CircleCreator cs;
Create(&rs);
Create(&cs);
return 0;
}
//单例模式(一个类只有一个实例,然后提高一个全局的接口来访问这个实例)
#include <iostream>
#include <cassert>
using namespace std;
class CSystemControl {
public:
static CSystemControl* instance();
private:
CSystemControl();
static CSystemControl* _instance;
};
CSystemControl* CSystemControl::_instance = 0;
CSystemControl* CSystemControl::instance() {
if(_instance == 0) _instance = new CSystemControl;
return _instance;
}
CSystemControl::CSystemControl() {
cout << "Construct a SystemControl object" << endl;
}
int main() {
CSystemControl *P1 = CSystemControl::instance();
CSystemControl *p2 = CSystemControl::instance();
ASSERT(p1 == p2);
delete p1;
return 0;
}
//适配器模式(把一个类的接口转化成另外一个类的接口)
//第一种实现方法:
#include <iostream>
using namespace std;
class Target {
public:
virtual void Request() = 0;
virtual ~Target() {}
};
class Adaptee {
public:
void SpecificRequest() {
cout << "test adapter pattern" << endl;
}
};
class Adapter : public Target {
public:
Adapter() {
_adaptee = new Adaptee;
}
~Adapter() {
delete _adaptee;
}
void Request() {
_adaptee->SpecificRequest();
}
private:
Adaptee* _adaptee;
};
int main() {
Target* aTarget = new Adapter;
aTarget->Request();
delete aTarget;
return 0;
}
//第二种实现方法:
#include <iostream>
using namespace std;
class Target {
public:
virtual void Request() = 0;
virtual ~Target() {}
};
class Adaptee {
public:
void SpecificRequest() {
cout << "test adapter pattern" << endl;
}
};
class Adapter : public Target, private Adaptee {
public:
void Request() {
SpecificRequest();
}
};
int main() {
Target* aTarget = new Adapter();
aTarget->Request();
Adapter adapter;
adapter.Request();
//adapter.SpecificRequest(); //这种方法是错误的,因为是私有继承
return 0;
}
//观察者模式
#include <iostream>
using namespace std;
class Observer { //delegate
public:
virtual void Update() = 0;
virtual ~Observer() {}
};
class Engine {
public:
Engine(Observer& aObserver) : _observer(aObserver) {}
void Download() {
cout << "Subject finished!" << endl;
_observer.Update();
}
private:
Observer& _observer;
};
class AppUI : public Observer {
public:
AppUI() {
_engine = new Engine(*this);
}
~AppUI() {
delete _engine;
}
void Start() {
cout << "The subject is called!" << endl;
_engine->Download();
}
void Update() {
cout << "the observer is called!" << endl;
}
private:
Engine* _engine;
};
int main() {
AppUI* pUI = new AppUI;
pUI->Start();
delete pUI;
return 0;
}
几种典型的设计模式
最新推荐文章于 2022-11-17 08:44:38 发布