原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/

Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】

This article explains why and how to use the Simple Factory Design Pattern in software development.
这篇文章解释了在软件开发中为什么使用,以及怎么使用简单工厂模式。

I am here to continue the discussion of Design Patterns. Today we will explain another creational design pattern called Simple Factory.

我在这继续来讨论设计模式。今天我将会解释另一个创造性的设计模式,也就是简单工厂模式。

In case you have not had a look at our previous articles, go through the following link:

假设你没有看我之前的文章,请先回去看,下面是链接:

Before talking about its implementation let's begin with some fundamental questions as in the following.

在讨论如何实现简单工厂模式之前,先看下下面一些基本的问题:

Purpose of the Factory pattern【工厂模式的目的】

I can think of two main objectives of using the Factory pattern. One is to achieve loose coupling between the client and business layer, another is to keep all the code for all the object instantiation logic in one place.

我能想到使用工厂模式的两个主要的目的。一个是在客户端和业务层之间达到松耦合,另外一个是确保所有对象的实例化的逻辑代码都在一个位置。

Purpose of loose coupling【松耦合的目的】

In modern software development where changes in existing systems are frequent and software design is expected to be scalable, not having a loosely-coupled design can create many problems.

在现代软件开发的过程中,需求的变更是很频繁的,所以软件的设计应该是要可扩展性好的,没有一个松耦合的设计,可能会有很多问题。

For example, in an application with a 3-layer architecture, if object creation logic is at the client side, for any new addition of concrete classes, the developer needs to modify not just the business but the client layer as well. Think about the maintainability and added testing effort.

例如,在一个简单三层框架的项目中,如果对象的创建是在客户端,那么任何新添加的具体的实体类,开发者,不仅需要去修改业务层还有客户端层。为了考虑可维护性,还需要添加测试的工作。

How about if the client is only aware of the high-level contracts and not about its concreate implementation?

如果客户端只需要关心和高一层次的关系,而不用关心具体的实现呢?

Yes, you got it right! The client just must pass the type of the object it needs and it will get it using the Factory Pattern.

Enough theory. Now let's talk about implementation.

是的,你是对的!客户端仅仅只需要传递对象需要的类型,剩下的就交给工厂模式去做。理论已经足够了,现在我们来讨论一下,如何实现简单工厂模式吧。

How to use the Simple Factory Pattern【怎么样来使用简单工厂模式】

Let's try to understand using a simple example.

Assume the client wants to know the on-road price of various brands of cars and have an interface as in the following.

我们使用一个简单的例子,来理解简单工厂模式吧。假设客户想要知道路上各种品牌汽车的价格,提供了下面一个这样的接口。

我们创建一个控制台程序,来学习简单工厂模式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
public interface ICar
{
/// <summary>
/// 获取汽车价格
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
string GetOnRoadPrice(string model);
}
}

We need to create a Factory class now that will sit between the client and business layers and it will provide the required object to the client based on the car brand passed.

我们现在需要去创建一个工厂类,这个工厂类位于客户端和业务层之间,并基于传递的汽车品牌,提供客户端需要的对象。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
public class CarFactory
{
/// <summary>
/// 获取汽车对象
/// </summary>
/// <param name="carBrand">汽车品牌</param>
/// <returns></returns>
public static ICar GetCar(string carBrand)
{
if (carBrand.ToLowerInvariant() == "baoma")
{
return new BaoMa();
}
else if (carBrand.ToLowerInvariant() == "benchi")
{
return new BenChi();
}
else if (carBrand.ToLowerInvariant() == "aodi")
{
return new AoDi();
}
else
{
return null;
}
}
}
}

And here goes the concreate business classes.

