简单工厂模式
简单工厂是工厂模式中比较简单何容易理解的一种模式。简单工厂模式其实就是用来创建不同类型的对象的。
代码:
public enum PhoneType {
Apple = 1,
XiaoMi,
HuaWei,
}
public class SimpleFactroy : MonoBehaviour {
void Start () {
for (int i = 0; i < 10; i++)
{
int index = Random.Range(1, 4);
IPhone phone = Factroy.CreatePhone((PhoneType)index);
Debug.Log(phone.version());
}
}
}
public interface IPhone {
string version();
}
public class ApplePhone : IPhone
{
public string version()
{
return "苹果";
}
}
public class XiaoMiPhone : IPhone {
public string version()
{
return "小米";
}
}
public class HuaWeiPhone : IPhone {
public string version()
{
return "华为";
}
}
//把对象的创建过程和初始化过程移到了工厂中,使用时比较方便
public class Factroy {
public static IPhone CreatePhone(PhoneType type) {
switch (type) {
case PhoneType.Apple:
return new ApplePhone();
case PhoneType.XiaoMi:
return new XiaoMiPhone();
case PhoneType.HuaWei:
return new HuaWeiPhone();
default:
return null;
}
}
}
优点:
1、简单易懂
2、逻辑清晰,就是根据不同的枚举类型创建不同的对象实例。
缺点:
违反了开闭原则,当对代码进行扩展时,需要对之前的代码进行改动。
工厂模式
工厂模式和简单工厂的区别就是,工厂模式克服了简单工厂的违反开闭原则,将工厂进行抽象,将实现逻辑延迟到工厂的子类当中。
代码:
public class SimpleFactroy2 : MonoBehaviour
{
void Start()
{
AppleFactroy appleFac = new AppleFactroy();
Debug.Log(appleFac.CreatePhone(1).version());
Debug.Log(appleFac.CreatePhone(2).version());
Debug.Log(appleFac.CreatePhone(3).version());
XiaoMiFactroy miFactory = new XiaoMiFactroy();
Debug.Log(miFactory.CreatePhone(3).version());
}
}
public interface IPhone
{
string version();
}
public class ApplePhone : IPhone
{
public string version()
{
return "苹果";
}
}
public class XiaoMiPhone : IPhone
{
public string version()
{
return "小米";
}
}
public class HuaWeiPhone : IPhone
{
public string version()
{
return "华为";
}
}
public interface IFactroy {
IPhone CreatePhone(int type);
}
public class AppleFactroy : IFactroy {
public IPhone CreatePhone(int type) {
return new ApplePhone();
}
}
public class XiaoMiFactroy : IFactroy {
public IPhone CreatePhone(int type) {
return new XiaoMiPhone();
}
}
public class HuaWeiFactroy : IFactroy {
public IPhone CreatePhone(int type)
{
return new HuaWeiPhone();
}
}
优点:
1、整体代码结构清晰,职责单一。
2、可扩展性和稳定性提高
缺点:
当增加新的产品的时候,就会怎加一个新的类,代码的复杂程度提高了。
抽象工厂
抽象工厂和工厂模式的区别就是在工厂模式的基础上增加了产品族这个感念,也就是说抽象工厂用来生成一系列产品时是更好的。
代码:
public class SimpleFactroy3 : MonoBehaviour
{
void Start()
{
IFactory apple = new AppleFactory();
Debug.Log(apple.CreateBook(1).bookVersion());
Debug.Log(apple.CreatePhone(1).version());
IFactory huawei = new HuaWeiFactory();
Debug.Log(huawei.CreateBook(1).bookVersion());
Debug.Log(huawei.CreatePhone(1).version());
}
}
public interface IPhone
{
string version();
}
public interface IBook
{
string bookVersion();
}
public class ApplePhone : IPhone
{
public string version()
{
return "苹果手机";
}
}
public class AppleBook : IBook
{
public string bookVersion()
{
return "苹果电脑";
}
}
public class XiaoMiPhone : IPhone
{
public string version()
{
return "小米手机";
}
}
public class XiaoMiBook : IBook
{
public string bookVersion()
{
return "小米电脑";
}
}
public class HuaweiPhone : IPhone
{
public string version()
{
return "华为手机";
}
}
public class HuaweiBook : IBook
{
public string bookVersion()
{
return "华为电脑";
}
}
public interface IFactory
{
IPhone CreatePhone(int type);
IBook CreateBook(int type);
}
//苹果品牌
public class AppleFactory : IFactory
{
public IPhone CreatePhone(int type)
{
return new ApplePhone();
}
public IBook CreateBook(int type)
{
return new AppleBook();
}
}
//小米品牌
public class XiaoMiFactory : IFactory
{
public IPhone CreatePhone(int type)
{
return new XiaoMiPhone();
}
public IBook CreateBook(int type)
{
return new XiaoMiBook();
}
}
//华为品牌
public class HuaWeiFactory : IFactory
{
public IPhone CreatePhone(int type)
{
return new HuaweiPhone();
}
public IBook CreateBook(int type)
{
return new HuaweiBook();
}
}
优点:
可以使我们很轻松的就增加了一系列产品的实现。
缺点:
不适合产品的横向扩展,当我们需要增加产品时,就需要去修改基类,违反开闭原则。