Factory Method(工廠方法)-對象創建型模式

来源:http://www.cnblogs.com/mgp200866130/archive/2016/05/05/5463698.html
-Advertisement-
Play Games

1.意圖 定義一個用於創建對象的介面,讓子類決定實例化哪一個類。Factory Method使一個類的實例化延遲到其子類。 2.動機 框架使用抽象類定義和維護對象之間的關係。這些對象的創建通常也由框架負責。 3.適用性 當一個類不知道它所必須創建的對象的類的時候。 當一個類希望由它的子類來指定它所創 ...


1.意圖

    定義一個用於創建對象的介面,讓子類決定實例化哪一個類。Factory Method使一個類的實例化延遲到其子類。

2.動機

    框架使用抽象類定義和維護對象之間的關係。這些對象的創建通常也由框架負責。

3.適用性

  • 當一個類不知道它所必須創建的對象的類的時候。
  • 當一個類希望由它的子類來指定它所創建的對象的時候。
  • 當類將創建對象的職責委托給多個幫助子類中的某一個,並且你希望將哪一個幫助子類是代理者這一局部化的時候。

4.結構

         

5.代碼實例

    

class Product
{
public:
    virtual ~Product() = 0;
};

class OneProduct : public Product
{
public:
    OneProduct();
};

class TwoProduct : public Product
{
public:
    TwoProduct();
};

class ThreeProduct : public Product
{
public:
    ThreeProduct();
};

class FourProduct : public Product
{
public:
    FourProduct();
};
Product.h
#include <memory>

class Product;
class Creator
{
public:
    virtual std::shared_ptr<Product> FactoryMethod() = 0;
};

class OneCreator : public Creator
{
public:
    std::shared_ptr<Product> FactoryMethod();
};

class TwoCreator : public Creator
{
public:
    std::shared_ptr<Product> FactoryMethod();
};


class ThreeCreator : public Creator
{
public:
    std::shared_ptr<Product> FactoryMethod();
};


class FourCreator : public Creator
{
public:
    std::shared_ptr<Product> FactoryMethod();
};
Creator.h
#include "Product.h"
#include <iostream>

Product::~Product()
{

}


OneProduct::OneProduct()
{
    std::cout << "OneProduct Created" << std::endl;
}

TwoProduct::TwoProduct()
{
    std::cout << "TwoProduct Created" << std::endl;
}

ThreeProduct::ThreeProduct()
{
    std::cout << "ThreeProduct Created" << std::endl;
}

FourProduct::FourProduct()
{
    std::cout << "FourProduct Created" << std::endl;
}
Product.cpp
#include "Creator.h"
#include "Product.h"


std::shared_ptr<Product> OneCreator::FactoryMethod()
{
    auto pProduct = std::shared_ptr<Product>(new OneProduct());
    return pProduct;
}

std::shared_ptr<Product> TwoCreator::FactoryMethod()
{
    auto pProduct = std::shared_ptr<Product>(new TwoProduct());
    return pProduct;
}

std::shared_ptr<Product> ThreeCreator::FactoryMethod()
{
    auto pProduct = std::shared_ptr<Product>(new ThreeProduct());
    return pProduct;
}

std::shared_ptr<Product> FourCreator::FactoryMethod()
{
    auto pProduct = std::shared_ptr<Product>(new FourProduct());
    return pProduct;
}
Creator.cpp
#include<memory>
#include"Creator.h"

int main()
{
    auto pOneCreator = std::shared_ptr<OneCreator>(new OneCreator());
    auto pOneProduct = pOneCreator->FactoryMethod();

    auto pTwoCreator = std::shared_ptr<TwoCreator>(new TwoCreator());
    auto pTwoProduct = pTwoCreator->FactoryMethod();

    auto pThreeCreator = std::shared_ptr<ThreeCreator>(new ThreeCreator());
    auto pThreeProduct = pThreeCreator->FactoryMethod();

    auto pFourCreator = std::shared_ptr<FourCreator>(new FourCreator());
    auto pFourProduct = pFourCreator->FactoryMethod();

    while(1);
}
Test.cpp

6.測試結果

7.效果

  • 優點:代碼僅處理Product介面,不再綁定特定的ConcreteProduct。
  • 缺點:一個工廠方法僅創建一種產品,創建特定的ConcreteProduct需要創建Creator的子類。

8.變形

    工廠方法的變形是:可以參數化工廠方法,以使工廠方法可以創建多種產品。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • discuz x3在DIY模塊中調用偽靜態不成功,顯示動態鏈接,然而其他的鏈接正常顯示偽靜態。 後臺啟用偽靜態後,發現論壇版塊、帖子點擊鏈接,偽靜態正常顯示,然後在門戶首頁DIY顯示的帖子,點進去後發現鏈接還是動態的鏈接,不宜被搜索引擎抓取。 解決辦法: 第一步:前臺管理員登錄 --> DIY -- ...
  • HibernateCallback是回調函數,是一個介面,該介面只有一個方法doInHibernate(org.hibernate.Session session), 這個方法體就是Spring執行的持久化操作。(上面的函數,是用hirbernate的回調函數來刪除id,因為類型是int,所以最後r ...
  • 原文:http://www.imsiren.com/archives/535 為了以後能開發PHP擴展..就一定要瞭解PHP的執行順序..這篇文章就是為C開發PHP擴展做鋪墊. web環境 我們假設為 apache. 在編譯PHP的時候,為了能夠讓Apache支持PHP,我們會生成一個mod_php ...
  • python支持對mysql的操作 已經安裝配置成功python、mysql 之後根據各自電腦配置選擇對應系統的MySQL-python 文件是EXE格式、打開下一步即可 下載地址博主分享下: http://pan.baidu.com/s/1c2uhVwc 安裝成功、在IDLE中輸入: import ...
  • [Architecture] 系統架構正交分解法 前言 隨著企業成長,支持企業業務的軟體,也會越來越龐大與複雜。當系統複雜到一定程度,開發人員會發現很多系統架構的設計細節,很難有條理、有組織的用一張大藍圖去做分析設計。先前在InfoQ上看到一篇文章:「億級用戶下的新浪微博平臺架構 - 衛向軍」,在這 ...
  • 關鍵的設計原則 在開始設計之前,思考一下關鍵的原則,將會幫助你創建一個最小花費、高可用性和擴展性的架構。 分離關註點,將應用劃分為在功能上儘可能不重覆的功能點。主要的參考因素就是最小化交互,高內聚、低耦合。但是,錯誤的分離功能邊界,可能會導致功能之間的高耦合性和複雜性, 職責單一,每一個組件或者是模 ...
  • 簡單工廠模式是屬於創建型模式,又叫做靜態工廠方法(Static Factory Method)模式,但不屬於23種GOF設計模式之一。簡單工廠模式是由一個工廠對象決定創建出哪一種產品類的實例。簡單工廠模式是工廠模式家族中最簡單實用的模式,可以理解為是不同工廠模式的一個特殊實現。 簡單工廠模式的UML ...
  • 解釋器模式(Interpreter) 定義 解釋器模式(Interpreter),給定一個語言,定義它的文法的一種表示,並定義一個解釋器,這個解釋器使用該表示來解釋語言中的句子。 類圖 描述 Expression:抽象表達式,聲明一個所有的具體表達式都需要實現的抽象介面;這個介面主要是一個inter ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...