创建型模式-简单工厂模式
2011-03-04 22:56
274 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 简单工厂模式 { public class SimpleFactory { //将此函数改为静态的则变为了静态工厂方法 public IProduct CreateProduct(string productName) { switch (productName) { case "ProductA": return new ProductA(); case "ProdcutB": return new ProductB(); default: throw new Exception("创建产品错误!"); } } } public interface IProduct { void MustDoSomething(); } public class ProductA : IProduct { public ProductA() { } public void MustDoSomething() { //产品A的业务逻辑 } } public class ProductB : IProduct { public ProductB() { } public void MustDoSomething() { //产品B的业务逻辑 } } public class AppClient { public static void Main(string[] args) { SimpleFactory afactory = new SimpleFactory(); IProduct producta = afactory.CreateProduct("ProductA"); producta.MustDoSomething();//调用产品A的方法 IProduct productb = afactory.CreateProduct("ProdcutB"); productb.MustDoSomething();//将调用产品B的方法 } } }
相关文章推荐
- 创建型模式之简单工厂模式(绘制图形)
- (1)设计模式之简单工厂模式(创建型模式)
- 创建型模式之简单工厂模式(java版)
- 第三课 创建型模式 第一节 简单工厂模式
- 创建型模式--简单工厂模式(Simple Factory)
- java设计模式(创建型)之简单工厂模式
- 为什么需要创建型模式以及简单工厂模式(三)
- 设计模式(创建型)之简单工厂模式(Simple Factory Pattern)
- JAVA设计模式(01):创建型-工厂模式【简单工厂模式】(Simple Factory)
- (创建型模式)FACTORY——简单工厂模式
- 简单工厂模式-创建型模式1
- 设计模式(四):SIMPLE FACTORY简单工厂模式 -- 创建型模式
- 设计模式1——创建型模式之简单工厂模式
- 创建型模式—简单工厂模式
- 创建型模式之一——简单工厂模式
- 创建型模式之简单工厂模式
- 简单工厂模式(Simple Factory Pattern(静态工厂方法模式)对象创建型模式)
- PYTHON设计模式,创建型之简单工厂模式
- 七种创建型模式之一: 简单工厂模式(Simple Factory)
- JAVA设计模式(01):创建型-工厂模式【简单工厂模式】(Simple Factory)