大话设计模式-第08章 雷锋依然在人间--工厂方法模式

08 雷锋依然在人间--工厂方法模式

8.2 简单工厂模式实现

8.2 简单工厂模式实现.tiff


class OperationFactory

{

    public static Operation createOperate(string operate)

    {

        Operation Oper = null;

        switch (operate)

        {

            case “+”:

                oper = new OperationAdd();

                break;

            case “-“:

                oper = new OperationSub();

                break;

            case “*”:

                oper = new OperationMul();

                break;

            case “/“

                oper = new OperationDiv();

                break;

        }

        return oper;

    }

}


客户端的应用

Operation oper;

oper = OperationFactory.createOperate(“+”);

oper.NumberA = 1;

oper.NumberB = 2;

double result = oper.GetResult();



8.3 工厂方法模式实现

8.3 工厂方法模式实现.tiff


先构建一个工厂接口:

interface IFactory

{

    Operation CreateOperation();

}

然后加减乘除各建一个具体工厂去实现这个接口。

class AddFactory : IFactory

{

    public Operation CreateOperation()

    {

        return new OperationAdd();

    }

}


class SubFactory : IFactory

{

    public Operation CreateOperation()

    {

        return new OperationSub();

    }

}


class MulFactory : IFactory

{

    public Operation CreateOperation()

    {

        return new OperationMul();

    }

}


class DivFactory : IFactory

{

    public Operation CreateOperation()

    {

        return new OperationDiv();

    }

}


客户端的实现是这样的:

IFactory operFactory new AddFactory();

Operation oper = operFactory.CreateOperation();

oper.NumberA = 1;

oper.NumberB = 2;

double result = oper.GetResult();


8.4 简单工厂VS工厂方法

    简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。但简单工厂不但对扩展开放,也对修改开放,这违背了开放封闭原则

简单工厂VS工厂方法.tiff


    我们讲过,既然这个工厂类与分支耦合,那么我们就对他下手,根据依赖倒转原则,我们把工厂类抽象出一个接口,这个接口只有一个方法,就是创建抽象产品的工厂方法。然后,所有的要生产具体类的工厂,就去实现这个接口,这样,一个简单工厂模式的工厂类,就变成了一个工厂抽象接口和多个具体生成对象的工厂,当我们需要增加新的功能时,就不需要更改原有的工厂类了,只需要增加此功能的运算类和相应的工厂类就可以了。

简单工厂VS工厂方法 结构图.tiff


    这样整个工厂和产品体系其实都没有修改的变化,而只是扩展的变化,这就完全符合了开放-封闭原则的精神。

    工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的,也就是说,工厂方法把简单工厂的内部逻辑判断移动到了客户端代码来运行。你想要加功能,本来是该工厂类的,而现在是修改客户端。


8.5 雷锋工厂

//雷锋

class LeiFeng

{

    public void Sweep()

    {

        Console.WriteLine(“扫地”);

    }

    public void Wash()

    {

        Console.WriteLine(“洗衣”);

    }

    public void BuyRice()

    {

        Console.WriteLine(“买米”);

    }

}

//学雷锋的大学生

class Undergraduate : LeiFeng

{ }


//客户端实现

Leiteng xueleifeng = new Undergraduate();

xueleifeng.BuyRice();

xueleifeng.Sweep();

xueleifeng.Wash();


//如果是有三个人学雷锋

Leiteng xueleifeng1 = new Undergraduate();

xueleifeng1.BuyRice();


Leiteng xueleifeng2 = new Undergraduate();

xueleifeng2.Sweep();


Leiteng xueleifeng3 = new Undergraduate();

xueleifeng3.Wash();


//社区志愿者

class Volunteer : LeiFeng

{ }


//简单雷锋工厂

class SimpleFactory

{

    public static LeiFeng CreateLeiFeng(string type)

    {

        LeiFeng result = null;

        switch (type)

        {

            case “学雷锋的大学生”:

                result = new Undergraduate();

                break;

            case “社区志愿者”:

                result = new Volunteer();

                break;

        }

        return result;

    }

}


//简单工厂模式

LeiFeng studentA = SimpleFactory.CreateLeiFeng(“学雷锋的大学生”);

studentA.BuyRice();


LeiFeng studentB = SimpleFactory.CreateLeiFeng(“学雷锋的大学生”);

studentB.Sweep();


LeiFeng studentC = SimpleFactory.CreateLeiFeng(“学雷锋的大学生”);

studentC.Wash();


此时你就发现,你需要在任何实例化的时候写出这个工厂的代码。这里有重复,也就有了坏味道。再用工厂方法模式写一遍:

//雷锋工厂

interface IFactory

{

    LeiFeng CreateLeiFeng();

}


//学雷锋的大学生工厂

class UndergraduateFactory : IFactory

{

    public LeiFeng CreateLeiFeng()

    {

        return new Undergraduate;

    }

}


//社区自愿者工厂

class VolunteerFactory : IFactory

{

    public LeiFeng CreateLeiFeng()

    {

        return new Volunteer();

    }

}


客户端调用的时候只需要这样就可以一了。

//工厂方法模式

IFactory factory = new UndergraduateFactory();

LeiFeng student = factory.CreateLeiFeng();


student.BuyRice();

student.Sweep();

student.Wash();

现在如果需要修改的话只需要修改一处就可以了。利用反射技术可以做到更好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单工厂模式(Simple Factory Pattern)是一种创建型设计模式,它通过一个工厂类来创建不同类型的对象,而无需暴露对象创建的逻辑给客户端。在Python中,简单工厂模式可以通过一个工厂类来创建不同的产品对象。下面是一个简单的示例: ```python class Product: def operation(self): pass class ConcreteProductA(Product): def operation(self): print("Performing operation A.") class ConcreteProductB(Product): def operation(self): print("Performing operation B.") class SimpleFactory: @staticmethod def create_product(product_type): if product_type == "A": return ConcreteProductA() elif product_type == "B": return ConcreteProductB() else: raise ValueError("Invalid product type.") # 使用简单工厂创建产品对象 factory = SimpleFactory() product_a = factory.create_product("A") product_a.operation() product_b = factory.create_product("B") product_b.operation() ``` 在上述示例中,`Product` 是一个抽象产品类,`ConcreteProductA` 和 `ConcreteProductB` 是具体产品类。`SimpleFactory` 是工厂类,通过 `create_product` 方法根据不同的产品类型创建相应的产品对象。 通过简单工厂模式,客户端无需知道具体的产品类,只需要通过工厂类来创建产品对象。这样可以降低客户端与具体产品类的耦合度,并且当需要新增产品时,只需要修改工厂类即可。 希望这个简单的示例能帮助你理解简单工厂模式在Python中的应用。如果有任何进一步的问题,请随时提问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值