大话设计模式学习篇--简单工厂模式

1 以一个计算器例子引入

将数据的输入和计算(加减乘除等)都写入主函数体内,代码难以得到复用。

2 业务封装

将业务逻辑(计算逻辑)与界面输入分隔开,将业务逻辑独立到类当中。

3  业务的继承与多态

在第二部分,计算逻辑被独立在一个类当中,实现了封装,但这还远远不够。现实情况下,业务逻辑通常会发生变化,如果整体的业务逻辑均放在一个类下,那么业务的修改将面临很多风险。这个时候,可采用继承和多态的方式解决。首先,对业务逻辑(计算过程)实现抽象;之后,让各个过程继承这一抽象,并独立实现;最终,当需要增加新逻辑时,只需继承这一抽象,并独立实现自己的逻辑。

4 简单工厂模式

用一个独立的类来完成实例的创建。简单工厂模式实际上是对实例化的细节进行封装。下面是一个比较好的例子,摘自博客:http://blog.csdn.net/aspnet2002web/article/details/5874405
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LxFactory
{
    //声明一个人类接口
    interface Ihuman
    {
        void getFav();        //声明未实现的爱好方法getFav()
        string getStatus();   //声明未实现的身份状态(孩子,成年人)方法
    }

    //声明一个抽象类Children,并实现接口的部分方法
    abstract class Children : Ihuman
    {
        protected string _status = "孩子";
        //实现接口Ihuman中的getStatus()方法
        public string getStatus()
        {
            return _status;
        }
        //将接口Ihuman中的getFav()方法映射到抽象方法
        abstract public void getFav();
    }
    //声明一个抽象类Adult,并实现接口的部分方法
    abstract class Adult : Ihuman
    {
        protected string _status = "成年人";
        //实现接口Ihuman中的getStatus()方法
        public string getStatus()
        {
            return _status;
        }
        //将接口Ihuman中的getFav()方法映射到抽象方法
        abstract public void getFav();
    }

    //声明男孩类Boy,继承小孩类Children,重写爱好方法getFav()
    class Boy : Children
    {
        public override void getFav()
        {
            Console.WriteLine("男孩,我喜欢游戏!");
        }
    }
    //声明女孩类Girl,继承小孩类Children,重写爱好方法getFav()
    class Girl : Children
    {
        public override void getFav()
        {
            Console.WriteLine("女孩,我喜欢布娃娃!");
        }
    }
    //声明男人类Man,继承成人类Adult,重写爱好方法getFav()
    class Man : Adult
    {
        public override void getFav()
        {
            Console.WriteLine("男人,我喜欢编程!");
        }
    }
    //声明女人类Woman,继承成人类Adult,重写爱好方法getFav()
    class Woman : Adult
    {
        public override void getFav()
        {
            Console.WriteLine("女人,我喜欢逛街!");
        }
    }
    //声明一个抽象工厂,分别创建相应类的实例
    abstract class HumanFactory
    {
        protected Ihuman h1 = new Boy();
        protected Ihuman h2 = new Man();
        protected Ihuman h3 = new Girl();
        protected Ihuman h4 = new Woman();
        //创建一个方法,根据不同输入返回一个接口的引用
        abstract public Ihuman getHuman(int i);
    }
    //声明可以实例化各种类的工厂Factory1,继承类HumanFactory,重写抽象方法getHuman()
    class Factory1 : HumanFactory
    {
        //根据不同输入获得不同对象所属接口的引用
        public override Ihuman getHuman(int i)
        {
            switch (i)
            {
                case 1:
                    return h1;
                case 2:
                    return h2;
                default:
                    return h1;
            }
        }
    }
    //声明可以实例化各种类的工厂Factory1,继承类HumanFactory,重写抽象方法getHuman()
    class Factory2 : HumanFactory
    {
        //根据不同输入获得不同对象所属接口的引用
        public override Ihuman getHuman(int i)
        {
            switch (i)
            {
                case 1:
                    return h3;
                case 2:
                    return h4;
                default:
                    return h3;
            }
        }
    }
    public class LxFactory
    {
        static void Main(string[] args)
        {
            //使用工厂1
            Console.WriteLine("男性中有两种人,请选择编号【1】男孩【2】男人:");
            int input1 = Int32.Parse(Console.ReadLine());
            Factory1 f1 = new Factory1();
            Ihuman h1 = f1.getHuman(input1);
            h1.getFav();
            Console.WriteLine("我的身份是:{0}", h1.getStatus());

            //使用工厂2
            Console.WriteLine("女性中有两种人,请选择编号【1】女孩【2】女人:");
            int input2 = Int32.Parse(Console.ReadLine());
            Factory2 f2 = new Factory2();
            Ihuman h2 = f2.getHuman(input2);
            h2.getFav();
            Console.WriteLine("我的身份是:{0}", h2.getStatus());
        }
    }

}


  • 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、付费专栏及课程。

余额充值