zoukankan      html  css  js  c++  java
  • [创建型模式] Factory

    Factory.h

    //
    //  Factory.h
    //  Factory
    //
    //  Created by Cheney Shen on 11-2-20.
    //  Copyright 2011年 __MyCompanyName__. All rights reserved.
    //
    
    #ifndef _FACTORY_H_
    #define _FACTORY_H_
    
    class Product;
    
    class Factory
    {
        public:
        virtual ~Factory();
        
        virtual Product* CreateProduct()=0;
        
        protected:
        Factory();
        
        private:
        
    };
    
    class ConcreteFactory:public Factory
    {
        public:
        
        ~ConcreteFactory();
        
        ConcreteFactory();
        
        Product* CreateProduct();
        
        protected:
        
        private:
        
    };
    
    #endif //~_FACTORY_H_
    

    Factory.cpp

    //
    //  Factory.cpp
    //  Factory
    //
    //  Created by Cheney Shen on 11-2-20.
    //  Copyright 2011年 __MyCompanyName__. All rights reserved.
    //
    
    #include "Factory.h"
    #include "Product.h"
    
    #include <iostream>
    using namespace std;
    
    Factory::Factory()
    {
        
    }
    
    Factory::~Factory()
    {
        
    }
    
    ConcreteFactory::ConcreteFactory()
    {
        cout<<"ConcreteFacotry..."<<endl;
    }
    
    ConcreteFactory::~ConcreteFactory()
    {
        
    }
    
    Product* ConcreteFactory::CreateProduct()
    {
        return new ConcreteProduct();
    }
    

    Product.h

    //
    //  Product.h
    //  Factory
    //
    //  Created by Cheney Shen on 11-2-20.
    //  Copyright 2011年 __MyCompanyName__. All rights reserved.
    //
    
    #ifndef _PRODUCT_H_
    #define _PRODUCT_H_
    
    class Product
    {
        public:
        virtual ~Product()=0;
        
        protected:
        Product();
        
        private:
        
        
    };
    
    class ConcreteProduct : public Product
    {
        public:
        ~ConcreteProduct();  
        
        ConcreteProduct();
        
        protected:
        
        private:
        
    };
    #endif  // ~_PRODUCT_H_
    

    Product.cpp

    //
    //  Product.cpp
    //  Factory
    //
    //  Created by Cheney Shen on 11-2-20.
    //  Copyright 2011年 __MyCompanyName__. All rights reserved.
    //
    
    #include "Product.h"
    
    #include <iostream>
    using namespace std;
    
    Product::Product()
    {
        
    }
    
    Product::~Product()
    {
        
    }
    
    ConcreteProduct::ConcreteProduct()
    {
        cout<<"ConcreteProduct..."<<endl;
    }
    
    ConcreteProduct::~ConcreteProduct()
    {
        
    }
    

    main.cpp

    //
    //  main.cpp
    //  Factory
    //
    //  Created by Cheney Shen on 11-2-20.
    //  Copyright 2011年 __MyCompanyName__. All rights reserved.
    //
    
    #include "Factory.h"
    #include "Product.h"
    
    #include <iostream>
    using namespace std;
    
    int main (int argc, const char * argv[]) {
    
        Factory* fac = new ConcreteFactory();
        Product* p = fac->CreateProduct();
        return 0;
    }
    

  • 相关阅读:
    Swift中字符串转化为Class的方法
    React Native安装
    关于error:Cannot assign to 'self' outside of a method in the init family
    Xcode6中Swift没有智能提示和自动补全功能
    关于消息推送的补充,主要介绍服务端的实现,包含object c 版本 c 版本 java 版本 php 版本 (转)
    Git和GitHub在线学习资源整理(转)
    HTTP协议详解(转)
    iOS开发之GCD基础
    一步一步实现消息推送 2014-06-19
    理解Certificate、App Id、Identifiers 和 Provisioning Profile
  • 原文地址:https://www.cnblogs.com/shenfei2031/p/1979302.html
Copyright © 2011-2022 走看看