最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++17實現(xiàn)flyweight_factory模板類及使用示例詳解

 更新時間:2023年08月10日 10:25:57   作者:hedzr  
這篇文章主要為大家介紹了C++17實現(xiàn)flyweight_factory模板類及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

FlyWeight Pattern

回顧享元模式,考慮實作它的各種問題。

理論

享元模式,是將復雜對象的相同的組成元素抽出并單獨維護的一種結構型設計模式。這些相同的組成元素被稱為共享元件,它們在一個單獨的容器中被唯一性地管理,而復雜對象只需持有到該唯一實例的參考,而無需重復創(chuàng)建這樣的相同的元素,從而能夠大幅度地削減內存占用。

以字處理器為例,每個字符都具有獨立的、區(qū)別于其它字符的特殊屬性:例如字體樣式,背景、邊框、對齊等等。如果一個文檔中全部字符都單獨存儲一份它的所有屬性的副本,那么這將會是龐大的內存需求。但考慮到一大堆(例如1000個)字符可能都有相同的“宋體,9pt”這樣的屬性,那么實際上我們只需要單獨存儲一份“宋體,9pt”的字體樣式屬性,而一個字符只需要一個指向該字體樣式屬性的指針就可以了,這就比1000個字符的1000個字體樣式屬性拷貝要節(jié)約的多。

類似的案例還有相當多,例如例子系統(tǒng)中的每個粒子(例如子彈、彈片,或者敵方飛機)都有一些相同的屬性(例如顏色,輪廓等等)占地不小,但值卻相同。

工廠模式

很容易想到,我們可以在一個工廠中就地管理享元對象。當客戶以具體值來請求一個享元對象時,工廠會從一個字典中檢索享元是否存在,然后返回該元素的參考引用給客戶。如果享元尚未存在,那么工廠會創(chuàng)建它,然后在返回引用。

不可變性

按照傳統(tǒng)的說法,享元模式要求這些相同的部分(享元,相同的組成元素)是不可變的。但這并不是鐵律。

一個方法是,以一個享元為整體,我們可以整體修改對象持有的享元參考。

例如我們正在修改字處理器中的一個單詞的字體樣式,從“宋體,9pt”改為“黑體,12pt”,那么我們可以直接修改引用指向。也就是說,我們提供 character.apply_font_style(font_style& style) 這樣的整體修改接口。

另一個方法可以從更細的粒度出發(fā)進行修改,例如從“宋體,9pt”改為“宋體,10pt”,但在發(fā)生變更時,嘗試從工廠中查證新值的參考。也就是說,我們提供 character.set_font_size(float pt) 這樣的接口,但在其實現(xiàn)過程中記得去查證享元工廠(管理器)以求更新內部引用。

C++ 實現(xiàn)

傳統(tǒng)的享元模式的實現(xiàn)方式有這樣的示例代碼:

