1. 简单工厂模式简介

1.1 定义

  简单工厂模式:定义一个Factory类,可以根据参数的不同返回不同类的实例,被创建的实例通常有共同的父类。

  简单工厂模式:只需要一个Factory类。

  简单工厂模式:又称为静态工厂模式(Static Factory Pattern),Factory类为静态类或包含静态方法

  简单工厂模式:不属于23种GOF设计模式。

  简单工厂模式:实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类实例。

1.2 使用频率

   中

2. 简单工厂模式结构

2.1 结构图

2.2 参与者

  简单工厂模式参与者:

  ◊ Product:抽象产品类,将具体产品类公共的代码进行抽象和提取后封装在一个抽象产品类中。

  ◊ ConcreteProduct:具体产品类,将需要创建的各种不同产品对象的相关代码封装到具体产品类中。

  ◊ Factory:工厂类,提供一个工厂类用于创建各种产品,在工厂类中提供一个创建产品的工厂方法,该方法可以根据所传入参数的不同创建不同的具体产品对象。

  ◊ Client:客户端类,只需调用工厂类的工厂方法并传入相应的参数即可得到一个产品对象。

3. 简单工厂模式结构实现

3.1 Product类抽象实现

  Product.cs

using System;
using System.Collections.Generic;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Structural
{
public abstract class Product
{
}
}

  ConcreteProduct.cs

using System;
using System.Collections.Generic;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Structural
{
public class ConcreteProduct : Product
{
}
}

  Factory.cs

using System;
using System.Collections.Generic;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Structural
{
public class Factory
{
/// <summary>
/// 静态方法创建Product实例
/// </summary>
public static Product CreateProduct()
{
return new ConcreteProduct();
}
}
}

  Program.cs

using System;
using System.Collections.Generic;
using System.Text; using DesignPatterns.SimpleFactoryPattern.Structural; namespace DesignPatterns.SimpleFactoryPattern
{
class Program
{
static void Main(string[] args)
{
Product product = Factory.CreateProduct();
Console.WriteLine("Created {0}", product.GetType().Name);
}
}
}

  运行结果:

Created ConcreteProduct
请按任意键继续. . .

3.2 Product接口类实现

  IProduct.cs

using System;
using System.Collections.Generic;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation
{
public interface IProduct
{
void Display();
}
}

  Product.cs

using System;
using System.Collections.Generic;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation
{
public class Product : IProduct
{
public void Display()
{
Console.WriteLine("DesignPatterns.SimpleFactoryPattern.Structural.Product");
}
}
}

  Factory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Reflection; namespace DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation
{
public class Factory
{
/// <summary>
/// Factory返回IProduct的静态方法
/// </summary>
/// <returns></returns>
public static IProduct Create()
{
// 使用new直接创建接口的具体类
//return new DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation.Product(); // 通过映射创建接口的具体类
return (IProduct)Assembly.Load("DesignPatterns.SimpleFactoryPattern").CreateInstance("DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation.Product");
}
}
}

  Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using DesignPatterns.SimpleFactoryPattern.StructuralInterfaceImplementation; namespace DesignPatterns.SimpleFactoryPattern
{
class Program
{
static void Main(string[] args)
{
IProduct product = Factory.Create();
product.Display();
}
}
}

4. 简单工厂模式实践应用

4.1 实践应用——运算操作

  Operation.cs

using System;
using System.Collections.Generic;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical
{
/// <summary>
/// 运算类
/// </summary>
public abstract class Operation
{
public double NumberA { get; set; }
public double NumberB { get; set; } public virtual double GetResult()
{
const double result = ;
return result;
}
}
}

  Plus.cs

using System;
using System.Collections.Generic;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical
{
/// <summary>
/// 加法运算
/// </summary>
public class Plus : Operation
{
public override double GetResult()
{
return NumberA + NumberB;
}
}
}

  Minus.cs

using System;
using System.Collections.Generic;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical
{
/// <summary>
/// 减法运算
/// </summary>
public class Minus : Operation
{
public override double GetResult()
{
return NumberA - NumberB;
}
}
}

  Multiply.cs

