几种典型的设计模式

//工厂模式
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值