设计模式读书笔记

策略模式

Strategy模式可以用来替换if else

class TaxStrategy{
public:
    virtual double Calculate(const Context & context) = 0;
    virtual ~TaxStrategy(){}
};
class SalesOrder{
private:
    TaxStrategy* strategy;
public:
    SalesOrder(StrategyFactory* strategyFactory){
        this->strategy = strategyFactory->NewStrategy();
    }//其实这个是工厂模式
    ~SalesOrder(){
        delete this->strategy;
    }
    public double CalculateTax(){
        Context context();
        double val = 
        strategy->Calculate(context);
    }
};

观察者模式

Observer/Event
通知依赖关系
所有的依赖对象都将得到通知ß

class MainForm: public Form, public IProgress{
    TextBox * txtFilePath;
    TextBox* txtFileNumber;
    ProgressBar* progressBar;
public:
    void Button1_Click(){
        string filePath = txtFilePath->getText();
        int number = atoi(txtFileNumber->getText().c_str());
        FileSplitter splitter(filePath, number);
        splitter.split();
    }
    virtual void DoProgress(float value){
        progressBar->setValue(value);
    };
};
class IProgress{
public:
    virtual void DoProgress(float value) = 0;
    virtua ~IProgress(){};
};//实现一个抽象的通知机制
class FileSplitter{
    string m_filePath;
    int m_fileNumber;
    /*
    假设要加入一个通知,通知进度如何
    加入一个抽象通知机制
    */
    IProgress * m_iprogress;
public:
    FileSplitter(const string & filePath, int fileNumber):m_filePath(filePath),m_fileNumber(fileNumber),m_iprogress(iprogress){}
    void split(){
        for(int i = 0 i < m_fileNumber; i ++){
            .....
        if(m_iprogress != nullptr){
            float progressValue = m_fileNumber;
            progressValue = (i + 1) / progressValue;
            m_iprogress->DoProgress(progressValue);
        }
        }
    }
};

工厂模式

Factory Method
对象创建模式
绕开new,来避免对象创建过程中的紧耦合
面向接口的编程
new带来了细节依赖

class MainForm:public Form{
private:
    TextBox * txtFilePath;
    TextBox* txtFileNumber;
    ProgressBar* progressBar;
    splitterFactory * factory;
public:
    MainForm(splitterFactory * factory){
        this->factory = factory;
    }
    void Button1_Click(){
        ISplitter * splitter =
        factory->CreateSplitter();
        splitter->split();
    }
};
class ISplitter{
public:
    virtual void split() = 0;
    ~ISplitter(){};
};
//具体类
class FileSplitter : public ISplitter{
public:
    void split(){

    }
};
class BinarySplitter: public ISplitter{

};
class TxtSplitter : public ISplitter{

};
class videoSplitter: public ISplitter{

};
class SplitterFactory{
public:
    virtual ISplitter * CreateSplitter() = 0;
    virtual ~SplitterFactory(){};
};
//具体工厂
class BinarySplitterFactory: public SplitterFactory{
public:
    virtual ISplitter * CreateSplitter(){
        return new BinarySplitter();
    }
}

抽象工厂

Abstract Factory
经常面对一些相互依赖的对象的创建工作
提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”
无需指定它们具体的类

class IDBConnection{

};
class IDBCommand{
    
};
class IDataReader{

};
class IDBConnectionFactory{//工厂类
public:
    virtual IDBConnection * CreateDBConnection() = 0;
};
class sqlConnectionFacory : public IDBConnectionFactory{//具体的工厂实现
public:
    virtual IDBConnection * CreateDBConnection(){
        return new SqlConnection();
    }
};
class SqlConnection : public IDBConnection{

};
class sqlCommand: public IDBCommand{

};
class IDBCommandFactory{
public:
    virtual IDBCommand * CreateDBCommand() = 0;
};
class sqlCommandFactory : public IDBCommandFactory{
public:
    virtual IDBCommand * CreateDBCommand(){
        return new sqlCommand();
    }
};
class sqlReader : public IDataReader{

};
class IDBDataReaderFactory{
public:
    virtual IDataReader * CreateDBReader()=0;
};
class sqlDataReaderFactory: public IDBDataReaderFactory{
public:
    virtual IDataReader * CreateDBReader(){
        return new sqlReader();
    }
};
/*
省去了对应的mysql,oracle
*/
class EmployeeDAO{
    IDBConnectionFactory * dbConnectionFactory;
    IDBCommandFactory * dbCommandFactory;
    IDBDataReaderFactory * dbReaderFactory;
    //这几个对应的参数应该是同一类
public:
    vector<EmployeeDO> GetEmployees(){
        IDBCConnection * connection = 
        dbConnectionFactory->CreateDBConnection();
        connection->ConnectionString = "...";
        IDBCommand * command = 
        dbCommandFactory->CreateCreateCommand();

        /*
        其他的也一样是对应的实现
        */
    }
};
//**********************************分割
/*
将三个合在一起
    IDBConnectionFactory * dbConnectionFactory;
    IDBCommandFactory * dbCommandFactory;
    IDBDataReaderFactory * dbReaderFactory;
合成一个工厂
    IDBFactory 
*/

Singleton 单例模式

“对象性能”模式
面向对象很好的解决了“抽象”的问题,但是必不可免地要付出一定的代价。
在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例
才能确保它们的逻辑正确性,以及良好的效率

class Singleton{
private:
    Singleton();
    Singleton(const Singleton & other);
public:
    static Singleton * getInstance();
    static Singleton * m_instance;
};
Singleton * Singleton::m_instance=nullptr;
//线程非安全版本 单线程可行
Singleton * Singleton::getInstance(){
    /*
    假设存在两个线程 prthead_A, prthead_B
    A走到new这一步 , 时间片轮转到B这一步
    然后就并发了
    */
    if(m_instance == nullptr){
        m_instance = new Singleton();
    }
    return m_instance;
}
//同步的懒汉模式 线程安全版本 互斥锁
Singleton * Singletion::getInstance(){
    Lock lock;
    if(m_instance == nullptr){
        m_instance = new Singleton();
    }
    return m_instance;
}
//线程安全版本,但由于内存读写reorder不安全
/*
在cpu指令层次 new三次会被reorder
乱序处理
就是不按照 我们想象的三步来处理
双重检查懒汉模式
*/
Singleton * Singletion::getInstance(){
    if(m_instance == nullptr){
        Lock lock;
        if(m_instance == nullptr)
            m_instance = new Singleton();
    }
    return m_instance;
}
//C++ 11版本之后的跨平台实现(bolatile)
std::atomic<Singleton*> Singleton::m_instance;
std::mutex Singleton::m_mutex;
Singleton * Singleton::getInstance(){
    Singleton * tmp = m_instance.load(std::memory_order_relaxed);
    std::atomic_thread_fence(std::memory_order_acquire);
    if(tmp == nullptr){
        st::lock_guard<std::mutex> lock(m_mutex);
        tmp = m_instance.load(std::memory_order_relaxed);
        if(tmp == nullptr){
            tmp = new Singletion;
            std::atomic_thread_fence(std::memory_order_release);
            m_instance.store(tmp, std::memory_order_relaxed);
        }
    }
    return tmp;ge
}

class Singleton1{//饿汉模式
private:
    Singleton1();
    Singleton1(const Singleton & other);
    static Singleton1 * m_instance = new Singleton1();
public:
    static Singleton1 * getInstance();
};
static Sigleton1 Sigleton1::getInstance(){
    return m_instance;
}
posted @ 2020-04-09 16:31  moxin0509  阅读(164)  评论(0编辑  收藏  举报