设计模式

文章目录

  • 23种设计模式
    • 1. 单例模式 Singleton
        • 单例对象的自动释放(自动delete单例对象)的4种方式
          • ①atexit( ) + 饿汉模式
          • ②友元类
          • ③内部类+private+static静态数据成员
          • ④pthread_once( )
    • 2. 编译防火墙 Pimpl
          • Line.h
          • Line.cc
          • testLine.cc
    • 3. 代理模式 Proxy
    • 4. 适配器模式
    • 5. Reactor pattern(反应器模式)
    • 6. Proactor模式

23种设计模式

设计模式_第1张图片

1. 单例模式 Singleton

单例模式Singleton(只能创建一个对象)
#include 

using std::cout;
using std::endl;

//设计需求:一个类只能创建一个对象
//适用场景:全局唯一的资源、对象、变量都可以使用单例模式

class Singleton
{
public:
    static Singleton *getInstance()
    {
        if(nullptr == _pInstance)
        {
            _pInstance = new Singleton();
        }
        return _pInstance;
    }

    static void destroy()
    {
        if(_pInstance)
        {
            delete  _pInstance;
            _pInstance = nullptr;
        }
    }

private:
    Singleton()
    {
        cout << "Singleton()" << endl;
    }

    ~Singleton()
    {
        cout << "~Singleton()" << endl;
    }

private:
    static Singleton *_pInstance;
};

Singleton *Singleton::_pInstance = nullptr;

/* Singleton gS1;//全局对象,error */
/* Singleton gS2;//全局对象,error */

int main()
{
    /* Singleton s1;//栈, error */
    /* Singleton s2;//栈, error */
    /* Singleton *ps1 = new Singleton();//error */
    Singleton *ps1 = Singleton::getInstance();
    Singleton *ps2 = Singleton::getInstance();
    cout << "ps1 = " << ps1 << endl;
    cout << "ps2 = " << ps2 << endl;

    /* delete ps1;//error */
    /* delete ps2;//error */
    /* ps1->destroy(); */
    /* ps2->destroy(); */
    Singleton::destroy();
    Singleton::destroy();

    return 0;
}
单例对象的自动释放(自动delete单例对象)的4种方式
①atexit( ) + 饿汉模式
②友元类
③内部类+private+static静态数据成员
④pthread_once( )




2. 编译防火墙 Pimpl

Line.h
#ifndef __LINE_H__
#define __LINE_H__

class Line
{
public:
    Line(int x1, int y1, int x2, int y2);
    void printLine();
    ~Line();
    class LinePimpl;//类前向声明
private:
    LinePimpl *_pimpl;
};

#endif
Line.cc
#include "Line.h"
#include 

using std::cout;
using std::endl;

class Line::LinePimpl
{
public:
    LinePimpl(int x1, int y1, int x2, int y2)
    : _pt1(x1, y1)
    , _pt2(x2, y2)
    {
        cout << "LinePimpl(int, int, int, int)" << endl;
    }

    void printLinePimpl()
    {
        _pt1.print();
        cout << "--->";
        _pt2.print();
        cout << endl;
    }

    ~LinePimpl()
    {
        cout << "~LinePimpl()" << endl;
    }

private:
    //内部类,嵌套类
    class Point
    {
    public:
        Point(int ix = 0, int iy = 0)
        : _ix(ix)
        , _iy(iy)
        {
            cout << "Point(int = 0, int = 0)" << endl;
        }
    
        void print()
        {
            cout << "(" << this->_ix
                 << ", " << this->_iy
                 << ")";
        }
    
        ~Point()
        {
            cout << "~Point()" << endl;
        }
    
    private:
        int _ix;
        int _iy;
    };
private:
    Point _pt1;//类对象成员(子对象)
    Point _pt2;
};

Line::Line(int x1, int y1, int x2, int y2)
: _pimpl(new LinePimpl(x1, y1, x2, y2))
{
    cout << "Line(int, int, int, int )" << endl;
}

void Line::printLine()
{
    _pimpl->printLinePimpl();
}

Line::~Line()
{
    cout << "~Line()" << endl;
    if(_pimpl)
    {
        delete _pimpl;
        _pimpl = nullptr;
    }
}
testLine.cc
#include "Line.h"
#include 

using std::cout;
using std::endl;

int main(int argc, char **argv)
{
    Line line(1, 2, 3, 4);
    cout << "line = ";
    line.printLine();
    return 0;
}




3. 代理模式 Proxy




4. 适配器模式

做接口:适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中




5. Reactor pattern(反应器模式)




6. Proactor模式

主动发起异步调用

你可能感兴趣的:(软件工程,计算机体系结构,设计模式)