image-20210114192912129

图说设计模式

创建型(Design Pattern)

1. 简单工厂(Simple Factory)

image-20210114192932043

一个Factory根据传入的参数不同生产各种实例Product

Java获取不同加密算法的密钥生成器

KeyGenerator keyGen=KeyGenerator.getInstance("DESede");

2. 工厂方法(Factory Method)

image-20210114192938475

一个Factory有一个对应的Product

JDBC

Connection conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=DB;user=sa;password=");
Statement statement=conn.createStatement();
ResultSet rs=statement.executeQuery("select * from UserInfo");

3. 抽象工厂(Abstract Factory)

image-20210114192947042

4. 建造者模式(Builder)

Builder作为抽象父类, 定义了ABC等若干的build方法

在ConcreteBuilder中实例Product, 并且实现Builder中的build方法, 留一个return Product的对外方法

由Director选取哪一种ConcreteBuilder进行build

image-20210114192955091

5. 单例模式(Singleton)

保证一个类只有一个实例, 并且提供一个访问该实例的全局访问点

  • 任务管理器
  • 回收站
  • 配置文件读取
  1. 构造函数私有
  2. 靠一个get的static方法return出去一个static的成员
//
// Created by chunibyo on 2/27/20.
//
 
#ifndef INC_05_SINGLETON_SINGLETON_H
#define INC_05_SINGLETON_SINGLETON_H
 
class Singleton {
 
protected:
    static Singleton *singleton;
 
    Singleton() = default;
 
    ~Singleton() {
        delete singleton;
    }
 
public:
    static Singleton *getInstance() {
        if (singleton == nullptr) {
            singleton = new Singleton();
        }
        return singleton;
    }
 
};
 
Singleton *Singleton::singleton = nullptr;
 
#endif //INC_05_SINGLETON_SINGLETON_H

结构型(Structural Pattern)

1. 适配器模式(Adapter)

把需要与外界交互那个接口继承下来, 再在继承下来的Adapter里面声明多个Adaptee进行交互

比如说想要调用XXuse()方法, 但是只有use()方法, 就可以在子类里面调用了

注意: C++父类使用子类方法是在父类的virtual方法情况下, 与Java不同

// Adapter.cpp
 
void Adapter::use() {
    adaptee->specific_use();
}
 
Adapter::Adapter(Adaptee *adptee) {
    this->adaptee = adptee;
}

3. 装饰器模式(Decorator)

image-20210114193008855

#include <iostream>
 
using std::cout;
using std::endl;
 
class Component {
public:
    virtual void operation() = 0;
};
 
class ConcreteComponent : public Component {
public:
    void operation() override {
        cout << "拍照" << endl;
    }
};
 
class Decorator : public Component {
public:
    Component *component;
 
    explicit Decorator(Component *_component) {
        component = _component;
    }
 
};
 
class ConcreteDecorator : public Decorator {
public:
    explicit ConcreteDecorator(Component *_component) : Decorator(_component) {
 
    }
 
    void operation() override {
        cout << "美颜" << endl;
        component->operation();
    }
 
};
 
int main() {
    (new ConcreteComponent())->operation();
    cout << endl;
    (new ConcreteDecorator(new ConcreteComponent()))->operation();
}

image-20210114193020275

4. 外观模式(Facade)

image-20210114193027870

#include <iostream>
 
using std::cout;
using std::endl;
 
class system {
public:
    virtual void operation() = 0;
};
 
class systemA : public system {
public:
    void operation() override {
        cout << 'A' << endl;
    }
};
 
class systemB : public system {
public:
    void operation() override {
        cout << 'B' << endl;
    }
};
 
class systemC : public system {
public:
    void operation() override {
        cout << 'C' << endl;
    }
};
 
class Facade {
    systemA *system_a;
    systemB *system_b;
    systemC *system_c;
public:
    explicit Facade() {
        system_a = new systemA();
        system_b = new systemB();
        system_c = new systemC();
    }
 
    void operation() {
        system_a->operation();
        system_b->operation();
        system_c->operation();
    }
 
    virtual ~Facade() {
        delete system_a;
        delete system_b;
        delete system_c;
    }
};
 
int main() {
    (new Facade())->operation();
}

5. 享元模式(Flyweight)

创建一个享元工厂来负责维护一个享元池(Flyweight Pool)用于存储具有相同内部状态的享元对象。

享元模式的目的就是使用共享技术来实现大量细粒度对象的复用。

#include <iostream>
#include <unordered_map>
 
using std::cout;
using std::endl;
using std::string;
 
class Tree {
public:
    string name;
 
    explicit Tree(string name) {
        cout << name << " created" << endl;
        this->name = std::move(name);
    }
};
 
class TreeFactory {
    static std::unordered_map<string, Tree *> m;
public:
    static Tree *getTree(const string &name) {
        if (m.find(name) != m.end()) return m[name];
        Tree *tree = new Tree(name);
        m[name] = tree;
        return tree;
    }
};
 
std::unordered_map<string, Tree *> TreeFactory::m;
 
int main() {
    TreeFactory::getTree("A");
    TreeFactory::getTree("B");
    TreeFactory::getTree("A");
}

6. 代理模式(Proxy)

#include <iostream>
 
using std::cout;
using std::endl;
 
class Subject {
public:
    virtual void request() {
        cout << "Base" << endl;
    }
};
 
class Proxy : public Subject {
    void beforeRequest() {
        cout << "before" << endl;
    }
 
    void afterRequest() {
        cout << "after" << endl;
    }
 
    Subject *subject;
 
public:
    void request() override {
        beforeRequest();
        subject->request();
        afterRequest();
    }
 
    Proxy() {
        subject = new Subject();
    }
};
 
int main() {
    (new Proxy())->request();
}

行为模式(Behavioral Pattern)

1. 命令模式(Command)

将命令封装成对象

image-20210114193039850

#include<iostream>
 
using std::cout;
using std::endl;
 
class Receiver {
public:
    void action() {
        cout << "Receiver Action" << endl;
    }
};
 
class Command {
protected:
    Receiver *receiver{};
public:
    virtual void setReceiver(Receiver *_receiver) {
        receiver = _receiver;
    }
 
    virtual void execute() = 0;
};
 
class ConcreteCommand : public Command {
public:
    void execute() override {
        cout << "Command Execute" << endl;
        receiver->action();
    }
};
 
class Invoker {
    Command *command;
public:
    explicit Invoker(Command *_command) {
        command = _command;
    }
 
    void call() {
        cout << "Command Invoke" << endl;
        command->execute();
    }
};
 
int main() {
    Command *cmd = new ConcreteCommand();
    cmd->setReceiver(new Receiver());
    (new Invoker(cmd))->call();
}