Unity设计模式之工厂模式

简单工厂模式

简单工厂是工厂模式中比较简单何容易理解的一种模式。简单工厂模式其实就是用来创建不同类型的对象的。

代码:

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();
        }
    }

优点:

可以使我们很轻松的就增加了一系列产品的实现。

缺点:

不适合产品的横向扩展,当我们需要增加产品时,就需要去修改基类,违反开闭原则。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值