namespace hicc::dp::flyweight::basic {
  /**
     * flyweight Design Pattern
     *
     * Intent: Lets you fit more objects into the available amount of RAM by sharing
     * common parts of state between multiple objects, instead of keeping all of the
     * data in each object.
     */
  struct shared_state {
    std::string brand_;
    std::string model_;
    std::string color_;
    shared_state(const std::string &brand, const std::string &model, const std::string &color)
      : brand_(brand)
        , model_(model)
        , color_(color) {
      }
    friend std::ostream &operator<<(std::ostream &os, const shared_state &ss) {
      return os << "[ " << ss.brand_ << " , " << ss.model_ << " , " << ss.color_ << " ]";
    }
  };
  struct unique_state {
    std::string owner_;
    std::string plates_;
    unique_state(const std::string &owner, const std::string &plates)
      : owner_(owner)
        , plates_(plates) {
      }
    friend std::ostream &operator<<(std::ostream &os, const unique_state &us) {
      return os << "[ " << us.owner_ << " , " << us.plates_ << " ]";
    }
  };
  /**
     * The flyweight stores a common portion of the state (also called intrinsic
     * state) that belongs to multiple real business entities. The flyweight accepts
     * the rest of the state (extrinsic state, unique for each entity) via its
     * method parameters.
     */
  class flyweight {
    private:
    shared_state *shared_state_;
    public:
    flyweight(const shared_state *o)
      : shared_state_(new struct shared_state(*o)) {
      }
    flyweight(const flyweight &o)
      : shared_state_(new struct shared_state(*o.shared_state_)) {
      }
    ~flyweight() { delete shared_state_; }
    shared_state *state() const { return shared_state_; }
    void Operation(const unique_state &unique_state) const {
      std::cout << "flyweight: Displaying shared (" << *shared_state_ << ") and unique (" << unique_state << ") state.\n";
    }
  };
  /**
     * The flyweight Factory creates and manages the flyweight objects. It ensures
     * that flyweights are shared correctly. When the client requests a flyweight,
     * the factory either returns an existing instance or creates a new one, if it
     * doesn't exist yet.
     */
  class flyweight_factory {
    std::unordered_map<std::string, flyweight> flyweights_;
    std::string key(const shared_state &ss) const {
      return ss.brand_ + "_" + ss.model_ + "_" + ss.color_;
    }
    public:
    flyweight_factory(std::initializer_list<shared_state> lists) {
      for (const shared_state &ss : lists) {
        this->flyweights_.insert(std::make_pair<std::string, flyweight>(this->key(ss), flyweight(&ss)));
      }
    }
    /**
     * Returns an existing flyweight with a given state or creates a new one.
     */
    flyweight get(const shared_state &shared_state) {
      std::string key = this->key(shared_state);
      if (this->flyweights_.find(key) == this->flyweights_.end()) {
        std::cout << "flyweight_factory: Can't find a flyweight, creating new one.\n";
        this->flyweights_.insert(std::make_pair(key, flyweight(&shared_state)));
      } else {
        std::cout << "flyweight_factory: Reusing existing flyweight.\n";
      }
      return this->flyweights_.at(key);
    }
    void list() const {
      size_t count = this->flyweights_.size();
      std::cout << "\nflyweight_factory: I have " << count << " flyweights:\n";
      for (std::pair<std::string, flyweight> pair : this->flyweights_) {
        std::cout << pair.first << "\n";
      }
    }
  };
  // ...
  void AddCarToPoliceDatabase(
    flyweight_factory &ff,
    const std::string &plates, const std::string &owner,
    const std::string &brand, const std::string &model, const std::string &color) {
    std::cout << "\nClient: Adding a car to database.\n";
    const flyweight &flyweight = ff.get({brand, model, color});
    // The client code either stores or calculates extrinsic state and passes it
    // to the flyweight's methods.
    flyweight.Operation({owner, plates});
  }
} // namespace hicc::dp::flyweight::basic
void test_flyweight_basic() {
  using namespace hicc::dp::flyweight::basic;
  flyweight_factory *factory = new flyweight_factory({ {"Chevrolet", "Camaro2018", "pink"}, {"Mercedes Benz", "C300", "black"}, {"Mercedes Benz", "C500", "red"}, {"BMW", "M5", "red"}, {"BMW", "X6", "white"} });
  factory->list();
  AddCarToPoliceDatabase(*factory,
                         "CL234IR",
                         "James Doe",
                         "BMW",
                         "M5",
                         "red");
  AddCarToPoliceDatabase(*factory,
                         "CL234IR",
                         "James Doe",
                         "BMW",
                         "X1",
                         "red");
  factory->list();
  delete factory;
}

其輸出結果如同這樣:

--- BEGIN OF test_flyweight_basic                     ----------------------

flyweight_factory: I have 5 flyweights:
BMW_X6_white
Mercedes Benz_C500_red
Mercedes Benz_C300_black
BMW_M5_red
Chevrolet_Camaro2018_pink

Client: Adding a car to database.
flyweight_factory: Reusing existing flyweight.
flyweight: Displaying shared ([ BMW , M5 , red ]) and unique ([ James Doe , CL234IR ]) state.

Client: Adding a car to database.
flyweight_factory: Can't find a flyweight, creating new one.
flyweight: Displaying shared ([ BMW , X1 , red ]) and unique ([ James Doe , CL234IR ]) state.