using System;
using System.Collections.Generic;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical
{
public class Multiply : Operation
{
public override double GetResult()
{
return NumberA * NumberB;
}
}
}

  Divide.cs

using System;
using System.Collections.Generic;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical
{
public class Divide :Operation
{
public override double GetResult()
{
return NumberA / NumberB;
}
}
}

  OperationFactory.cs

using System;
using System.Collections.Generic;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical
{
public class OperationFactory
{
public static Operation CreateOperate(string operate)
{
Operation operation = null; switch (operate)
{
case "+":
operation = new Plus();
break;
case "-":
operation = new Minus();
break;
case "*":
operation = new Multiply();
break;
case "/":
operation = new Divide();
break;
} return operation;
}
}
}

  Program.cs

using System;
using System.Collections.Generic;
using System.Text; using DesignPatterns.SimpleFactoryPattern.Practical; namespace DesignPatterns.SimpleFactoryPattern
{
class Program
{
static void Main(string[] args)
{
Operation operateion = OperationFactory.CreateOperate("+");
operateion.NumberA = ;
operateion.NumberB = ; Console.WriteLine(operateion.GetResult());
}
}
}

4.2 实践应用——银行支付接口

  IPayment.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical
{
public interface IPayment
{
bool Payfor(decimal money);
}
}

  ABCPayment.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical
{
public class ABCPayment : IPayment
{
public bool Payfor(decimal money)
{
// 调用中国农业银行支付接口进行支付
return true;
}
}
}

  ICBCPayment.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical
{
public class ICBCPayment : IPayment
{
public bool Payfor(decimal money)
{
// 调用中国工商银行支付接口进行支付
return true;
}
}
}

  PaymentFactory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical
{
public class PaymentFactory
{
public static IPayment CreatePayment(string bank)
{
IPayment payment = null;
switch (bank)
{
case "ABC":
payment = new ABCPayment();
break;
case "ICBC":
payment = new ICBCPayment();
break;
} return payment;
}
}
}

  OrderService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DesignPatterns.SimpleFactoryPattern.Practical
{
public class OrderService
{
public bool CreateOrder(string bank)
{
decimal money=100m;
var payment = PaymentFactory.CreatePayment(bank); return payment.Payfor(money);
}
}
}

  在OrderService类中,不依赖具体的支付类,只通过PaymentFactory来获取真正的支付类。

5. 简单工厂模式应用分析

5.1 简单工厂模式优点

  ◊ 实现创建和使用分离;

  ◊ Client无需知道所创建的ConcreteProduct类名,只需要知道ConcreteProduct所对应的参数。

5.2 简单工厂模式缺点

  ◊ Factory类集中所有ConcreteProduct的创建逻辑,职责过重。一旦需要添加新的ConcreteProduct,则需要修改Factory逻辑。这样违背了OCP(开放-关闭原则)

  ◊ 由于使用了static方法,造成Factory无法形成基于继承的结构。

