C#命令模式用法实例
作者:程序猴 时间:2021-10-21 12:46:02
本文实例讲述了C#命令模式。分享给大家供大家参考。具体实现方法如下:
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#程序设计有所帮助。
标签:C#,模式
0
投稿
猜你喜欢
如何把VS Code打造成Java开发IDE
2021-09-16 16:37:36
java实现小球碰撞功能
2023-04-05 19:22:41
DecimalFormat多种用法详解
2022-11-13 15:06:52
startActivityForResult和setResult案例详解
2023-09-15 19:13:33
SpringBoot添加自定义拦截器的实现代码
2023-11-26 13:30:54
Java 反射调用静态方法的简单实例
2021-10-09 06:47:58
Java使用pulsar-flink-connector读取pulsar catalog元数据代码剖析
2023-11-05 17:25:41
剑指Offer之Java算法习题精讲链表专项训练
2023-11-29 16:31:48
java根据网络地址保存图片的方法
2021-09-01 18:37:02
spring webflux自定义netty 参数解析
2023-07-26 18:38:25
分布式系统下调用链追踪技术面试题
2023-11-25 05:55:00
springboot整合token的实现代码
2023-11-10 19:02:03
Spring使用三级缓存解决循环依赖的问题
2023-03-14 09:06:15
netty pipeline中的inbound和outbound事件传播分析
2023-08-27 06:57:00
Spring Cloud Eureka(全面解析) 大白话
2022-11-12 22:43:02
Java中对list map根据map某个key值进行排序的方法
2023-09-04 17:10:03
C#预定义的基础类型转换
2023-08-13 03:14:16
jar包手动添加到本地maven仓库的步骤详解
2023-11-23 05:09:37
Java聊天室之实现运行服务器与等待客户端连接
2023-11-23 08:16:17
Java Bean 作用域及它的几种类型介绍
2022-12-02 20:39:42