flyweight_factory: I have 6 flyweights:
BMW_X1_red
Mercedes Benz_C300_black
BMW_X6_white
Mercedes Benz_C500_red
BMW_M5_red
Chevrolet_Camaro2018_pink
--- END OF test_flyweight_basic                       ----------------------

可以看到,像 [ BMW , X1 , red ] 這樣的一個享元,單個實例較大(數十、數百乃至數十K 字節(jié)),而引用參考不過是一個指針的大?。ㄍǔJ?64 bytes on 64-bit OS),那么最終節(jié)省的內存是非??捎^的。

元編程中的 FlyWeight Pattern

上面的示例,已經是舊風格了,C++11 以后我們需要大量地使用智能指針、以及模板語法,而在 C++17 之后更好的原位構造能力允許我們的代碼能夠更加 meaningful。

flyweight_factory

一個想法是,我們認為一個盡可能通用的享元工廠可能是有利于代碼書寫的。所以我們嘗試這樣一個享元工廠模板:

namespace hicc::dp::flyweight::meta {
  template<typename shared_t = shared_state_impl, typename unique_t = unique_state_impl>
  class flyweight {
    std::shared_ptr<shared_t> shared_state_;
    public:
    flyweight(flyweight const &o)
      : shared_state_(std::move(o.shared_state_)) {
      }
    flyweight(shared_t const &o)
      : shared_state_(std::make_shared<shared_t>(o)) {
      }
    ~flyweight() {}
    auto state() const { return shared_state_; }
    auto &state() { return shared_state_; }
    void Operation(const unique_t &unique_state) const {
      std::cout << "flyweight: Displaying shared (" << *shared_state_ << ") and unique (" << unique_state << ") state.\n";
    }
    friend std::ostream &operator<<(std::ostream &os, const flyweight &o) {
      return os << *o.shared_state_;
    }
  };
  template<typename shared_t = shared_state_impl,
                   typename unique_t = unique_state_impl,
                   typename flyweight_t = flyweight<shared_t, unique_t>,
                   typename hasher_t = std::hash<shared_t>>
  class flyweight_factory {
    public:
    flyweight_factory() {}
    explicit flyweight_factory(std::initializer_list<shared_t> args) {
      for (auto const &ss : args) {
        flyweights_.emplace(_hasher(ss), flyweight_t(ss));
      }
    }
    flyweight_t get(shared_t const &shared_state) {
      auto key = _hasher(shared_state);
      if (this->flyweights_.find(key) == this->flyweights_.end()) {
        std::cout << "flyweight_factory: Can't find a flyweight, creating new one.\n";
        this->flyweights_.emplace(key, flyweight_t(shared_state));
      } else {
        std::cout << "flyweight_factory: Reusing existing flyweight.\n";
      }
      return this->flyweights_.at(key);
    }
    void list() const {
      size_t count = this->flyweights_.size();
      std::cout << "\nflyweight_factory: I have " << count << " flyweights:\n";
      for (auto const &pair : this->flyweights_) {
        std::cout << pair.first << " => " << pair.second << "\n";
      }
    }
    private:
    std::unordered_map<std::size_t, flyweight_t> flyweights_;
    hasher_t _hasher{};
  };
} // namespace hicc::dp::flyweight::meta

然后我們就可以以派生類的方式直接使用這個享元工廠了:

class vehicle : public flyweight_factory<shared_state_impl, unique_state_impl> {
  public:
  using flyweight_factory<shared_state_impl, unique_state_impl>::flyweight_factory;
  void AddCarToPoliceDatabase(
    const std::string &plates, const std::string &owner,
    const std::string &brand, const std::string &model, const std::string &color) {
    std::cout << "\nClient: Adding a car to database.\n";
    auto const &flyweight = this->get({brand, model, color});
    flyweight.Operation({owner, plates});
  }
};

其中 using flyweight_factory<shared_state_impl, unique_state_impl>::flyweight_factory; 是 C++17 以后的新語法,它將父類的所有構造函數原樣復制給派生類,從而讓你不必拷貝粘貼代碼然后修改類名。

在 vehicle 模板類中我們使用默認的 flyweight<shared_t, unique_t>,但你可以在 flyweight_factory 的模板參數中修改它以便提供你自己的享元類具體實現(xiàn)。

