zoukankan      html  css  js  c++  java
  • 设计模式(五):工厂模式

      设计模式,是一种指导思想,是一种需要融合进代码设计的一种提纲挈领。

      今天进入工厂模式探究:

      简单工厂:

         简单工厂即在工厂类中,根据所输入产品的类型,生产对应的产品。但是,这样的话,工程类就需要包含所有产品的生产实现。例子如下:

        

    #include <iostream>
    
    using namespace std;
    
    
    enum CAR_TYPE { 
        ROLLSROYCE,
        BENTLEY,
        MAYBACH,
        LAMBORGHINI,
        FERRARI,
        ASTONMARTIN
     };
    
    class Car
    {
    public:
        Car(){}
        ~Car(){}
    
        virtual void showType(){}
    
        /* data */
    };
    
    class RollsRoyce : public Car
    {
    public:
        RollsRoyce(){}
        ~RollsRoyce(){}
    
        void showType()
        {
            cout << "Tish is RollsRoyce, from German." << endl;
        }
        /* data */
    };
    
    class Bentley : public Car
    {
    public:
        Bentley(){}
        ~Bentley(){}
    
        void showType()
        {
            cout << "This is Bentley, from England." << endl;
        }
    
        /* data */
    };
    
    class Maybach : public Car
    {
    public:
        Maybach(){}
        ~Maybach(){}
    
        /* data */
        void showType()
        {
            cout << "This is Maybach, from Netherland." << endl;
        }
    };
    
    class CarFactory
    {
    public:
        CarFactory(){}
        ~CarFactory(){}
    
        static Car* CreateFactory( enum CAR_TYPE carType )
        {
            if ( ROLLSROYCE == carType )
            {
                /* code */
                return new RollsRoyce();
            }
            else if ( BENTLEY == carType )
            {
                return new Bentley();
            }
            else if ( MAYBACH == carType )
            {
                return new Maybach();
            }
            else
            {
                return NULL;
            }
        }
    
    
        /* data */
    };
        
    
    void TestSimpleFactory()
    {
        // CarFactory carfactory;
    
        Car* pCar = CarFactory::CreateFactory( ROLLSROYCE );
        pCar->showType();
    
        delete pCar;
    
        pCar = CarFactory::CreateFactory( BENTLEY );
        pCar->showType();
    
        delete pCar;
    }
    
    int main()
    {
        TestSimpleFactory();
        return 0;
    }

      上例中工厂类方法中,因为不涉及到其他的成员属性,所以使用static 类静态方法。

    class Car(object):
        """docstring for Car"""
        def __init__(self):
            self.speed = None
    
    class Rollsroyce( Car ):
        """docstring for """
        def __init__(self ):
            self.name = "Rollsroyce"
    
        def showType( self ):
            print "This is " + self.name
    
    class Bentley( Car ):
        def __init__( self ):
            self.name = "Bentley"
    
        def showType( self ):
            print "This is " + self.name
    
    class CarFactory(object):
        """docstring for CarFactory"""
        
        @classmethod
        def CreateCar( cls, type ):
            if "BENTLEY" == type:
                return Bentley()
            elif "ROLLSROYCE" == type:
                return Rollsroyce()
    
    
            
    
    if __name__ == '__main__':
        car = CarFactory.CreateCar( "BENTLEY" )
        car.showType()
        car = CarFactory.CreateCar( "ROLLSROYCE" )
        car.showType()
  • 相关阅读:
    inline-block图文布局
    display: inline-block换行问题
    前端Fiddler高级调试技巧
    开发人员linux命令总结
    随笔-系统安装
    Fiddler高阶技能-项目文件代理
    译:如何使用时间轴工具
    jquery 选择器之children与find
    ASP.NET 大文件下载的实现思路及代码
    Git 使用指南
  • 原文地址:https://www.cnblogs.com/bracken/p/3013155.html
Copyright © 2011-2022 走看看