我们以做一个计算器为例,给大家介绍简单工厂的应用:

效果:

这里我们使用 继承 ,虚方法, 简单工厂的设计模式来完成

首先,我们除了搭好窗体外,我们应该把我们的一些类准备好:

1.计算的父类Calculation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 计算器_简单工厂_
{
//父类(虚) 计算类
public abstract class Calculation
{
//第一个操作数
public double Numleft { get; set; }
//第二个操作数
public double NumRight { get; set; }
//运算符
public string Operator { get; set; }
//虚方法
public abstract double Cal();
}
}

2.然后就是,加,减,乘,除 的子类

01.加法类 Add

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 计算器_简单工厂_
{
//加法
public class Add:Calculation
{
public override double Cal()
{
double result = Numleft + NumRight;
return result;
}
}
}

02.减法类 Sub

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 计算器_简单工厂_
{
//减法
public class Sub:Calculation
{
public override double Cal()
{
double result = Numleft - NumRight;
return result;
}
}
}

03.乘法类 Mul

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 计算器_简单工厂_
{
//减法类
public class Mul:Calculation
{
public override double Cal()
{
double result = Numleft * NumRight;
return result;
}
}
}

04.除法类 Div

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 计算器_简单工厂_
{
//除法类
public class Div:Calculation
{
public override double Cal()
{
//除法中 除数是不能为0
if (NumRight==)
{
throw new Exception("除数不能为0");
}
double result = Numleft / NumRight;
return result;
}
}
}

3.最后就是准备 “简单工厂” 的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 计算器_简单工厂_
{
//工作类
public class Factory
{
public static Calculation Cal(string ope)
{
//初始化父类对象
Calculation calculation = null;
//根据传来的运算符实例化
switch (ope)
{
case"+":
calculation = new Add();
break;
case "-":
calculation = new Sub();
break;
case "*":
calculation = new Mul();
break;
case "/":
calculation = new Div();
break; }
//返回实例化的对象
return calculation;
}
}
}

4.一切准备好后,我们来看看在窗体中是如何用的呐!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 计算器_简单工厂_
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void btnCal_Click(object sender, EventArgs e)
{
//外层try作用:判断输入的操作数是否正确
try
{
//分别获得要计算的值
double num1 = Convert.ToDouble(txtLift.Text);
double num2 = Convert.ToDouble(txtRight.Text);
string ope = cmbstring.Text;
//把获得的运算符传达“简单工厂”
Calculation cal = Factory.Cal(ope);
double result = ; //赋值(最会忘记)
cal.Numleft = num1;
cal.NumRight = num2; //内层try作用:判断当除法时除数是否为0
try
{
//知道需求,开始进行相应工作
result = cal.Cal();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
//显示结果
label1.Text = result.ToString();
}
catch (Exception ex)
{
//如果输入不正确,则系统会提示“输入字符串的格式不正确”
MessageBox.Show(ex.Message); } }
}
}

我们来总结一下我们“简单工厂”类的一些注意点吧:

1.方法是静态的,调用的时候不需要实例化

2.方法的返回值是父类 类型

3.方法是有参的