測試代碼

void test_flyweight_meta() {
    using namespace hicc::dp::flyweight::meta;
    auto factory = std::make_unique<vehicle>(
            std::initializer_list<shared_state_impl>{
                    {"Chevrolet", "Camaro2018", "pink"},
                    {"Mercedes Benz", "C300", "black"},
                    {"Mercedes Benz", "C500", "red"},
                    {"BMW", "M5", "red"},
                    {"BMW", "X6", "white"}});
    factory->list();
    factory->AddCarToPoliceDatabase("CL234IR",
                                    "James Doe",
                                    "BMW",
                                    "M5",
                                    "red");
    factory->AddCarToPoliceDatabase("CL234IR",
                                    "James Doe",
                                    "BMW",
                                    "X1",
                                    "red");
    factory->list();
}

附加

我們使用了稍稍不同的基礎類 shared_state_impl 以及 unique_state_impl

namespace hicc::dp::flyweight::meta {
  struct shared_state_impl {
    std::string brand_;
    std::string model_;
    std::string color_;
    shared_state_impl(const std::string &brand, const std::string &model, const std::string &color)
      : brand_(brand)
        , model_(model)
        , color_(color) {
      }
    shared_state_impl(shared_state_impl const &o)
      : brand_(o.brand_)
        , model_(o.model_)
        , color_(o.color_) {
      }
    friend std::ostream &operator<<(std::ostream &os, const shared_state_impl &ss) {
      return os << "[ " << ss.brand_ << " , " << ss.model_ << " , " << ss.color_ << " ]";
    }
  };
  struct unique_state_impl {
    std::string owner_;
    std::string plates_;
    unique_state_impl(const std::string &owner, const std::string &plates)
      : owner_(owner)
        , plates_(plates) {
      }
    friend std::ostream &operator<<(std::ostream &os, const unique_state_impl &us) {
      return os << "[ " << us.owner_ << " , " << us.plates_ << " ]";
    }
  };
} // namespace hicc::dp::flyweight::meta
namespace std {
  template<>
  struct hash<hicc::dp::flyweight::meta::shared_state_impl> {
    typedef hicc::dp::flyweight::meta::shared_state_impl argument_type;
    typedef std::size_t result_type;
    result_type operator()(argument_type const &s) const {
      result_type h1(std::hash<std::string>{}(s.brand_));
      hash_combine(h1, s.model_, s.color_);
      return h1;
    }
  };
} // namespace std

這是因為我們在 flyweight_factory 中使用了 std::hash 技術來管理一個享元的鍵值,所以我們必須明確地實現(xiàn) shared_state_impl 的 std::hash 特化版本。

而在這個特化版本中我們又利用了一個特別的 hash_combine 函數。

hash_combine

這是一個技術性很強的概念,因為它涉及到了一個神奇的幻數(magicnum)0x9e3779b9。

我們在 hicc-cxx/cmdr-cxx 中提供了一個源于 boost::hash_combine 的同名擴展:

namespace std {
  template<typename T, typename... Rest>
  inline void hash_combine(std::size_t &seed, T const &t, Rest &&...rest) {
    std::hash<T> hasher;
    seed ^= 0x9e3779b9 + (seed << 6) + (seed >> 2) + hasher(t);
    int i[] = {0, (hash_combine(seed, std::forward<Rest>(rest)), 0)...};
    (void) (i);
  }
  template<typename T>
  inline void hash_combine(std::size_t &seed, T const &v) {
    std::hash<T> hasher;
    seed ^= 0x9e3779b9 + (seed << 6) + (seed >> 2) + hasher(v);
  }
} // namespace std

它的作用在于計算一系列的對象的 hash 值并組合它們。

關于如何正確地組合一堆 hash 值,較為簡單地方法是:

std::size_t h1 = std::hash<std::string>("hello");
std::size_t h2 = std::hash<std::string>("world");
std::size_t h = h1 | (h2 << 1);

但仍然有更多的探討,其中得到了公認的最佳手段(C++中)是源自于 boost::hash_combine 實現(xiàn)代碼的一個方法:

seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);

就目前已知的學術研究中,這是最佳的。

