C++工程,C++設計模式-原型模式,原型模式和拷貝構造函數的區別

c++常用設計模式
設計模式之原型模式
原型模式和建造者模式、工廠方法模式一樣,都屬於創建型模式的一種。簡單的來說,我們使用原型模式,就是爲了創建對象。不過,適合原型模式的最好選擇如下:

1.當我們的對象類型不是開始就能確定的,而這個類型是在運行期確定的話,那麼我們通過這個類型的對象克隆出一個新的對象比較容易一些;

2.有的時候,我們需要一個對象在某個狀態下的副本,此時,我們使用原型模式是最好的選擇;例如:一個對象,經過一段處理之後,其內部的狀態發生了變化;這個時候,我們需要一個這個狀態的副本,如果直接new一個新的對象的話,但是它的狀態是不對的,此時,可以使用原型模式,將原來的對象拷貝一個出來,這個對象就和之前的對象是完全一致的了;

3.當我們處理一些比較簡單的對象時,並且對象之間的區別很小,可能就幾個屬性不同而已,那麼就可以使用原型模式來完成,省去了創建對象時的麻煩了;

4.有的時候,創建對象時,構造函數的參數很多,而自己又不完全的知道每個參數的意義,就可以使用原型模式來創建一個新的對象,不必去理會創建的過程。

->適當的時候考慮一下原型模式,能減少對應的工作量,減少程序的複雜度,提高效率。

1,運行效果

在這裏插入圖片描述

2,代碼示例

#include <iostream>
#include <thread>
#include <mutex>
#include <unistd.h>
#include <list>

enum class ThreadType : int {
    PRODUCTION = 1,
    CONSUMPTION = 2,
};
// the output operator overloading of the enum class ThreadType
std::ostream &operator<<(std::ostream &os, const ThreadType &obj) {
    switch(static_cast<int>(obj)) {
        case static_cast<int>(ThreadType::PRODUCTION) :
            os << "PRODUCTION "; break;
        case static_cast<int>(ThreadType::CONSUMPTION) :
            os << "CONSUMPTION "; break;
    }
    return os;
}
//provides an abstract base class Clone.
class Prototype {
    public:
        virtual Prototype *clone() = 0;
};

class Thread : public Prototype {
    private:
        ThreadType m_type;
        bool m_run_flag = true;
        std::list<int> m_data;
        std::thread m_thread;
    private:
        int produce_data() {
            int count = 0;
            while(m_run_flag) {
                m_data.push_back(count);
                std::cout << "count:" << count << "  list_size:" << m_data.size() <<std::endl;
                count++;
                sleep(1);
            }
        };
        int consume_data() {
            while(m_run_flag) {
                if(!m_data.empty()) {
                    int data = m_data.front();
                    m_data.pop_front();
                    std::cout << "data:" << data << "  list_size:" << m_data.size() <<std::endl;
                    sleep(1);
                }
            }
        };
        int run() {
            std::cout << "thread_type:" << m_type << std::endl;
            switch(static_cast<int>(m_type)) {
                case static_cast<int>(ThreadType::PRODUCTION) :
                    produce_data(); break;
                case static_cast<int>(ThreadType::CONSUMPTION) :
                    consume_data(); break;
                default :
                    std::cerr << "error thread type:" << m_type << std::endl;
            }
        };
    public:
        Thread(ThreadType type):m_type(type) {std::cout << "thread constructor" << std::endl;};
        ~Thread() {m_run_flag = false;};
        //the key code.clone need to call the copy constructor.
        Thread(const Thread& obj) {
            this->m_type = obj.m_type;
            this->m_run_flag = obj.m_run_flag;
            this->m_data = obj.m_data;
            std::cout << "thread copy constructor" << std::endl;
        };
        //the key code. return a copy of the object.
        Prototype* clone() {return new Thread(*this);}
        void setType(ThreadType type) {m_type = type;};
        int startThread() {
            m_run_flag = true;
            m_thread = std::thread(&Thread::run, this);
            m_thread.detach();
            return 0;
        };
};

int main() {
    Thread *thread_1 = new Thread(ThreadType::PRODUCTION);

    thread_1->startThread();
    sleep(5);

    //Prototype mode
    Thread *thread_2 = (Thread *)thread_1->clone();
    thread_2->setType(ThreadType::CONSUMPTION);
    thread_2->startThread();
    sleep(5);

    // copy constructor
    Thread *thread_3 = new Thread(*thread_1);
    thread_3->setType(ThreadType::CONSUMPTION);
    thread_3->startThread();

    int count = 5;
    while(count--){sleep(1);};
    delete thread_1;
    delete thread_2;
    return 0;
}

3,原型模式和拷貝構造函數的區別

相同點:原型模式和拷貝構造函數都是要產生對象的複製品。

不同點:原型模式實現的是一個clone接口,注意是接口,也就是基於多態的clone虛函數。也就是說原型模式能夠通過基類指針來複制派生類對象。拷貝構造函數完不成這樣的任務。

           原型模式的核心是克隆,構造函數只是克隆的一個辦法而已。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章