設計模式——抽象工廠

抽象工廠關注點是:一個工廠裏面可以創建多種類型的實例
比如 既可以創建 Phone 也可以創建Pad
抽象工廠包含多少個職責,分別創建不同類型的實例

抽象工廠和簡單工廠的區別在於,工廠可以擴展

抽象工廠適合工廠職責是穩定的情況下


平板基類

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

namespace DesignMode.Interface
{
    public abstract class BasePad
    {
        public abstract void Online();
        public abstract void Photo();
    }
}

iPad類

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

namespace AbstractFactory
{
    public class iPad : BasePad
    {
        public override void Online()
        {
            Console.WriteLine("use {0} Online", this.GetType().Name);
        }

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

山寨版 iPad類

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

namespace AbstractFactory
{
    public class iPadChina : BasePad
    {
        public override void Online()
        {
            Console.WriteLine("use {0} Online", this.GetType().Name);
        }

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

手機基類

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

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

iPhone 類

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

namespace AbstractFactory
{
    public class iPhone : BasePhone
    {
        public iPhone(int price)
        {

        }

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

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

山寨版 iPhone 類

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

namespace AbstractFactory
{
    public class iPhoneChina : BasePhone
    {
        public iPhoneChina(int price)
        {

        }

        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 DesignMode.Interface;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFactory
{
    /// <summary>
    /// 約束作用
    /// </summary>
    public abstract class AbstractFactory
    {
        /// <summary>
        /// 創建pad
        /// </summary>
        /// <returns></returns>
        public abstract BasePad CreatePad();

        /// <summary>
        /// 創建Phone
        /// </summary>
        /// <returns></returns>
        public abstract BasePhone CreateiPhone();

        //public abstract BaseWatch CreateWatch();
    }
}

美版工廠

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

namespace AbstractFactory
{
    public class ObjectFactory : AbstractFactory
    {

        public override BasePad CreatePad()
        {
            return new iPad();
        }

        public override BasePhone CreateiPhone()
        {
            return new iPhone(5000);
        }


    }
}

國版工廠(山寨版工廠)

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

namespace AbstractFactory
{
    public class ObjectFactoryChina : AbstractFactory
    {

        public override BasePad CreatePad()
        {
            return new iPadChina();
        }

        public override BasePhone CreateiPhone()
        {
            return new iPhoneChina(3500);
        }
    }
}

上端調用

using AbstractFactory;
using DesignMode.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)
        {
            {
                AbstractFactory.AbstractFactory objectFactory = new ObjectFactory();
                BasePhone iphone = objectFactory.CreateiPhone();
                BasePad ipad = objectFactory.CreatePad();
            }

            {
                AbstractFactory.AbstractFactory objectFactory = new ObjectFactoryChina();
                BasePhone iphone = objectFactory.CreateiPhone();
                BasePad ipad = objectFactory.CreatePad();
            }
            Console.ReadKey();

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