C#设计模式系列:简单工厂模式(Simple Factory)的更多相关文章

  1. 设计模式之简单工厂模式Simple Factory(四创建型)

    工厂模式简介. 工厂模式专门负责将大量有共同接口的类实例化 工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类. 工厂模式有三种形态: 1.简单工厂模式Simple Factory ...

  2. 【设计模式】简单工厂模式 Simple Factory Pattern

    简单工厂模式Simple Factory Pattern[Simple Factory Pattern]是设计模式里最简单的一个模式,又叫静态工厂模式[Static Factory Pattern], ...

  3. C#设计模式-1简单工厂模式Simple Factory)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 简单的工 ...

  4. 设计模式之简单工厂模式(Simple Factory Pattern)

    一.简单工厂模式的由来 所有设计模式都是为解决某类问题而产生的,那么简单工厂模式是为解决什么问题呢?我们假设有以下业务场景: 在一个学生选课系统中,文科生用户选课时,我们要获得文科生的所有课程列表:理 ...

  5. 设计模式之—简单工厂模式<Simple Factory Pattern >

    简单工厂模式结构图: 简单工厂模式以简单的加减乘除运算为例: 运算符类(Operation): namespace CalcTest.Simple_Factory_patterns { class O ...

  6. 【UE4 设计模式】简单工厂模式 Simple Factory Pattern

    概述 描述 又称为静态工厂方法 一般使用静态方法,根据参数的不同创建不同类的实例 套路 创建抽象产品类 : 创建具体产品类,继承抽象产品类: 创建工厂类,通过静态方法根据传入不同参数从而创建不同具体产 ...

  7. Golang设计模式—简单工厂模式(Simple Factory Pattern)

    Golang设计模式--简单工厂模式 背景 假设我们在做一款小型翻译软件,软件可以将德语.英语.日语都翻译成目标中文,并显示在前端. 思路 我们会有三个具体的语言翻译结构体,或许以后还有更多,但现在分 ...

  8. 创建型模式(前引)简单工厂模式Simple Factory

    一引出的原因(解决下面的问题) 简单工厂模式(Simple Factory Pattern):又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式. 在简单工厂模式 ...

  9. 大白话简单工厂模式 (Simple Factory Pattern)

    大白话简单工厂模式 (Simple Factory Pattern) 从买车经历说起 毕业两年,码农张小两口无法忍受挤公交,凌晨起床抢火车票的痛苦,遂计划买车.逛了多家4S店,最终定下日产某车型的轿车 ...

  10. Net设计模式实例之简单工厂模式(Simple Factory Pattern)

    一.简单工厂模式简介(Bref Introduction) 简单工厂模式(Simple Factory Pattern)的优点是,工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类, ...

随机推荐

  1. java工厂模式

    (1)概念大白话:java工厂模式就是客户端(main函数)要创建对象觉得麻烦就让另外一个叫工厂的类帮它创建,然后自己每次要创建对象就叫工厂帮它弄,举个例子,在没有工厂这个"手下" ...

  2. HDU 4801 Pocket Cube

    题目链接 去年现场,虎哥1Y的,现在刷刷题,找找状态... 一共6种转法,把3个面放到顶部,左旋和右旋,感觉写的还不错....都写成常数了. #include <stdio.h> #inc ...

  3. css 深入浅出定位

    前面我们简单的了解了盒子模型,这里我们就不复习了哈.有什么不清楚的去看我的上一篇博文.其实说定位之前大家一定要先理解一个东西:文档流,那什么是文档流?和文档有关系吗?是dom树吗? 这一对的问题我们应 ...

  4. (转)对博士学位说永别 by 王珢

    对博士学位说永别 by 王垠 经过深思熟虑之后,我决定再次“抛弃”我的博士学位.这是我第三次决定离开博士学位,也应该是最后一次了.这应该不是什么惊人的消息,因为我虽然读博士10年了,可是我的目标从来就 ...

  5. css制作对话框

    当你发现好多图都能用css画出来的时候,你就会觉得css很有魅力了.//我是这么觉得的,先不考虑什么兼容问题 像漫画里出现的对话框,往往都是一个对话框然后就加入一个箭头指向说话的那一方,来表示这个内容 ...

  6. 如何给GridView添加网格

    如何给gridview的单元格加上分割线 原文链接:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/1227/1582.html ...

  7. HTML实践发现(标签<pre>)

    1. (1).第一种编辑: (2).浏览器中显示: 2. (1).第二种编辑 (2).浏览器中显示: 结果发现:使用标签<pre>,在浏览器中显示的结果与在<pre>下方编写的 ...

  8. 最后一周psp

    团队项目PSP 一:表格     C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论用户界面 10:20 11:45 25 40 80 分析 ...

  9. html 5 实现拖放效果

    在html5中要实现拖放操作,相对于以前通过鼠标操作实现,要简单得多,数据安全性也更有保障.只需要以下几步即可. 给被拖拽元素添加draggable属性,如果是文件拖放. 在拖拽元素的dragstar ...

  10. 简单的ATM取款过程

    一个简单的ATM的取款过程是这样的:首先提示用户输入密码(pwd),最多只能输3次,超过三次则提示用户“密码已输入三次错误,请取卡.“结束交易.如果用户密码正确,在提示用户输入金额(money),AT ...