zoukankan      html  css  js  c++  java
  • 设计模式之工厂模式 练习

    设计模式中 最基本的工厂模式

    感觉就是根据输入的类型决定选择何种类与进行何种操作。

    跟面向过程中输入1则执行func1();输入2则执行func2()基本一致的想法

    #include <iostream>
    
    using namespace std;
    
    
    enum eShoeType{ leather = 0,rubber};
    
    class CShoe{
    public:
    	virtual void What() = 0;
    };
    
    class CLeatherShoe : public CShoe
    {
    public:
    	void What() {cout << "I am leather shoe." << endl;}
    };
    
    class CRubberShoe : public CShoe
    {
    public:
    	void What() {cout << "I am rubber shoe." << endl;}
    };
    
    class CShoeFactory
    {
    public:
    	CShoe* GetShowInstance(eShoeType type)
    	{
    		cout << "工厂生产鞋子ing..." << endl;
    		switch(type)
    		{
    		case leather:
    			return new CLeatherShoe();
    		case rubber:
    			return new CRubberShoe();
    		default:
    			return NULL;
    		}
    	}
    };
    
    void TestWithCpp()
    {
    	CShoeFactory factory;
    	CShoe* pShoe = NULL; 
    
    
    	pShoe = factory.GetShowInstance(leather);
    	if(NULL != pShoe)
    	{
    		pShoe->What();
    		delete pShoe;
    		pShoe = NULL;
    	}
    	
    
     	pShoe = factory.GetShowInstance(rubber);
    	if(NULL != pShoe)
    	{
    		pShoe->What();
    		delete pShoe;
    		pShoe = NULL;
    	}
    }
    ////////////////////////////////////////////////////////
    typedef struct SHOE{
    	int type;
    	void (*print_shoe)(struct SHOE*);
    }Shoe;
    
    void PrintLeatherShoe(Shoe* pShoe)
    {
    	if(NULL != pShoe)
    		printf("I am leather shoe \n");
    }
    
    void PrintRubberShoe(Shoe* pShoe)
    {
    	if(NULL != pShoe)
    		printf("I am rubber shoe \n");
    }
    
    
    Shoe* FactoryShoe(int type)
    {
    	Shoe* pShoe = NULL;
    
    	pShoe = (Shoe*)malloc(sizeof(Shoe));
    	if(NULL == pShoe)
    		return NULL;
    	
    	memset(pShoe,0,sizeof(Shoe));
    
    	if(leather == type)
    	{
    		pShoe->type = type;
    		pShoe->print_shoe = PrintLeatherShoe;
    	}else if( rubber == type)
    	{
    		pShoe->type = type;
    		pShoe->print_shoe = PrintRubberShoe;
    	}else
    	{
    		free(pShoe);
    		pShoe = NULL;
    		return NULL;
    	}
    	return pShoe;
    }
    
    
    
    void TestWithC()
    {
    	printf("\n\n 测试C版本工厂模式\n");
    
    
    	Shoe* pShoe = FactoryShoe(leather);
    	if(NULL != pShoe)
    	{
    		pShoe->print_shoe(pShoe);
    		free(pShoe);
    		pShoe = NULL;
    	}
    
    	pShoe = FactoryShoe(rubber);
    	if(NULL != pShoe)
    	{
    		pShoe->print_shoe(pShoe);
    		free(pShoe);
    		pShoe = NULL;
    	}
    
    	pShoe = FactoryShoe(994);
    	if(NULL != pShoe)
    	{
    		pShoe->print_shoe(pShoe);
    		free(pShoe);
    		pShoe = NULL;
    	}
    
    
    }
    
    
    ////////////////////////////////////////////////////////
    int _tmain(int argc, _TCHAR* argv[])
    {
    	TestWithCpp();
    	TestWithC();
    	return 0;
    }

    我的 c++ 交流群 324164944 
  • 相关阅读:
    JID 2.0 RC4 发布,高性能的 Java 序列化库
    FBReaderJ 1.6.3 发布,Android 电子书阅读器
    Arquillian 1.0.3.Final 发布,单元测试框架
    JavaScript 的宏扩展 Sweet.js
    Hypertable 0.9.6.5 发布,分布式数据库
    JRuby 1.7.0 发布,默认使用 Ruby 1.9 模式
    httppp 1.4.0 发布,HTTP响应时间监控
    Redis 2.6.0 正式版发布,高性能K/V服务器
    OfficeFloor 2.5.0 发布,IoC 框架
    XWiki 4.3 首个里程碑发布
  • 原文地址:https://www.cnblogs.com/itdef/p/3872722.html
Copyright © 2011-2022 走看看