那么誰制造了這么奇幻的一個神經質數(golden ratio)呢,大體可考的原作應該是: A Hash Function for Hash Table Lookup 或 Hash Functions for Hash Table Lookup 。原作者 Bob Jenkins,原發(fā)于 DDJ 刊物 1997 年,代碼大約是成形于 1996 年。而這個數嘛,源于這個表達式:

$\frac{2^{32}}{\frac{1+\sqrt{5}}{2}}$ (

Epilogue

好,雖然不算太盡人意,但我確實實現(xiàn)了一個 C++17 的勉強比較通用的 flyweight_factory 模板類,就將就了吧。

以上就是C++17實現(xiàn)flyweight_factory模板類及使用示例詳解的詳細內容,更多關于C++17 flyweight_factory模板類的資料請關注腳本之家其它相關文章!

相關文章

  • sublime text3搭建配置c語言編譯環(huán)境的詳細圖解教程(小白級)

    sublime text3搭建配置c語言編譯環(huán)境的詳細圖解教程(小白級)

    這篇文章主要介紹了sublime text3搭建配置c語言編譯環(huán)境,詳細圖解,小白教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01
  • C++利用棧實現(xiàn)中綴表達式轉后綴表達式

    C++利用棧實現(xiàn)中綴表達式轉后綴表達式

    這篇文章主要為大家詳細介紹了C++利用棧實現(xiàn)中綴表達式轉后綴表達式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 一文掌握C++ 智能指針全部用法

    一文掌握C++ 智能指針全部用法

    學習智能指針有很多好處,可以幫我們C++程序員管理動態(tài)分配的內存的,它會幫助我們自動釋放new出來的內存,從而避免內存泄漏,感興趣的朋友跟隨小編一起看看吧
    2021-08-08
  • Qt?QTableWidget基本操作及使用

    Qt?QTableWidget基本操作及使用

    QTableWidget?是?Qt?中的表格組件類。很類似于VC、C#中的DataGrid,本文就詳細來介紹一下Qt?QTableWidget基本操作及使用,感興趣的可以了解一下
    2021-11-11
  • 面試常見問題之C語言與C++的區(qū)別問題

    面試常見問題之C語言與C++的區(qū)別問題

    在C中,用static修飾的變量或函數,主要用來說明這個變量或函數只能在本文件代碼塊中訪問,而文件外部的代碼無權訪問,今天重點給大家介紹面試中常見的C語言與C++區(qū)別的問題,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • 最新C/C++中的new和delete的實現(xiàn)過程小結

    最新C/C++中的new和delete的實現(xiàn)過程小結

    這篇文章主要介紹了C/C++中的new和delete的實現(xiàn)過程,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C++菱形繼承和虛繼承的實現(xiàn)

    C++菱形繼承和虛繼承的實現(xiàn)

    本文主要介紹了C++菱形繼承和虛繼承的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • C語言字符函數與字符串函數的實現(xiàn)示例

    C語言字符函數與字符串函數的實現(xiàn)示例

    C語言標準庫中的<ctype.h>和<string.h>頭文件分別提供了豐富的字符處理和字符串處理函數,本文就來介紹一下C語言字符函數與字符串函數的實現(xiàn)示例,感興趣的可以了解一下
    2024-11-11
  • C語言中的運算符和結合性問題

    C語言中的運算符和結合性問題

    這篇文章主要介紹了C語言中的運算符和結合性問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • C++ template用法案例詳解

    C++ template用法案例詳解

    這篇文章主要介紹了C++ template用法案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-09-09

最新評論

酒泉市| 莆田市| 锦州市| 潮安县| 西昌市| 泸州市| 五华县| 都匀市| 巴南区| 雅江县| 东光县| 和田市| 大荔县| 株洲市| 宁波市| 若羌县| 泰兴市| 乌拉特前旗| 泊头市| 商河县| 宜都市| 商丘市| 黄大仙区| 华容县| 大连市| 仙居县| 中牟县| 屏东县| 宁海县| 永年县| 五峰| 方正县| 崇阳县| 芜湖市| 阜南县| 二连浩特市| 大石桥市| 厦门市| 安吉县| 武强县| 顺平县|