設計模式——工廠方法 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個原則——開閉原則。

開閉原則:對擴展開發,對修改封閉

在某種角度上來說工廠方法是最完美的設計模式,因爲它完美的遵循了開閉原則

工廠方法使用起來太麻煩,要多很多類和工廠,感覺是類的大爆炸 :(

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章