zoukankan      html  css  js  c++  java
  • 6.大话设计模式-工厂模式

    工厂模式和简单工厂有什么区别。废话不多说,对比第一篇例子应该很清楚能看出来。

    优点: 工厂模式弥补了简单工厂模式中违背开放-封闭原则,又保持了封装对象创建过程的优点。

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

    namespace DesignModel
    {
        public interface Factory
        {
            JS createjs();
        }

        public class JS
        {
            public int NumA { get; set; }
            public int NumB { get; set; }

            public virtual int GetResult()
            {
                return 0;
            }
        }

        public class Add1 : JS
        {
            public override int GetResult()
            {
                return NumA + NumB;
            }

        }

        public class Sub1 : JS
        {
            public override int GetResult()
            {
                return NumA - NumB;
            }
        }

        public class AddFactory : Factory
        {
            public JS createjs()
            {
                return new Add1();
            }
        }

        public class SubFactory: Factory
        {
            public JS createjs()
            {
                return new Sub1();
            }
        }

    }

    客户端调用:

      Factory factory = new AddFactory();
                JS  js = factory.createjs();
                js.NumA = 1;
                js.NumB = 2;
                Console.WriteLine( js.GetResult());


                Factory f = new SubFactory();
                JS J= f.createjs();
                J.NumA = 9;
                J.NumB = 0;
                Console.WriteLine(J.GetResult());
                Console.ReadLine();

  • 相关阅读:
    03、CPU主频,和性能
    02、计算机组成原理相关知识
    常用正则表达式,手机号、固话号、身份证号等
    01、计算机原理结构,及冯诺依曼体系结构
    7-7 Complete Binary Search Tree (30分) 完全二叉搜索树
    7-2 Reversing Linked List (25分)
    7-1 Maximum Subsequence Sum (25分)
    6-17 Shortest Path [4] (25分)
    6-16 Shortest Path [3] (25分)
    6-15 Iterative Mergesort (25分)
  • 原文地址:https://www.cnblogs.com/zhanjun/p/3861723.html
Copyright © 2011-2022 走看看