美文网首页
1.简单工厂模式

1.简单工厂模式

作者: 卡布萨岛 | 来源:发表于2018-10-19 00:17 被阅读0次

原理

这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

意图

定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。

主要解决

主要解决接口选择的问题。

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 简单工厂模式
{
    class Program
    {
        static void Main(string[] args)
        {
            Operation oper;
            oper = OperationFactory.createOperate("+");
            oper.NumberA = 1;
            oper.NumberB = 2;
            double result = oper.GetResult();
            Console.Write("result: " + result);
            Console.ReadKey();
        }
    }
}

Operation.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 简单工厂模式
{
    //简单工厂类
    class OperationFactory
    {
        public static Operation createOperate(string operate)
        {
            Operation oper = null;
            switch (operate)
            {
                case "+":   oper = new OperationAdd();
                    break;
                case "-":   oper = new OperationSub();
                    break;
                case "*":   oper = new OperationMul();
                    break;
                case "/":   oper = new OperationDiv();
                    break;
            }
            return oper;
        }
    }

    //运算类
    class Operation
    {
        private double _numberA = 0;
        private double _numberB = 0;

        public double NumberA {
            get { return _numberA; }
            set { _numberA = value; }
        }
        public double NumberB
        {
            get { return _numberB; }
            set { _numberB = value; }
        }
        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
    }
    //加法类
    class OperationAdd : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA + NumberB;
            return result;
        }
    }
    //减法类
    class OperationSub : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA - NumberB;
            return result;
        }
    }
    //乘法类
    class OperationMul : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA * NumberB;
            return result;
        }
    }
    //除法类
    class OperationDiv : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            if (NumberB == 0) {
                throw new Exception("除数不能为0."); 
            }
            result = NumberA / NumberB;
            return result;
        }
    }

}

相关文章

  • Java设计模式——工厂模式

    工厂模式简单工厂模式工厂方法模式抽象工厂模式 1.简单工厂模式 1.基本介绍1)简单工厂模式也叫静态工厂模式,是属...

  • iOS设计模式-工厂模式

    工厂模式分为简单工厂模式,工厂模式、抽象工厂模式三类。 简单工厂模式 1. 定义 简单工厂模式并不是常用的设计模式...

  • 工厂模式

    分类 简单工厂模式 工厂方法模式 抽象工厂模式 1. 简单工厂模式 简单工厂模式其实就是只有一个工厂,根据传进来参...

  • 三、创建型模型

    简单工厂模式 工厂方法模式 抽象工厂模式 单例模式 1.简单工厂模式 简单工厂模式并不属于23种设计模式。 不难看...

  • 设计模式之工厂模式

    设计模式之工厂模式 1. 简单工厂模式 1.1 模式定义 简单工厂模式又称为静态工厂方法,它属于创建型模式。在简单...

  • php设计模式之工厂模式

    1. 工厂模式分类 工厂模式具体可分为三类模式:简单工厂模式,工厂方法模式,抽象工厂模式; 2. 简单工厂模式 又...

  • 工厂方法模式

    1.简单工厂模式 先看一下简单工厂模式 代码: 2.工厂方法模式 简单工厂模式大家比较熟悉也比较简单,但是简单工厂...

  • 创建相关的设计模式

    简单工厂模式 工厂模式 工厂生产行为交由子类去实现 抽象工厂模式 三种工厂模式的缺点:1.简单工厂:工厂类需要实现...

  • 设计模式之工厂模式

    1.简单工厂模式 2.抽象工厂模式

  • Java中的工厂模式

    1.简单工厂模式 2.常规工厂模式

网友评论

      本文标题:1.简单工厂模式

      本文链接:https://www.haomeiwen.com/subject/impezftx.html