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. ZOJ 3955 Saddle Point

    排序. 枚举每一个格子,计算这个格子在多少矩阵中是鞍点,只要计算这一行有多少数字比他大,这一列有多少数字比他小,方案数乘一下就是这个格子对答案做出的贡献. #include<bits/stdc+ ...

  2. 实现RMQ的两种常用方法

    RMQ RMQ(Range Maximum/Minimum Question)是指区间最值问题,在OI中较为常见,一般可以用ST表和线段树实现. ST表是基于倍增思想的一种打表方法,在确定区间范围和所 ...

  3. .zip格式和zip伪加密

    ZIP文件的组成: 压缩源文件数据区+压缩源文件目录区+压缩源文件目录结束标志 压缩源文件数据区 50 4B 03 04:这是头文件标记(0x04034b50) 14 00:解压文件所需 pkware ...

  4. Python开发基础-Day25-28FTP项目(待补充)

    optparse C:\Users\Mr.chai>python C:/Users/Mr.chai/Desktop/PythonProject/DAY/day27/LuffyFTP/client ...

  5. hdu 1253

    D - 胜利大逃亡 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  6. python中对list去重的多种方法

    今天遇到一个问题,用了 itertools.groupby 这个函数.不过这个东西最终还是没用上. 问题就是对一个list中的新闻id进行去重,去重之后要保证顺序不变. 直观方法 最简单的思路就是: ...

  7. 【BZOJ 2555】 2555: SubString (SAM+LCT)

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 2548  Solved: 762 Description 懒得写背景了 ...

  8. Matrix-tree定理 spoj HIGH

    Matrix-tree定理,给出一个无向图,问求出的生成树方案有多少种方案,利用Matrix-tree定理,主对角线第i行是i的度数,(i,j) 值为i和j之间边的数量,然后删去第一行第一列,利用初等 ...

  9. redisson实现基于业务的互斥锁

    虽然数据库有锁的实现,但是有时候对于数据的操作更需要业务层控制. 这个解决的问题有次面试被问到过,当时不知道怎么解决,乱说一通,今天也算是有个解决方案了 项目中有个需求,就是订单需要经过一层一层的审核 ...

  10. bzoj 1051 强连通分量

    反建图,计算强连通分量,将每个分量看成一个点,缩点后的图是一个DAG,如果是一棵树,则根代表的连通分量的大小就是答案,否则答案为0. 收获: 图的东西如果不好解决,可以尝试缩点(有向图将每个强连通分量 ...