您的位置:首页 > 其它

设计模式——工厂方法 FactoryMethod

2016-07-18 22:33 363 查看
工厂接口

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个原则——开闭原则。

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

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

工厂方法使用起来太麻烦,要多很多类和工厂,感觉是类的大爆炸 :(
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  设计模式