Csharp/C#教程:浅谈C#设计模式之工厂模式分享

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

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

代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceDesignModel
{
   publicinterfaceFactory
   {
       JScreatejs();
   }
   publicclassJS
   {
       publicintNumA{get;set;}
       publicintNumB{get;set;}
       publicvirtualintGetResult()
       {
           return0;
       }
   }
   publicclassAdd1:JS
   {
       publicoverrideintGetResult()
       {
           returnNumA+NumB;
       }
   }
   publicclassSub1:JS
   {
       publicoverrideintGetResult()
       {
           returnNumA-NumB;
       }
   }
   publicclassAddFactory:Factory
   {
       publicJScreatejs()
       {
           returnnewAdd1();
       }
   }
   publicclassSubFactory:Factory
   {
       publicJScreatejs()
       {
           returnnewSub1();
       }
   }
}

客户端调用:

代码如下:
 Factoryfactory=newAddFactory();
           JS js=factory.createjs();
           js.NumA=1;
           js.NumB=2;
           Console.WriteLine(js.GetResult());
           Factoryf=newSubFactory();
           JSJ=f.createjs();
           J.NumA=9;
           J.NumB=0;
           Console.WriteLine(J.GetResult());
           Console.ReadLine();

这里主要是对比了下和简单工厂模式的区别,记录下来,以防自己搞混。

您可能感兴趣的文章:C#设计模式之单例模式实例讲解c#设计模式适配器模式详细介绍C#设计模式之观察者模式实例讲解实例解析C#设计模式编程中简单工厂模式的使用C#设计模式之外观模式介绍浅谈c#设计模式之单一原则浅谈C#设计模式之代理模式举例讲解C#编程中对设计模式中的单例模式的运用C#中利用代理实现观察者设计模式详解C#设计模式之行为型模式详解

标签: 工厂模式 设计模式

C语言自增(++)和自减(–)实例详解

C 语言简单加减乘除运算

上述就是C#学习教程:浅谈C#设计模式之工厂模式分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/905942.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