zoukankan      html  css  js  c++  java
  • 游戏中的设计模式:工厂模式

    现在的网游更新很快,要延长网络游戏生命周期的方法是,更新,不断地更新,不断的将新内容呈现于玩家面前。这要求游戏程序的设计要有弹性,代码的重用至关重要。

    今天就说说游戏中的工厂模式。

    说到工厂模式,有简单工厂模式,工厂方法模式,抽象工厂模式。

    (一)简单工厂模式

    ps:面向对象的编程,并不是类越多越好,类的划分是为了封装,但分类的基础是抽象,具有相同属性和功能的对象的抽象集合才是类。

    主要用于创建对象。新添加类时,不会影响以前的系统代码。核心思想是用一个工厂来根据输入的条件产生不同的类,然后根据不同类的virtual函数得到不同的结果。

    (工厂类与基类为关联关系)

    //基类
    class COperation
    {
    public:
        int m_nFirst;
        int m_nSecond;
        virtual double GetResult()
        {
            double dResult=0;
            return dResult;
        }
    };
    
    //加法
    class AddOperation : public COperation
    {
    public:
        virtual double GetResult()
        {
            return m_nFirst+m_nSecond;
        }
    };
    
    //减法
    class SubOperation : public COperation
    {
    public:
        virtual double GetResult()
        {
            return m_nFirst-m_nSecond;
        }
    };
    
    //工厂类
    class CCalculatorFactory
    {
    public:
        static COperation* Create(char cOperator);
    };
    
    COperation* CCalculatorFactory::Create(char cOperator)
    {
        COperation *oper;
        //在C#中可以用反射来取消判断时用的switch,在C++中用什么呢?RTTI??
        switch (cOperator)
        {
        case '+':
            oper=new AddOperation();
            break;
        case '-':
            oper=new SubOperation();
            break;
        default:
            oper=new AddOperation();
            break;
        }
        return oper;
    }
    客户端
    int main()
    {
        int a,b;
        cin>>a>>b;
        COperation * op=CCalculatorFactory::Create('-');
        op->m_nFirst=a;
        op->m_nSecond=b;
        cout<<op->GetResult()<<endl;
        return 0;
    }
     

    优:适用于不同情况创建不同的类时。

    劣:客户端必须要知道基类和工厂类,耦合性差。

    (二)工厂方法模式

    简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。但简单工厂模式中不遵守开放-封闭原则。代码耦合性差。

    #include <string>
    #include <iostream>
    using namespace std;
    //实例基类,相当于Product(为了方便,没用抽象)
    class LeiFeng
    {
    public:
        virtual void Sweep()
        {
            cout<<"雷锋扫地"<<endl;
        }
    };
    
    //学雷锋的大学生,相当于ConcreteProduct
    class Student: public LeiFeng
    {
    public:
        virtual void Sweep()
        {
            cout<<"大学生扫地"<<endl;
        }
    };
    
    //学雷锋的志愿者,相当于ConcreteProduct
    class Volenter: public LeiFeng
    {
    public :
        virtual void Sweep()
        {
            cout<<"志愿者"<<endl;
        }
    };
    //工场基类Creator
    class LeiFengFactory
    {
    public:
        virtual LeiFeng* CreateLeiFeng()
        {
            return new LeiFeng();
        }
    };
    //工场具体类
    class StudentFactory : public LeiFengFactory
    {
    public :
        virtual LeiFeng* CreateLeiFeng()
        {
            return new Student();
        }
    };
    class VolenterFactory : public LeiFengFactory
    {
    public:
        virtual LeiFeng* CreateLeiFeng()
        {
            return new Volenter();
        }
    };
    
    //客户端
    int main()
    {
        LeiFengFactory *sf=new LeiFengFactory();
        LeiFeng *s=sf->CreateLeiFeng();
        s->Sweep();
        delete s;
        delete sf;
        return 0;
    }

    优:修正了简单工厂模式中不遵守开放-封闭原则。工厂方法模式把选择判断移到了客户端去实现,如果想添加新功能就不用修改原来的类,直接修改客户端即可。

    (三)抽象工厂模式

    提供一个创建一系列相关或相互依赖的接口,而无需指定它们的具体类。

    #include <string>
    #include <iostream>
    #include <vector>
    using namespace std;
    
    //用户抽象接口
    class IUser
    {
    public :
        virtual void GetUser()=0;
        virtual void InsertUser()=0;
    };
    
    //部门抽象接口
    class IDepartment
    {
    public:
        virtual void GetDepartment()=0;
        virtual void InsertDepartment()=0;
    };
    
    //ACCESS用户
    class CAccessUser : public IUser
    {
    public:
        virtual void GetUser()
        {
            cout<<"Access GetUser"<<endl;
        }
        virtual void InsertUser()
        {
            cout<<"Access InsertUser"<<endl;
        }
    };
    
    //ACCESS部门
    class CAccessDepartment : public IDepartment
    {
    public:
        virtual void GetDepartment()
        {
            cout<<"Access GetDepartment"<<endl;
        }
        virtual void InsertDepartment()
        {
            cout<<"Access InsertDepartment"<<endl;
        }
    };
    
    //SQL用户
    class CSqlUser : public IUser
    {
    public:
        virtual void GetUser()
        {
            cout<<"Sql User"<<endl;
        }
        virtual void InsertUser()
        {
            cout<<"Sql User"<<endl;
        }
    };
    
    //SQL部门类
    class CSqlDepartment: public IDepartment
    {
    public:
        virtual void GetDepartment()
        {
            cout<<"sql getDepartment"<<endl;
        }
        virtual void InsertDepartment()
        {
            cout<<"sql insertdepartment"<<endl;
        }
    };
    
    //抽象工厂
    class IFactory
    {
    public:
        virtual IUser* CreateUser()=0;
        virtual IDepartment* CreateDepartment()=0;
    };
    
    //ACCESS工厂
    class AccessFactory : public IFactory
    {
    public:
        virtual IUser* CreateUser()
        {
            return new  CAccessUser();
        }
        virtual IDepartment* CreateDepartment()
        {
            return new CAccessDepartment();
        }
    };
    
    //SQL工厂
    class SqlFactory : public IFactory
    {
    public:
        virtual IUser* CreateUser()
        {
            return new  CSqlUser();
        }
    
        virtual IDepartment* CreateDepartment()
        {
            return new CSqlDepartment();
        }
    };
    
    
    客户端:
    int main()
    {
        IFactory* factory= new SqlFactory();
        IUser* user=factory->CreateUser();
        IDepartment* depart = factory->CreateDepartment();
        user->GetUser();
        depart->GetDepartment();
        return 0;
    }

     参考:

    游戏中的设计模式:工厂模式

  • 相关阅读:
    前端性能优化-雅虎军规
    Webpack简易入门教程
    orm框架的使用
    【Nginx】使用nginx反向代理IIS实现80端口的解放
    【开发调试】谷歌浏览器中调试移动网页和测试网速下页面效果
    Identity4实现服务端+api资源控制+客户端请求
    【问题】VS问题集合,不用也要收藏防止以后使用找不到
    【worker】js中的多线程
    pdf.js插件使用记录,在线打开pdf
    【CSS】小妙招,各种问题总结方法处理
  • 原文地址:https://www.cnblogs.com/losophy/p/9521927.html
Copyright © 2011-2022 走看看