两种设计模式(1)==>>“简单工厂”的更多相关文章

  1. Spring主要用到两种设计模式

    Spring主要用到两种设计模式 1.工厂模式 Spring容器就是实例化和管理全部Bean的工厂. 工厂模式可以将Java对象的调用者从被调用者的实现逻辑中分离出来. 调用者只关心被调用者必须满足的 ...

  2. Spring 实现两种设计模式:工厂模式和单态模式(单例模式)

    本文摘自:李刚 著 <轻量级 Java EE企业应用实战 Struts2+Spring+hibernate整合开发> 在Spring 中大量使用的以下两种设计模式:工厂模式和单态模式. 工 ...

  3. 两种设计模式和XML解析

    两种设计模式 1.单例模式 模式的保证步骤:单例(是说在一个类中只能有一个对象)三条件 1.1类构造设置私有  private  Play() { } 1.2 定义一个私有的静态的  类类型  变量 ...

  4. Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)

    jquery提供的serialize方法能够实现. $("#searchForm").serialize();但是,观察输出的信息,发现serialize()方法做的是将表单中的数 ...

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

    原文地址:http://www.cnblogs.com/BeyondAnyTime/archive/2012/07/06/2579100.html 今天呢,要学习的设计模式是“简单工厂模式”,这是一个 ...

  6. C#设计模式(2)-简单工厂模式

    引言 上一遍中介绍了设计模式中的单例模式-C#设计模式(1)-单例模式,本篇将介绍简单工厂模式,也是比较容易理解的一种模式: 简单工厂模式简介 什么是简单工厂模式? 定义一个工厂类,它可以根据参数的不 ...

  7. C#设计模式(2)——简单工厂模式(转)

    C#设计模式(2)——简单工厂模式   一.引言 这个系列也是自己对设计模式的一些学习笔记,希望对一些初学设计模式的人有所帮助的,在上一个专题中介绍了单例模式,在这个专题中继续为大家介绍一个比较容易理 ...

  8. iOS设计模式 - (3)简单工厂模式

    iOS设计模式 - (3)简单工厂模式           by Colin丶 转载请注明出处:              http://blog.csdn.net/hitwhylz/article/ ...

  9. PHP设计模式:简单工厂

    示例代码详见https://github.com/52fhy/design_patterns 简单工厂 简单工厂模式的工厂类一般是使用静态方法,通过接收的参数的不同来返回不同的对象实例. 也就是使用的 ...

随机推荐

  1. CI 框架去掉url 中index.php的方法

    1 修改 apache 的 httpd.conf 文件 #LoadModule rewrite_module modules/mod_rewrite.so 去掉前面的# 2 找到 你程序目录下的 .h ...

  2. jetty服务器的安装和部署、新增到开机启动服务

    Jetty的首页地址是http://www.mortbay.org/jetty/,点击Downloads进入下载介绍页面,由于Jetty7之后,托管服务有Eclipse接替,所以jetty6.1之前( ...

  3. SQL1159 Initialization error with DB2 .NET Data Provider, reason code 7(问题补充)

    SQL1159 Initialization error with DB2 .NET Data Provider, reason code 7 需要注册GAC,修改注册表 IBM官方方案: http: ...

  4. angularjs + seajs构建Web Form前端(二)

    回顾 上一篇讲解了引入bootstrap构建一个简单的登录页面,如何让angularjs自动启动并绑定视图,操作过程当中如何使用ui-bootstrap,继而完成简单功能后如何引入seajs后如何使n ...

  5. Go加密解密之RSA[转]

    安全总是很重要的,各个语言对于通用的加密算法都会有实现.前段时间,用Go实现了RSA和DES的加密解密,在这分享一下.(对于RSA和DES加密算法本身,请查阅相关资料) 在PHP中,很多功能经常是一个 ...

  6. JS中style属性

    解决办法:1.先定义一个CSS规则,然后this.className=''2.document.getElementByIdx_x("a").style.cssText=" ...

  7. Android View的加载过程

    大家都知道Android中加载view是从Activity的onCreate方法调用setContentView开始的,那么View的具体加载过程又是怎么的呢?这一节我们做一下分析. 首先追踪一下代码 ...

  8. 超实用的JavaScript技巧及最佳实践(下)

    1.使用逻辑符号&&或者||进行条件判断 1 2 3 var foo = 10;  foo == 10 && doSomething(); // is the same ...

  9. JavaScript 中介者模式与观察者模式有何不同?

    http://addyosmani.com/resources/essentialjsdesignpatterns/book/#detailmvp 感觉二者非常像,都是pub/sub机制,如何进行区分 ...

  10. ES6入门系列一(基础)

    1.let命令 Tips: 块级作用域(只在当前块中有效) 不会变量提升(必须先申明在使用) 让变量独占该块,不再受外部影响 不允许重复声明 总之:let更像我们熟知的静态语言的的变量声明指令 ES6 ...