单例模式(c++实现)

单例模式

单例模式使我们使用非常多的模式,也是很简单的一个设计模式。

模式原理

单例模式通过私有化类的构造函数来避免外部创建该类的实例,仅仅提供一个静态的getInstace()方法来获取在类内部创建的一个全局唯一的实例,同时在该方法种创建唯一实例,还要保证创建过程是线程安全的。

使用场景

单例模式的使用场景可以从他的特性来分析,比如:

  1. windows的画图工具,画笔是单例,但是画布是我们可以随意创建的。我们可以改变画笔的颜色和粗细,但是我们用来画出图案的画笔实例永远是同一个;
  2. 也可以把一些通用的方法放到一个commonn类里面定义成static的方法,这样我们就可以通过common的单例来调用这些方法,当然简单的使用全局函数也是可以滴~。这里的单例就更多的是贡献了它的封装特性;

代码实现

draw.h

#include <string>
#include "singleton.h"

class Draw
{
public:
    void show();
    Draw(std::string name);
    void setPan();

private:
    std::string m_Name;
};

draw.cpp

#include <iostream>
#include "draw.h"

Draw::Draw(std::string name)
:m_Name(name)
{

}

void Draw::show()
{
    std::cout << m_Name << ":" << std::endl;

    for(int n = 0; n < 20; ++n)
    {
        for(int m = 0; m < n; ++m)
        {
//            std::cout << "*";
            std::cout << Pen::getInstance()->draw();
        }
        std::cout << std::endl;
    }
}

properties.h

#include <string>

struct ProPerties
{
    std::string     p_Color;
    uint32_t        p_Width;
    std::string     p_Shape;
};

singleton.h

#include "properties.h"

class Pen
{
public:
    static Pen* getInstance();

    void setProperty(const ProPerties& proty);
    std::string draw();
private:
    Pen();

    ProPerties  m_Property;
};

singleton.cpp

#include "singleton.h"

Pen *Pen::getInstance()
{
    //线程安全
    static Pen sig;
    return &sig;
}

void Pen::setProperty(const ProPerties &proty)
{
    m_Property = proty;
}

std::string Pen::draw()
{
    return m_Property.p_Shape;
}

Pen::Pen()
{
}

main.cpp

#include <iostream>
#include <thread>
#include "singleton.h"
#include "properties.h"
#include "draw.h"
using namespace std;

int main()
{
    Pen* sig = Pen::getInstance();

    ProPerties proty;
    proty.p_Color = "red";
    proty.p_Width = 65;
    proty.p_Shape = '*';
    sig->setProperty(proty);

    std::thread t1([](){
        Draw d1("Draw1");
        d1.show();
    });

    std::thread t2([](){
        Draw d2("Draw2");
        d2.show();
    });


    std::thread t3([](){
        Draw d3("Draw3");
        d3.show();
    });

    t1.join();
    t2.join();
    t3.join();
    return 0;
}