C#命令模式用法實(shí)例
更新時間:2015年07月02日 16:19:35 作者:程序猴
這篇文章主要介紹了C#命令模式用法,以實(shí)例形式較為詳細(xì)的分析了C#命令模式的功能、定義及使用技巧,需要的朋友可以參考下
本文實(shí)例講述了C#命令模式。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 命令模式 { class Program { static void Main(string[] args) { Receiver r = new Receiver(); Command c = new ConcreteCommand(r); Invoker i = new Invoker(); i.SetCommand(c); i.ExectueCommand(); } public abstract class Command { private Receiver receiver; internal Receiver Receiver { get { return receiver; } set { receiver = value; } } public Command(Receiver receiver) { this.receiver = receiver; } public abstract void Execute(); } public class Receiver { public void Action() { Console.WriteLine("取得receiver的action方法!"); } } public class ConcreteCommand : Command { public ConcreteCommand(Receiver receiver) : base(receiver) { } public override void Execute() { Receiver.Action(); } } public class Invoker { private Command command; internal Command Command { get { return command; } set { command = value; } } public void SetCommand(Command command) { this.command = command; } public void ExectueCommand() { command.Execute(); } } } }
希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C# 文件上傳下載(Excel導(dǎo)入,多線程下載)功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了C# 文件上傳下載(Excel導(dǎo)入,多線程下載)功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-08-08C#條件編譯、內(nèi)聯(lián)函數(shù)、CLS介紹
這篇文章介紹了C#的條件編譯、內(nèi)聯(lián)函數(shù)、CLS,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03C# Directory.GetFiles()函數(shù)案例詳解
這篇文章主要介紹了C# Directory.GetFiles()函數(shù)案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08