I have read the book Api Design For C++ recently , and I must say that it is a masterpiece.
There is a section to introduce the Factory Pattern, I think it is the best practical example for this pattern which I have read.
So I code it according the example, made little modification and added some annotation, then now share with you guys.
Example from:  
Api Design For C++, Martin Reddy
Section: 3.3.3 Extensible Factory Example


renderer.h
#include <iostream>
#include <string>

class IRenderer
{
public:
    virtual void Render()=0;
    virtual ~IRenderer()
    {
        std::cout<< "delete IRenderer object."<<std::endl;
    }
};

class UserRenderer:public IRenderer
{
public:
    ~UserRenderer() 
    {
        std::cout<< "delete UserRenderer object."<<std::endl;
    }
    bool LoadScene(const std::string &filename) 
    { 
        return true; 
    }

    void SetViewportSize(int w, int h) {}
    void SetCameraPosition(double x, double y, double z) {}
    void SetLookAt(double x, double y, double z) {}
    void Render() 
    { 
        std::cout << "User Render" << std::endl; 
    }
    
    static IRenderer* Create() 
    { 
        return new UserRenderer(); //Pay attention: this method must be static.
    }
    
};



rendererfactory.h

#include "renderer.h"
#include <string>
#include <map>

class RendererFactory
{
public:
    typedef IRenderer* (*CreateCallback)();
    static void RegisterRenderer(const std::string& type, CreateCallback cb);
    static void UnregisterRender(const std::string& type);
    static IRenderer* CreateRenderer(const std::string& type);

private:
    typedef std::map<std::string, CreateCallback> CallbackMap;
    static CallbackMap mRenderers;
};



rendererfactory.cpp
#include "rendererfactory.h"

RendererFactory::CallbackMap RendererFactory::mRenderers;

void RendererFactory::RegisterRenderer(const std::string &type, RendererFactory::CreateCallback cb)
{
    mRenderers[type] = cb;
}

void RendererFactory::UnregisterRender(const std::string &type)
{
    mRenderers.erase(type);
}

IRenderer* RendererFactory::CreateRenderer(const std::string &type)
{
    CallbackMap::iterator it = mRenderers.find(type);
    if(it!=mRenderers.end())
    {
        return (it->second)();
    }
    return NULL;
}



mainFile.cpp
#include <iostream>
#include "rendererfactory.h"

using namespace std;

int main()
{
    //Register a new renderer
    RendererFactory::RegisterRenderer("user", UserRenderer::Create);

    //create an instance of our new renderer
    IRenderer* render = RendererFactory::CreateRenderer("user");
    render->Render();
    delete render; //Remember to release the resources on heap.

    getchar()
    return 0;
}




An extensible Factory Pattern example的更多相关文章

  1. Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)

    一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...

  2. Net设计模式实例之简单工厂模式(Simple Factory Pattern)

    一.简单工厂模式简介(Bref Introduction) 简单工厂模式(Simple Factory Pattern)的优点是,工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类, ...

  3. [Design Pattern] Factory Pattern 简单案例

    Factory Pattern , 即工厂模式,用于创建对象的场景,属于创建类的设计模式 . 下面是一个工厂模式案例. Shape 作为接口, Circle, Square, Rectangle 作为 ...

  4. 设计模式 - Abstract Factory模式(abstract factory pattern) 详细说明

    Abstract Factory模式(abstract factory pattern) 详细说明 本文地址: http://blog.csdn.net/caroline_wendy/article/ ...

  5. 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)

    原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factor ...

  6. 设计模式 - 出厂模式(factory pattern) 详细说明

    出厂模式(factory pattern) 详细说明 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27081511 工厂方法模式 ...

  7. 设计模式复习小结一(Strategy Pattern/Observer Pattern/Decorator Patter/Factory Pattern)

    目录: 前言 1. Stratrgy Pattern 2. Observer Pattern 3. Decorator Pattern 4. Factory Pattern 4.1 FactoryPa ...

  8. 【设计模式】抽象工厂模式 Abstract Factory Pattern

    简单工厂模式是一个工厂类根据工厂方法的参数创建不出不同的产品, 工厂方法模式是每一个产品都有一个一一对应的工厂负责创建该产品.那么今天要讲的抽象工厂模式是一个工厂能够产生关联的一系列产品.抽象工厂模式 ...

  9. 【设计模式】简单工厂模式 Simple Factory Pattern

    简单工厂模式Simple Factory Pattern[Simple Factory Pattern]是设计模式里最简单的一个模式,又叫静态工厂模式[Static Factory Pattern], ...

随机推荐

  1. [Codeforces19D]Points 线段树

    大致题意: 给出n个询问,每次询问有三种: 1.往平面上加一个点 2.删除平面上的一个点 3.给出一个点p,查询平面上某点q,使得q.x>p.x且q.y>p.y,输出x轴坐标最小的q,若有 ...

  2. HDU 6025 Coprime Sequence

    枚举,预处理. 预处理前缀$gcd$与后缀$gcd$,枚举删哪一个即可. #include <bits/stdc++.h> using namespace std; int T,n; ]; ...

  3. 洛谷P1099 BZOJ1999 树网的核 [搜索,树的直径]

    洛谷传送门,BZOJ传送门 树网的核 Description 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边带有正整数的权,我们称T为树网(treenetwork),其中V ...

  4. 洛谷——P1349 广义斐波那契数列

    题目描述 广义的斐波那契数列是指形如an=p*an-1+q*an-2的数列.今给定数列的两系数p和q,以及数列的最前两项a1和a2,另给出两个整数n和m,试求数列的第n项an除以m的余数. 输入输出格 ...

  5. python 进程间通信(下)

    利用 Value,Array   先说明这个方法并不常用,因为有更灵活的方法 from multiprocessing import Process,Value,Array def f(n,a,not ...

  6. Linux下burg引导

    用得比较久了,比grub顺手: 安装: sudo add-apt-repository ppa:n-muench/burg; sudo apt-get update; sudo apt-get ins ...

  7. input限制中文字数

    我们知道input控件有一个maxlength属性可以控制输入字符的长度,但是,它并不会识别是汉字还是其他符号,所以输入maxlength个汉字显然是不符合要求的. 为了实现对带有汉字的输入框长度控制 ...

  8. 网络流24题之最长k可重线段集问题

    对于每个线段拆成两个点,如同之前一样建图,由于可能出现垂直于x轴的 所以建图由i指向i~ 继续最小费用最大流 By:大奕哥 #include<bits/stdc++.h> using na ...

  9. Codeforces 1103 C. Johnny Solving

    Codeforces 1103 C. Johnny Solving 题目大意: 有一张 \(n\) 个点 \(m\) 条边的简单无向图,每个点的度数至少为 \(3\) ,你需要构造出两种情况之一 一条 ...

  10. 数据离散化 ( 以及 stl 中的 unique( ) 的用法 )+ bzoj3289:Mato的文件管理

    http://blog.csdn.net/gokou_ruri/article/details/7723378 ↑惯例Mark大神的博客   bzoj3289:Mato的文件管理 线段树求逆序对+莫队 ...