简单工厂模式(Simple Factory)

类图

描述

简单工厂:

一个抽象产品类,可以派生多个具体产品类;
一个具体工厂类;
工厂只能创建一个具体产品。

应用场景

汽车接口

    public interface ICar
{
void Print();
}

汽车类

    public class Audi : ICar
{
public void Print()
{
Console.WriteLine("这是一辆奥迪车.");
}
} public class Benz : ICar
{
public void Print()
{
Console.WriteLine("这是一辆奔驰车.");
}
}

汽车工厂类

    public class CarFactory
{
public string name { get; set; } /// <summary>
/// 生产汽车
/// </summary>
/// <returns></returns>
public ICar CreateCar()
{
ICar car = null;
switch (name.ToLower())
{
case "audi":
car = new Audi();
break;
case "benz":
car = new Benz();
break;
}
return car;
}
}

调用,从配置文件中读取操作符

            string value = ConfigurationManager.AppSettings["simpleFactoryPattern"];
CarFactory factory = new CarFactory();
factory.name = value;
ICar car = factory.CreateCar();
car.Print();

设计模式之笔记--简单工厂模式(Simple Factory)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. 设计模式学习之简单工厂(Simple Factory,创建型模式)(1)

    简单工厂(Simple Factory,创建型模式) 第一步: 比如我们要采集苹果和香蕉,那么我们需要创建一个Apple类和Banana类,里面各自有采集方法get(),然后通过main方法进行调用, ...

  8. 设计模式在cocos2d-x中的使用--简单工厂模式(Simple Factory)

    什么是简单工厂模式? 从设计模式的类型上来说,简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式.通过专门定义一个类来负责创建其它类的实例,被创建的实例 ...

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

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

随机推荐

  1. 问题 A: Least Common Multiple

    题目描述 The least common multiple (LCM) of a set of positive integers is the smallest positive integer ...

  2. 时间动态协同过滤(TimeSVD++)

    原作者 原论文地址 http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.379.1951&rep=rep1&type=pd ...

  3. Ubuntu 常见错误及解决方法——长期不定时更新

    1. 修复 /etc/sudoers 文件损坏导致不能使用 sudo 命令 这是之前错误地编辑了 /etc/sudoers 这个文件导致的,因此撤销编辑即可,但由于已经不能使用 sudo 命令,因此不 ...

  4. 多文件上传 input 的multiple 属性

    一.上传多张图片并且预览 HTML: <div class="container"> <label>请选择一个图像文件:</label> < ...

  5. gitbook.explore更新升级了, 不能再搜索了

    www.gitbook.com/explore 不再是一个索引页面 Can I browse existing projects on GitBook ? The new version of Git ...

  6. MapReduce 并行编程理论基础

    对于mapreduce这一并行计算模型,一直以来都不是很清楚其具体的执行细节,今天看了学院一位老师的实验指导书,对这一过程有了一个初步的理解,特别是map阶段和reduce阶段,所以做了一份笔记,现在 ...

  7. [洛谷P2384]最短路

    题目大意:给你一个图,要你求出其中1->n路径中乘积最小的一条路 题解:用$log_2$把乘法变成加法,然后记录每个点的前驱,最后求出答案 C++ Code: #include<cstdi ...

  8. axios超时重发

    axios的超时是在response中处理的,所以要在response中添加拦截器: axios.interceptors.response.use(undefined, function axios ...

  9. CentOS 安装 debuginfo-install

    安装debuginfo相关的包步骤如下: 1. 修改文件/etc/yum.repos.d/CentOS-Debuginfo.repo中的enabled参数,将其值修改为1 2. 使用命令: yum i ...

  10. Spring学习--泛型依赖注入

    暂时没有搞懂.