C# 设计模式中的命令模式 问题求解~~

peng2739956 2011-03-17 07:18:26
刚刚看完了命令模式 写了个小小的例子 突然发现不改源码能变功能 但是要加功能的话 还得改源码 求解怎么不改源码添加功能
//发送者

using System;
using System.Collections.Generic;
using System.Text;

namespace Command
{
public class Command_Sample01
{
public void Open()
{
Console.WriteLine("播放");
}
public void Close()
{
Console.WriteLine("暂停");
}
}
}

//功能实现

using System;
using System.Collections.Generic;
using System.Text;

namespace Command
{
public class TVcloseCommand:AbstractCommand
{

private Command_Sample01 tv;
public TVcloseCommand()
{
tv = new Command_Sample01();
}
public void Execute()
{
tv.Close();
}


}
}


using System;
using System.Collections.Generic;
using System.Text;

namespace Command
{
public class TvOpenCommand:AbstractCommand
{

#region AbstractCommand 成员
private Command_Sample01 tv;
public TvOpenCommand()
{
tv = new Command_Sample01();
}
public void Execute()
{
tv.Open();
}

#endregion
}
}

//接口

using System;
using System.Collections.Generic;
using System.Text;

namespace Command
{
public interface AbstractCommand
{
void Execute();
}
}

//接收者

using System;
using System.Collections.Generic;
using System.Text;

namespace Command
{
public class Controller
{
private AbstractCommand openCommand, CloseCommand;
public Controller(AbstractCommand openCommand, AbstractCommand CloseCommand)
{
this.openCommand = openCommand;
this.CloseCommand = CloseCommand;
}
public void Open()
{
openCommand.Execute();
}
public void Close()
{
CloseCommand.Execute();
}
}
}


//客户代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Reflection;
namespace Command
{
class Program
{
static void Main(string[] args)
{
AbstractCommand openCommand, CloseCommand;
string open = ConfigurationManager.AppSettings["TvOpenClass"];
openCommand = (AbstractCommand)Assembly.Load("Command").CreateInstance(open);
string Close = ConfigurationManager.AppSettings["TvCloseClass"];
CloseCommand = (AbstractCommand)Assembly.Load("Command").CreateInstance(Close);
//string Control = ConfigurationManager.AppSettings["Conntrol"];
//Controller control = (Controller)Assembly.Load("Command").CreateInstance(Control);
Controller control = new Controller(openCommand, CloseCommand);
control.Open();
control.Close();
Console.Read();
}
}
}

求解 加功能 不修改源码 怎么弄!
...全文
189 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2011-03-17
  • 打赏
  • 举报
回复
将更多的功能放入独立的dll

反射加载,转换成基类(或者接口),调用。

建议使用工厂模式。
cf370697816 2011-03-17
  • 打赏
  • 举报
回复
添加功能 肯定要加代码啊
内容概要:本文介绍了一种用于电磁暂态(EMT)研究的第四类全变流器型风力发电系统的通用Simulink仿真模型,旨在构建一个能够准确反映实际风电系统动态特性的简化通用模型。该模型涵盖了风力机、传动链、发电机、全功率变流器及其控制策略等关键组成部分,重点突出系统在电网故障、风速波动等复杂工况下的动态响应能力,适用于风电并网电磁暂态分析、新型电力系统稳定性评估及高比例可再生能源接入场景的研究。模型设计兼顾准确性与仿真效率,便于研究人员快速搭建和调试,推动风电系统建模与控制技术的发展; 适合人群:具备一定电力系统理论基础和MATLAB/Simulink仿真能力,从事新能源发电、电力电子变换、风电并网控制及相关方向的研究生、科研人员及工程技术人员; 使用场景及目标:①开展风电系统在电网扰动下的电磁暂态仿真分析;②研究全功率变流器风电机组的动态行为与控制特性;③支撑新型电力系统高渗透率风电接入的稳定性与电能质量评估,服务于学术研究、课程教学与工程项目前期仿真验证; 阅读建议:建议读者结合文提供的模型结构与参数设置,在Simulink环境动手复现并调试仿真模型,通过设置不同运行工况(如三相短路、低电压穿越、风速突变等)观察系统响应,深入理解全变流器风电机组的建模方法、控制逻辑与动态特性,进而拓展应用于更复杂的多机并网或综合能源系统仿真场景。
源码下载地址: https://pan.quark.cn/s/d18200fd2664 在基于Ubuntu的系统,针对TCP与UDP网络进行排错对于开发人员及系统维护者而言是日常工作的关键环节。为了能够迅速地识别并解决网络层面的故障,挑选出合适的辅助工具显得尤为必要。提及的"适用于ubuntu的TCP/UDP排错资源"很可能是指向`mNetAssist`这款应用,从压缩文件清单包含一个名为`mNetAssist-released.deb`的条目来看,这通常表明该文件是为Debian或Ubuntu平台设计的软件安装包。`mNetAssist`作为一个功能全面的网络监控与排错软件,能够协助用户对TCP/IP网络连接进行检测、剖析以及调试。接下来,我们将对这款软件的各项特性以及其在Ubuntu系统上的安装与运用方法进行详尽的阐述。 为了部署`mNetAssist`,首先需要确认Ubuntu系统已经更新至最新状态,并且已经配置了`apt`软件包管理机制。在终端界面输入下列指令来执行软件包的安装流程: ```bash sudo apt update sudo apt install dpkg ``` 接着,将`mNetAssist-released.deb`软件包文件传输到Ubuntu系统的某个文件夹内,例如`~/Downloads`目录,随后利用`dpkg`指令进行安装操作: ```bash cd ~/Downloads sudo dpkg -i mNetAssist-released.deb ``` 在安装阶段若遭遇依赖性难题,可能需要借助`apt-get`来处理: ```bash sudo apt-get install -f ``` 完成安装后,用户可以通...

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