标签:rgs 封装 jpg 系统 art 抽象工厂 工厂模式 line create
定义了一个创建对象的接口, 但由子类决定要实例化的类是哪一个. 工厂方法让类把实例化推迟到子类
1、抽象工厂角色:这是工厂方法模式的核心,它与应用程序无关。是具体工厂角色必须实现的接口或者必须继承的父类。
2、具体工厂角色:它含有和具体业务逻辑有关的代码。由应用程序调用以创建对应的具体产品的对象。
3、抽象产品角色:它是具体产品继承的父类或者是实现的接口。
4、具体产品角色:具体工厂角色所创建的对象就是此角色的实例。
namespace DesignPatterns.FactoryMethod { class Program { static void Main(string[] args) { Console.WriteLine("使用工厂方法进行计算"); Factory factory1 = new AddFactory(); Operation operation1 = factory1.CreateOperation(); operation1.numberA = 10; operation1.numberB = 20; Console.WriteLine("{0}+{1}={2}", operation1.numberA, operation1.numberB, operation1.GetResult()); Factory factory2 = new SubFactory(); Operation operation2 = factory2.CreateOperation(); operation2.numberA = 10; operation2.numberB = 20; Console.WriteLine("{0}-{1}={2}", operation2.numberA, operation2.numberB, operation2.GetResult()); Console.Read(); } } /// <summary> /// 抽象操作类 /// </summary> public abstract class Operation { public int numberA; public int numberB; public abstract int GetResult(); } /// <summary> /// 加法操作 /// </summary> public class AddOperation : Operation { public override int GetResult() { return (this.numberA + this.numberB); } } /// <summary> /// 减法操作 /// </summary> public class SubOperation : Operation { public override int GetResult() { return (this.numberA - this.numberB); } } public abstract class Factory { public abstract Operation CreateOperation(); } public class AddFactory : Factory { public override Operation CreateOperation() { return new AddOperation(); } } public class SubFactory : Factory { public override Operation CreateOperation() { return new SubOperation(); } } }
优点:
缺点:
参考:
http://www.cnblogs.com/yinxiangpei/articles/2366092.html
http://www.cnblogs.com/ywqu/archive/2010/01/07/1640855.html
http://www.cnblogs.com/Terrylee/archive/2006/01/04/310716.html
标签:rgs 封装 jpg 系统 art 抽象工厂 工厂模式 line create
原文地址:http://www.cnblogs.com/xcsn/p/7337024.html