zoukankan      html  css  js  c++  java
  • c++简单工厂类的设计模式

    /*简单工厂设计模式
    */
    
    #include <iostream>
    using namespace std;
    class Fruit
    {
        /************************************************************************/
        /* 创建一个抽象水果类                                                               */
        /************************************************************************/
    public:
        virtual void getFruit()=0;
    protected:
    private:
    };
    
    class Banana :public Fruit
    {
    public:
        virtual void getFruit(){
            cout<<"香蕉"<<endl;
        }
    protected:
    private:
    };
    
    class pear :public Fruit
    {
    public:
        void getFruit(){
            cout<<"pear"<<endl;
        }
    
    protected:
    private:
    };
    
    class Factory
    {
        /************************************************************************/
        /* 创建一个工厂类,生产具体的水果                                                                     */
        /************************************************************************/
    public:
          static Fruit * Create(char *name){
            Fruit * temp=NULL;
            if(strcmp(name,"banana")==0){
                temp=new Banana();
            }else if(strcmp(name,"pear")==0){
                temp=new pear();
            }else{
                return NULL;
            }
        }
    protected:
    private:
    };
    
    int main(){
        Fruit *pear=Factory::Create("pear");
    if(pear==NULL){ cout<<"生产失败"<<endl; } pear->getFruit(); Fruit *banana =Factory::Create("banana"); banana->getFruit(); char * a="xsaxa"; cout<< *a <<endl; return 0; }
  • 相关阅读:
    常用的字符集编码
    live555—VS2010/VS2013 下live555编译、使用及测试(转载)
    win32下Socket编程(转载)
    do{...}while(0)的意义和用法(转载)
    C++ static与单例模式
    MFC多线程各种线程用法 .
    a^1+b problem
    重返现世——题解
    第K大C
    懒癌
  • 原文地址:https://www.cnblogs.com/zhangyanguang/p/4923437.html
Copyright © 2011-2022 走看看