每日总结

软件设计模式 装饰模式:

 

 

 
#include <iostream>
#include <string>
using namespace std;
class Phone {
public:
    virtual void Bling()=0;
};
class SimplePhone :public Phone{
public:
    SimplePhone(Phone* phone);
    void Bling();
};
class Decorator :public Phone{
public:
    Phone* phone;
    Decorator() {};
    Decorator(Phone* phone);
    void Bling();
};
class JarPhone :public Decorator{
public:
    JarPhone(Phone* phone);
    void Bling();
};
class ComplexPhone :public Decorator {
public:
    ComplexPhone(Phone* phone);
    void Bling();
};
SimplePhone::SimplePhone(Phone* phone) {
    cout << "SimplePhone出厂\n";
}
void SimplePhone::Bling() {
    cout << "来电话了,发出声音\n";
}
Decorator::Decorator(Phone* phone) {
    this->phone = phone;
}
void Decorator::Bling() {
    cout << "来电话了\n";
}
JarPhone::JarPhone(Phone* phone) {
    cout << "升级为JarPhone\n";
}
void JarPhone::Bling() {
    cout << "发出声音并震动\n";
}
ComplexPhone::ComplexPhone(Phone* phone) {
    cout << "升级为ComplexPhone\n";
}
void ComplexPhone::Bling() {
    cout << "发出声音震动并闪烁\n";
}
int main() {
    Phone* phone=NULL;
    phone = new SimplePhone(phone);
    phone->Bling();
    JarPhone* jar = new JarPhone(phone);
    jar->Bling();
    ComplexPhone* com = new ComplexPhone(phone);
    com->Bling();
    system("pause");
    return 0;
}

 

posted @ 2021-10-07 10:08  chenghaixinag  阅读(24)  评论(0编辑  收藏  举报