这里接着创建具体的实体类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class AoDi:ICar
{ public string GetOnRoadPrice(string model)
{
if (model.ToLowerInvariant() == "aodi")
{
return "300w 人民币";
}
else
{
return "你输入的汽车品牌找不到,请重新输入!!!";
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class BaoMa:ICar
{
public string GetOnRoadPrice(string model)
{
if (model.ToLowerInvariant() == "baoma")
{
return "200w 人民币";
}
else
{
return "你输入的汽车品牌找不到,请重新输入!!!";
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class BenChi:ICar
{
public string GetOnRoadPrice(string model)
{
if (model.ToLowerInvariant() == "glc")
{
return "550w 人民币";
}
else
{
return "你输入的汽车品牌找不到,请重新输入!!!";
}
}
}
}

Now let's see how the client can use the setup we have created so far.

现在,我们看看客户端怎么使用我们目前为止创建的对象。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//设置控制台标题栏中显示的标题
Console.Title = "简单工厂模式Demo学习";
ICar car = null;
string model = null; //奔驰车测试
car= CarFactory.GetCar("BenChi");
model = "glc";
Console.WriteLine("奔驰系列{0}的汽车,售价为{1}",model,car.GetOnRoadPrice(model));
Console.ReadKey();
}
}
}

And here goes the class diagram.
这是类图:

As you can see in the preceding code, the client is getting the required object by just passing the car brand. And then it calls the GetOnRoadPrice method to get the On-road price by passing model name. So just to summarize, using simple factory, we achieved the following.
上面的代码中你可以看到,客户端获取需要的对象,仅仅通过传递汽车的品牌就可以了。然后传递model name调用GetOnRoadPrice 方法来获取价格。所以总结一下,使用简单工厂模式,我们达到了下面的目的。
  • Loose coupling between client and business layers.【客户端和业务层之间的松耦合。】
  • Placed object creation logic at common place.【把对象的创建逻辑,放在了一个公共的地方。】
  • Abstracted concreate classes (Maruti, Hyundai) from client.【客户端的类抽象化】

I hope you have liked this article. I look forward to your comments/suggestions.

我希望你喜欢这篇文章,期待你的评论和建议。

 

Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】的更多相关文章

  1. Design Patterns Simplified - Part 2 (Singleton)【设计模式简述--第二部分(单例模式)】

    原文链接: http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part-2-singleton/ De ...

  2. Design Patterns | 01 为什么要尽早掌握设计模式

    标题:Design Patterns | 01 为什么要尽早掌握设计模式 链接: 标签:设计模式 摘要:设计模式是前人经验的总结,教大家如何写出可扩展.可读.可维护的高质量代码.设计模式与日常工作中的 ...

  3. Java 设计模式系列(三)抽象工厂

    Java 设计模式系列(三)抽象工厂 每天用心记录一点点.内容也许不重要,但习惯很重要!

  4. Java 设计模式系列(二)简单工厂模式和工厂方法模式

    Java 设计模式系列(二)简单工厂模式和工厂方法模式 实现了创建者和调用者的分离.分为:简单工厂模式.工厂方法模式.抽象工厂模式 简单工厂模式.工厂方法模式都很简单,就不详细介绍了. 一.简单工厂 ...

  5. 设计模式(C#)——02简单工厂模式

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321       工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来.通俗来说,你只关心怎么用,不用关心怎么做 ...

  6. 学习设计模式第二十七 - GoF之外简单工厂模式

    示例代码来自<深入浅出设计模式>和<大话设计模式> 概述 简单工厂模式又被称为静态工厂模式,属于类的创建型模式.其实质是由一个工厂类根据传入的参量,动态决定应该创建出哪一个产品 ...

  7. Javascript设计模式理论与实战:简单工厂模式

    通常我们创建对象最常规的方法就是使用new关键字调用构造函数,这会导致对象之间的依赖性.工厂模式是一种有助于消除类之间依赖性的设计模式,它使用一个方法来决定要实例化哪一个类.本文详细介绍了简单工厂模式 ...

  8. 设计模式(二)——Java简单工厂模式

    简单工厂模式 案例: 披萨的项目(要便于披萨种类的扩展,要便于维护) 1)披萨的种类很多(比如 GreekPizz.CheesePizz 等) 2)披萨的制作有 prepare,bake, cut, ...

  9. [Python设计模式] 第1章 计算器——简单工厂模式

    github地址:https://github.com/cheesezh/python_design_patterns 写在前面的话 """ 读书的时候上过<设计模 ...

随机推荐

  1. 和 Thrift 的一场美丽邂逅

    一. 与 Thrift 的初识 也许大多数人接触 Thrift 是从序列化开始的.每次搜索 “java序列化” + “方式”.“对比” 或 “性能” 等关键字时,搜索引擎总是会返回一大堆有关各种序列化 ...

  2. Python(九) Python 操作 MySQL 之 pysql 与 SQLAchemy

    本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...

  3. Web开发安全之文件上传安全

    很长一段时间像我这种菜鸡搞一个网站第一时间反应就是找上传,找上传.借此机会把文件上传的安全问题总结一下. 首先看一下DVWA给出的Impossible级别的完整代码: <?php if( iss ...

  4. 编译器开发系列--Ocelot语言4.类型定义的检查

    这里主要介绍一下检查循环定义的结构体.联合体.是对成员中包含自己本身的结构体.联合体进行检查.所谓"成员中包含自己本身",举例来说,就是指下面这样的定义. struct point ...

  5. linux系统维护时的一些小技巧,包括系统挂载新磁盘的方法!可收藏!

    这里发布一些平时所用到的小技巧,不多,不过会持续更新.... 1.需要将history创建硬链接ln 全盘需要备份硬链接 ln /etc/xxx /home/xxx 2.root用户不可以远程 /et ...

  6. 嵌入式C语言代码的调试技巧

    在项目开发的过程中,不可避免的会遇到调试代码的情况. 刚开始写代码时,我们想看具体执行到哪儿时,往往这么写: printf("***** Code is here! *****\n" ...

  7. BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4026  Solved: 1473[Submit] ...

  8. js动态加载css和js

    之前写了一个工具类点此链接里面含有这段代码,感觉用处挺多,特意提出来 var loadUtil = { /* * 方法说明:[动态加载js文件css文件] * 使用方法:loadUtil.loadjs ...

  9. Linux学习日记-WCF RestFul的部署(三)

    一.关于WCF 的部署 默认的wshttp风格的wcf是很容易部署上去的,但是这里给个建议尽量不要使用WCF的配置文件去部署尽管 我们都已经很熟悉了,在使用配置文件你会发现各种蛋疼的问题. 二.WCF ...

  10. 【Win10】UAP/UWP/通用 开发之 x:Bind

    [Some information relates to pre-released product which may be substantially modified before it's co ...