日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 设计模式,工厂方法

發布時間:2023/12/2 C# 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 设计模式,工厂方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C#工廠方法

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 工廠方法 { 8 class Program { 9 static void Main(string[] args) { 10 IFacotry i = new FactoryAdd(); 11 Operation op = i.CreateOperation(); 12 op.Number1 = 10; 13 op.Number2 = 20; 14 Console.WriteLine(op.getResult()); 15 Console.ReadKey(); 16 } 17 } 18 class Operation { //父類,封裝 19 protected int number1; 20 protected int number2; 21 22 public int Number1 { 23 get { return number1; } 24 set { number1 = value; } 25 } 26 public int Number2 { 27 get { return number2; } 28 set { number2 = value; } 29 } 30 public virtual int getResult() { 31 int result = 0; 32 return result; 33 } 34 } 35 class OperationAdd : Operation { 36 public override int getResult() { 37 return number1 + number2; 38 } 39 } 40 class OperationSub : Operation { 41 public override int getResult() { 42 return number1 - number2; 43 } 44 } 45 interface IFacotry { 46 Operation CreateOperation(); 47 } 48 class FactoryAdd:IFacotry { 49 public Operation CreateOperation() { 50 return new OperationAdd(); 51 } 52 } 53 class FacotrySub : IFacotry { 54 public Operation CreateOperation() { 55 return new OperationSub(); 56 } 57 } 58 } View Code

與簡單工廠不同的是,在計算乘,除,不需要修改代碼,而是直接在外部添加代碼

簡單工廠:類方法中實例化對象,通過父類進行指向,每一個子類中完成自己獨立的運算,根據多態調用正確的方法。

工廠方法:簡單工廠中的方法創建實例部分給抽象成各個類的方法,不同的類用來創建不同的實例對象。

轉載于:https://www.cnblogs.com/zgrh/p/11115140.html

總結

以上是生活随笔為你收集整理的C# 设计模式,工厂方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。