问题描述
我正在学习 C#,我正在寻找一个问题的解决方案,这个问题可能是由糟糕的架构引起的,并且不会影响现实生活中的程序.
I'm learning C# and I'm looking for a solution for a problem that perhaps is caused by bad architecture and doesn't affect real-life programs.
我想传递一个包含多个函数并且不需要实例化的东西"(不一定是一个类),例如,我想在字典中有一个包含时间和任务列表的类,所以,例如,在 12:00 我想让班级午餐",但午餐可能取决于其他变量,所以我有一个字典条目来检查 {12, LunchTask}, LunchTask 是 ' 的子类/实现/派生Task' 这样我们就可以安全地传递它并调用诸如 SomeTask.Start、SomeTask.Pause、SomeTask.Stop 之类的东西.
I want to pass "something" (not necessarily a class) that holds multiple functions and doesn't need to be instantiated, for example, I want to have a class with a list of hours and tasks in a dictionary, so, for example, at 12:00 I want the class to 'lunch', but that lunch may depend on other variables, so I have a dict entry to check like {12, LunchTask}, LunchTask is subclass/implementation/derivation of 'Task' so we can safely pass it and call something like SomeTask.Start, SomeTask.Pause, SomeTask.Stop.
我虽然关于使用 Dictionary (int,System.Type) 但无法让它工作,我也尝试了静态,但它们不能被子类化,据我所知,委托是用于单个函数的.我只想在具有无需实例化即可直接访问的函数的 dict 中传递一些内容.我知道一个可行的解决方案是使用包含所有不同任务实例的静态类,但我发现非常不优雅.
I though about using Dictionary (int,System.Type) but couldn't get it working, I also tried statics but they can't be subclassed and delegates are for single functions as far as I know. I just want to pass something in a dict that has functions that can be accessed directly without instantiating. One solution I know would work but I find very inelegant is to have a static class with instances of all the different tasks.
我不知道有什么更好的方法来实现这样一个基本功能,也许我做的每件事都非常错误.因此,如果你们能指出我正确的方向,我将不胜感激.提前致谢.
I don't know of any better way to achieve such a basic functionality and perhaps I'm doing everything terribly wrong. So if you guys could point me in the right direction I'd be very grateful. Thank you in advance.
这是一些(伪c#)代码:
Here is some (pseudo-c#) code:
public abstract class Task {
public abstract void ExecuteTask ();
public virtual void PauseTask() {Console.WriteLine ("Task Paused")}
public virtual void StopTask() {Console.WriteLine ("Task Stopped")}
}
public class Lunch : Task {
public override void ExecuteTask ()
{
Console.WriteLine ("Lunch Task Started");
}
}
//the following is gonna be instantiated
public class Human {
Dictionary<int, Something> attributions = new Dictionary<int, Something>(){{12, Lunch}};
void ToBeCalledEveryHour () {
int hour = someHour();
if (attributions.ContainsKey(hour))
attributions[hour].ExecuteTask();
}
}
推荐答案
在您的情况下,您可以使用对象池模式或工厂模式.
In your case you can use either Object pool pattern or Factory Pattern.
在对象池模式的情况下,你可以有一个映射,其中键应该是唯一标识对象的字符串,例如它可以是类名,值可以是对应的对象.
In case of Object pool pattern you can have a Map in which key should be the string that will identify the object uniquely, for example it can be class name and the value can be corresponding object.
像这样:
public abstract class Task {
public abstract void ExecuteTask ();
public virtual void PauseTask() {Console.WriteLine ("Task Paused")}
public virtual void StopTask() {Console.WriteLine ("Task Stopped")}
}
public class Lunch : Task {
public override void ExecuteTask ()
{
Console.WriteLine ("Lunch Task Started");
}
}
public class ObjectCollection{
Dictionary<string,Task> objectStringMapper= new Dictionary<string,string>();
Dictionary<string,Task> objectTimeMapper= new Dictionary<string,string>();
public ObjectCollection(){
objectMapper.Add("Lunch",new LunchTask());
objectTimeMapper.Add(12,new LunchTask());
}
public Task getObject(string objId){
return objectMapper.get(objId);
}
public Task getObject(int time){
return objectTimeMapper.get(time);
}
}
public class Human {
ObjetCollection objectsFactory = new ObjectCollection();
void ToBeCalledEveryHour () {
int hour = someHour();
if (attributions.ContainsKey(hour))
objectsFactory.getObject(hour).ExecuteTask();
}
}
或者您可以选择工厂模式,在该模式中您可以拥有一个使用反射或 switch case 创建对象的类.
Or you can Opt for Factory Pattern , in which you can have a class for creating objects using either reflection or switch case.
注意:由于我是一名 Java 开发人员并且不熟悉 c#,因此您可能会发现一些语法错误.
Note: Since I'm a Java Developer and new to c# you may find some syntax's wrong.
这篇关于除了对象实例之外,我还可以使用什么可传递值来访问多个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!