设计模式——工厂方法 FactoryMethod

工厂接口

using Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryMethod
{
    /// <summary>
    /// 手机工厂接口
    /// </summary>
    public interface IBasePhoneFactory
    {
        BasePhone CreateBasePhone();
    }
}

手机基类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Interface
{
    public abstract class BasePhone
    {
        public abstract void Call();
        public abstract void Text();
    }
}

三星手机

using Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryMethod
{
    public class Galaxy : BasePhone
    {
        public Galaxy(string name)
        { }

        public override void Call()
        {
            Console.WriteLine("use {0} call", this.GetType().Name);
        }

        public override void Text()
        {
            Console.WriteLine("use {0} text", this.GetType().Name);
        }
    }
}

三星手机工厂

using Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryMethod
{
    public class GalaxyFactory : IBasePhoneFactory
    {
        public BasePhone CreateBasePhone()
        {
            return new Galaxy("FY");
        }
    }
}

苹果手机

using Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryMethod
{
    public class iPhone : BasePhone
    {
        public override void Call()
        {
            Console.WriteLine("use {0} call", this.GetType().Name);
        }

        public override void Text()
        {
            Console.WriteLine("use {0} text", this.GetType().Name);
        }
    }
}

苹果手机工厂

using Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryMethod
{
    public class iPhoneFactory : IBasePhoneFactory
    {
        public BasePhone CreateBasePhone()
        {
            return new iPhone();
        }
    }
}

魅族手机

using Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryMethod
{
    public class MX : BasePhone
    {
        public override void Call()
        {
            Console.WriteLine("use {0} call", this.GetType().Name);
        }

        public override void Text()
        {
            Console.WriteLine("use {0} text", this.GetType().Name);
        }
    }
}

魅族手机工厂

using Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryMethod
{
    public class MXFactory : IBasePhoneFactory
    {
        public BasePhone CreateBasePhone()
        {
            return new MX();
        }
    }
}

上端调用

using FactoryMethod;
using Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            BasePhone galaxy = new GalaxyFactory().CreateBasePhone();
            BasePhone iPhone = new iPhoneFactory().CreateBasePhone();
            BasePhone MX = new MXFactory().CreateBasePhone();
        }
    }
}

上面可以看出,只要我们在增加一个手机类型,同时在增加一个该类型手机工厂,那么我们对业务就可以很好的扩展


设计模式第6个原则——开闭原则。

开闭原则:对扩展开发,对修改封闭

在某种角度上来说工厂方法是最完美的设计模式,因为它完美的遵循了开闭原则

工厂方法使用起来太麻烦,要多很多类和工厂,感觉是类的大爆炸 :(

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值