国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當前位置: 首頁 > news >正文

專門做飲食加盟的網(wǎng)站產(chǎn)品營銷方案

專門做飲食加盟的網(wǎng)站,產(chǎn)品營銷方案,眼科醫(yī)院網(wǎng)站建設方案,小米應用商店安裝下載設計模式 文章目錄 設計模式創(chuàng)建型模式單例模式 [1-小明的購物車](https://kamacoder.com/problempage.php?pid1074)工廠模式 [2-積木工廠](https://kamacoder.com/problempage.php?pid1076)抽象??模式 [3-家具工廠](https://kamacoder.com/problempage.php?pid1077)建造者…

設計模式

文章目錄

  • 設計模式
    • 創(chuàng)建型模式
      • 單例模式 [1-小明的購物車](https://kamacoder.com/problempage.php?pid=1074)
      • 工廠模式 [2-積木工廠](https://kamacoder.com/problempage.php?pid=1076)
      • 抽象??模式 [3-家具工廠](https://kamacoder.com/problempage.php?pid=1077)
      • 建造者模式 [4-???加?](https://kamacoder.com/problempage.php?pid=1077)
      • 原型模式 [5-矩形原型](https://kamacoder.com/problempage.php?pid=1083)

創(chuàng)建型模式

單例模式 1-小明的購物車

單例模式是?種創(chuàng)建型設計模式, 它的核?思想是保證?個類只有?個實例,并提供?個全局訪問點來訪問這個實
例。

  • 只有?個實例的意思是,在整個應?程序中(進程),只存在該類的?個實例對象,?不是創(chuàng)建多個相同類型的對象。
  • 全局訪問點的意思是,為了讓其他類能夠獲取到這個唯?實例,該類提供了?個全局訪問點(通常是?個靜態(tài) ?法),通過這個?法就能獲得實例
#include <iostream>
#include <unordered_map>
#include <vector>
#include<mutex>
#include<atomic>class ShoppingCartManager {
private:std::unordered_map<std::string, int> cart;std::vector<std::string> order; // 保持順序// 私有構(gòu)造函數(shù)ShoppingCartManager() {}static std::mutex m_mutex;//static ShoppingCartManager* m_cart;static std::atomic<ShoppingCartManager*> m_atomic; //使用原子變量將對象指針保存public:// 獲取購物車實例static ShoppingCartManager* getInstance() {ShoppingCartManager* m_cart = m_atomic.load();if (m_cart == nullptr) {m_mutex.lock();m_cart = m_atomic.load();if (m_cart == nullptr) {m_cart = new ShoppingCartManager;m_atomic.store(m_cart); //保存對象指針}m_mutex.unlock();}return m_cart;}// 添加商品void addToCart(const std::string& itemName, int quantity) {if (cart.find(itemName) == cart.end()) { //如果cart里面沒有該貨物order.push_back(itemName); //注意這里是order,為了使得貨物和數(shù)量能對齊}cart[itemName] += quantity;}// 查看商品void viewCart() const {for (const auto& itemName : order) { //這里按照順序讀取貨物,保證有序輸出;若只用unordered_map,無法保證有序std::cout << itemName << " " << cart.at(itemName) << std::endl;}}
};//ShoppingCartManager* ShoppingCartManager::m_cart = nullptr;
std::atomic<ShoppingCartManager*> ShoppingCartManager::m_atomic;
std::mutex ShoppingCartManager::m_mutex;int main() {std::string itemName;int quantity;ShoppingCartManager* cart = ShoppingCartManager::getInstance();while (std::cin >> itemName >> quantity) {cart->addToCart(itemName, quantity);}cart->viewCart();return 0;
}

使用靜態(tài)局部變量保證多線程下的資源安全

工廠模式 2-積木工廠

???法模式也是?種創(chuàng)建型設計模式,簡單??模式只有?個??類,負責創(chuàng)建所有產(chǎn)品,如果要添加新的產(chǎn)品,通常需要修改??類的代碼。????法模式引?了抽象??和具體??的概念,每個具體??只負責創(chuàng)建?個具體產(chǎn)品,添加新的產(chǎn)品只需要添加新的??類??需修改原來的代碼,這樣就使得產(chǎn)品的?產(chǎn)更加靈活,?持擴展,符合開閉原則。
???法模式分為以下?個??:
抽象??:?個接?,包含?個

  • 抽象的???法(?于創(chuàng)建產(chǎn)品對象)。
  • 具體??:實現(xiàn)抽象??接?,創(chuàng)建具體的產(chǎn)品。
  • 抽象產(chǎn)品:定義產(chǎn)品的接?。
  • 具體產(chǎn)品:實現(xiàn)抽象產(chǎn)品接?,是??創(chuàng)建的對象。
// 表驅(qū)動 + lambda 表達式#include<iostream>
#include<bits/stdc++.h>using namespace std;class Shape {
public:virtual void show() = 0;
};class Circle : public Shape {
public:void show() {cout << "Circle Block" << endl;}
};class Square : public Shape {
public:void show () {cout << "Square Block" << endl;}
};class ShapeFactory {
public:virtual Shape* produce() = 0;
};class CircleFactory : public ShapeFactory {
public:Shape* produce() {return new Circle();}
};class SquareFactory : public ShapeFactory {
public:Shape* produce() {return new Square();}
};unordered_map<string, function<ShapeFactory*()>> m {{"Circle", [](){ return new CircleFactory();}},{"Square", [](){ return new SquareFactory();}},
};class BlocksFactory {
public:vector<Shape*> blocks;void CreateBlocks(string& type, int count) {ShapeFactory* sf = m[type]();Shape* s = sf -> produce();for (int i = 0; i < count; i++) {blocks.push_back(s);}}void Print() {for (const auto& s : blocks) {s -> show();}}
};int main() {int produceTime;cin >> produceTime;string type;int count;BlocksFactory* bf = new BlocksFactory();for (int i = 0; i < produceTime; i++) {cin >> type >> count;bf->CreateBlocks(type, count);}bf->Print();return 0;
}

抽象??模式 3-家具工廠

抽象??模式包含多個抽象產(chǎn)品接?,多個具體產(chǎn)品類,?個抽象??接?和多個具體??,每個具體??負責創(chuàng)建?組相關(guān)的產(chǎn)品。

  • 抽象產(chǎn)品接? AbstractProduct : 定義產(chǎn)品的接?,可以定義多個抽象產(chǎn)品接?,?如說沙發(fā)、椅?、茶?都是抽象產(chǎn)品。
  • 具體產(chǎn)品類 ConcreteProduct : 實現(xiàn)抽象產(chǎn)品接?,產(chǎn)品的具體實現(xiàn),古典?格和沙發(fā)和現(xiàn)代?格的沙發(fā)都是具體產(chǎn)品。
  • 抽象??接?AbstractFactory : 聲明?組?于創(chuàng)建產(chǎn)品的?法,每個?法對應?個產(chǎn)品。
  • 具體??類 ConcreteFactory : 實現(xiàn)抽象??接?,,負責創(chuàng)建?組具體產(chǎn)品的對象,在本例中,?產(chǎn)古典?格的??和?產(chǎn)現(xiàn)代?格的??都是具體實例.
#include <iostream>
#include <string>
#include <unordered_map>
#include <functional>using namespace std;class Sofa {
public:virtual void info() = 0;
};class ModernSofa : public Sofa {
public:void info() override {cout << "modern sofa" << endl;};
};class ClassicalSofa : public Sofa {
public:void info() override {cout << "classical sofa" << endl;};
};class Chair {
public:virtual void info() = 0;
};class ModernChair : public Chair {
public:void info() override {cout << "modern chair" << endl;};
};class ClassicalChair : public Chair {
public:void info() override {cout << "classical chair" << endl;};
};class Factory {
public:virtual Sofa *buildSofa() = 0;virtual Chair *buildChair() = 0;
};class ModernFactory : public Factory {
public:Sofa *buildSofa() override{return new ModernSofa();}Chair *buildChair() override{return new ModernChair();}
};class ClassicalFactory : public Factory {
public:Sofa *buildSofa() override{return new ClassicalSofa();}Chair *buildChair() override{return new ClassicalChair();}
};unordered_map<string, function<Factory*()>> factoryTable {{"modern", [](){ return new ModernFactory(); }},{"classical", [](){ return new ClassicalFactory(); }}
};int main()
{int loop;cin >> loop;string type;while (loop--){cin >> type;Factory *f = factoryTable[type]();Chair *c = f->buildChair();Sofa *s = f->buildSofa();c->info();s->info();}return 0;
}

建造者模式 4-???加?

建造者模式(也被成為?成器模式),是?種創(chuàng)建型設計模式,軟件開發(fā)過程中有的時候需要創(chuàng)建很復雜的對象,?建造者模式的主要思想是將對象的構(gòu)建過程分為多個步驟,并為每個步驟定義?個抽象的接?。具體的構(gòu)建過程由實現(xiàn)了這些接?的具體建造者類來完成。同時有?個指導者類負責協(xié)調(diào)建造者的?作,按照?定的順序或邏輯來執(zhí)?構(gòu)建步驟,最終?成產(chǎn)品。

  • 產(chǎn)品Product:被構(gòu)建的復雜對象, 包含多個組成部分。
  • 抽象建造者 Builder : 定義構(gòu)建產(chǎn)品各個部分的抽象接?和?個返回復雜產(chǎn)品的?法
  • 具體建造者 getResult Concrete Builder :實現(xiàn)抽象建造者接?,構(gòu)建產(chǎn)品的各個組成部分,并提供?個?法返回最終的產(chǎn)品。
  • 指導者 Director :調(diào)?具體建造者的?法,按照?定的順序或邏輯來構(gòu)建產(chǎn)品。

使?建造者模式有下??處優(yōu)點:

  • 使?建造者模式可以將?個復雜對象的構(gòu)建與其表示分離,通過將構(gòu)建復雜對象的過程抽象出來,可以使客戶端代碼與具體的構(gòu)建過程解耦
  • 同樣的構(gòu)建過程可以創(chuàng)建不同的表示,可以有多個具體的建造者(相互獨?),可以更加靈活地創(chuàng)建不同組合的對象
#include <iostream>
#include <string>
#include <unordered_map>
#include <functional>using namespace std;class Sofa {
public:virtual void info() = 0;
};class ModernSofa : public Sofa {
public:void info() override {cout << "modern sofa" << endl;};
};class ClassicalSofa : public Sofa {
public:void info() override {cout << "classical sofa" << endl;};
};class Chair {
public:virtual void info() = 0;
};class ModernChair : public Chair {
public:void info() override {cout << "modern chair" << endl;};
};class ClassicalChair : public Chair {
public:void info() override {cout << "classical chair" << endl;};
};class Factory {
public:virtual Sofa *buildSofa() = 0;virtual Chair *buildChair() = 0;
};class ModernFactory : public Factory {
public:Sofa *buildSofa() override{return new ModernSofa();}Chair *buildChair() override{return new ModernChair();}
};class ClassicalFactory : public Factory {
public:Sofa *buildSofa() override{return new ClassicalSofa();}Chair *buildChair() override{return new ClassicalChair();}
};unordered_map<string, function<Factory*()>> factoryTable {{"modern", [](){ return new ModernFactory(); }},{"classical", [](){ return new ClassicalFactory(); }}
};int main()
{int loop;cin >> loop;string type;while (loop--){cin >> type;Factory *f = factoryTable[type]();Chair *c = f->buildChair();Sofa *s = f->buildSofa();c->info();s->info();}return 0;
}

原型模式 5-矩形原型

如果?個對象的創(chuàng)建過程?較復雜時(?如需要經(jīng)過?系列的計算和資源消耗),那每次創(chuàng)建該對象都需要消耗資源,?通過原型模式就可以復制現(xiàn)有的?個對象來迅速創(chuàng)建/克隆?個新對象,不必關(guān)?具體的創(chuàng)建細節(jié),可以降低對象創(chuàng)建的成本。
原型模式的基本結(jié)構(gòu)
實現(xiàn)原型模式需要給【原型對象】聲明?個克隆?法,執(zhí)?該?法會創(chuàng)建?個當前類的新對象,并將原始對象中的成員變量復制到新?成的對象中,?不必實例化。并且在這個過程中只需要調(diào)?原型對象的克隆?法,??需知道原型對象的具體類型。
原型模式包含兩個重點模塊:

  • 抽象原型接? prototype : 聲明?個克隆?身的?法
  • 具體原型類 ConcretePrototype : 實現(xiàn) clone ?法,復制當前對象并返回?個新對象。在客戶端代碼中,可以聲明?個具體原型類的對象,然后調(diào)?clone() ?法復制原對象?成?個新的對象
#include<iostream>
#include<string>
using namespace std;class Prototype {
public:virtual Prototype* clone() = 0;virtual void print() = 0;virtual ~Prototype() {}
};class Triangle : public Prototype{
private:string color;int width, height;
public:Triangle(const string& color, int width, int height): color(color), width(width), height(height) {}Prototype* clone() override {return new Triangle(this->color, this->width, this->height);}void print() override {cout<<"Color: "<<this->color<<", Width: "<<this->width<<", Height: "<<this->height<<endl;}
};int main(){int n;string color;int w,h;cin >> color >> w >> h;cin >> n;while(n--){Prototype* original = new Triangle(color, w, h);Prototype* cloned = original->clone();cloned->print();delete original;delete cloned;}return 0;
}
http://m.aloenet.com.cn/news/43300.html

相關(guān)文章:

  • 如何制作門戶網(wǎng)站宣傳推廣
  • 個人網(wǎng)站 數(shù)據(jù)庫如何上傳到空間seo哪個軟件好
  • web設計師凌哥seo技術(shù)博客
  • 網(wǎng)頁設計與制作的理解紹興seo公司
  • 國外工會網(wǎng)站建設培訓網(wǎng)站有哪些
  • 英文網(wǎng)站報價蘇州網(wǎng)站建設開發(fā)公司
  • 海南網(wǎng)站備案百度一下你就知道官網(wǎng)新聞
  • wordpress 淘口令主題seo模擬點擊工具
  • 古色古香網(wǎng)站模板seo工具下載
  • 免費的外鏈網(wǎng)站青島自動seo
  • web前端開發(fā)環(huán)境有哪些做抖音seo排名軟件是否合法
  • 網(wǎng)站打開是404什么是電商?電商怎么做
  • 做物流網(wǎng)站費用多少產(chǎn)品宣傳推廣策劃
  • 鄭州做網(wǎng)站推廣的公司天津百度seo代理
  • 常州市建設局網(wǎng)站qq營銷
  • 網(wǎng)站搭建報價百度一下官網(wǎng)首頁百度一下
  • 深圳網(wǎng)站建設hi0755seo怎么發(fā)外鏈的
  • 互聯(lián)網(wǎng)網(wǎng)站建設公司百度新站關(guān)鍵詞排名
  • 網(wǎng)站備案要關(guān)站嗎google網(wǎng)頁搜索
  • 汽車網(wǎng)站建設規(guī)劃書百度首頁優(yōu)化排名
  • 網(wǎng)易游戲官網(wǎng)seo網(wǎng)站推廣如何做
  • 電子商務代運營百度筆記排名優(yōu)化
  • 上蔡專業(yè)網(wǎng)站建設突發(fā)大事震驚全國
  • 南寧哪里做網(wǎng)站輸入關(guān)鍵詞進行搜索
  • 網(wǎng)站權(quán)重劃分seo 是什么
  • 用pc做網(wǎng)站服務器為什么不如云主機百度輸入法下載
  • 長沙免費網(wǎng)站排名seo觀察網(wǎng)
  • 做o2o平臺網(wǎng)站需要多少錢chrome 谷歌瀏覽器
  • 個性個人網(wǎng)站模板建網(wǎng)站建設
  • 化妝品可做的團購網(wǎng)站有哪些seo排名優(yōu)化教程