设计模式 - Factory 模式
来源:互联网 发布:软件进口销售 编辑:程序博客网 时间:2024/05/20 16:36
1 问题
1 为了提高内聚(Cohesion)和松耦合(Coupling),我们经常会抽象出一些类的公共 接口以形成抽象基类或者接口。 2 父类中并不知道具体要实例化哪一个具体的子类。
2 功能
1 定义创建对象的接口,封装了对象的创建; 2 使得具体化类的工作延迟到了子类中。
3 代码
不用关心MonoBehaviout,只是在unity引擎的脚本验证的该模式,Start()方法可以看做main()函数
using UnityEngine;using System.Collections;//产品原型public class Product{ public virtual void say(){ Debug.Log("Basic Product"); }}//具体产品public class ConcreteProduct : Product{ public override void say(){ Debug.Log("Concrete Product"); }}//工厂原型public abstract class Factory{ public abstract Product createProduct();}//具体工厂public class ConcreteFactory : Factory{ public override Product createProduct(){ return new ConcreteProduct(); }}public class FactoryDemo : MonoBehaviour { //工厂模式 void Start(){ Factory factory = new ConcreteFactory(); factory.createProduct().say(); }}
4 总结
没感觉有没感觉有多方便,多高端。。可能因为年轻
0 0
- 设计模式 - Factory 模式
- [设计模式]Factory模式
- 设计模式------Factory 模式
- 设计模式:Factory模式
- 设计模式--Factory模式
- 设计模式-Factory模式戏说
- 设计模式之Factory
- 设计模式之Factory
- 设计模式之Factory
- 设计模式之Factory
- 设计模式之Factory
- Factory Method设计模式
- 设计模式之Factory
- 设计模式之Factory
- 设计模式之Factory
- 设计模式之Factory
- SharpDevelop设计模式(Factory)
- 设计模式之Factory
- JavaScript函数调用及嵌套
- 自定义RatingBar
- src与href属性的区别
- JavaScript是如何做到单线程异步的
- 职群教育 | IOS开发 Swift语言入门(一):简单值
- 设计模式 - Factory 模式
- dom4j学习总结(一)
- EhCache 分布式缓存/缓存集群
- 嵌入式 SIGBUS和SIGSEGV详解
- mysql-5.7.10-winx64免安装版配置时碰到的问题
- C#设计模式——适配器模式
- Linux 启动grub error 24: Attempt to access block outside partition 解决
- ble广播包解析
- 【HTML5入门教程二】HTML5 视频属性的技巧分享