什么说得过去的价值,我可以使用,除了一个对象实例,访问多个功能呢? [英] What passable value can I use, besides an object instance, to access multiple functions?

查看:79
本文介绍了什么说得过去的价值,我可以使用,除了一个对象实例,访问多个功能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习C#和我在寻找,也许是坏的架构造成的,不影响现实生活方案问题的解决方案。

我想通过持有多种功能,并不需要被实例化,例如,我想有几个小时和任务在字典列表中的类东西(不一定是一个类),因此,例如,在12:00我希望类'午餐',但午餐可能依赖于其他变量,所以我有一个字典项检查如{12} LunchTask,LunchTask是子/实施/推导任务',因此我们可以安全地传递,并呼吁像SomeTask.Start,SomeTask.Pause,SomeTask.Stop。

不过,我觉得关于使用词典(INT,System.Type的),但无法得到它的工作,我也尝试过静,但他们不能被继承和代表是单一功能的据我所知。我只是想传递的东西在具有可直接访问不实例化功能的字典。一个解决方案,我知道的工作,但我觉得很不雅观就是与所有不同的任务实例的静态类。

我不知道有什么更好的方法来实现这样一个基本的功能,也许我所做的一切可怕的错误。所以,如果你们能在正确的方向指向我,我会非常感激。谢谢你在前进。

下面是一些(伪C#)code:

 公共抽象类任务{
    公共抽象无效ExecuteTask();
    公共虚拟无效PauseTask(){Console.WriteLine(任务已暂停)}
    公共虚拟无效StopTask(){Console.WriteLine(任务已停止)}
}公共类午餐:任务{
    公共覆盖无效ExecuteTask()
    {
        Console.WriteLine(午餐任务开始);
    }
}//下面是要被实例化
公共类人{
    字典< INT,有事>归因=新词典< INT,有事>(){{12,午餐}};
    无效ToBeCalledEveryHour(){
        INT小时= someHour();
        如果(attributions.ContainsKey(小时))
            归属[小时] .ExecuteTask();
    }
}


解决方案

在你的情况,你可以使用对象池模式或工厂模式。

在对象池模式的情况下,你可以在其中的关键应该是唯一标识对象的字符串地图,例如,它可以是类名和值可以是对应的对象。

这样的:

 公共抽象类任务{
公共抽象无效ExecuteTask();
公共虚拟无效PauseTask(){Console.WriteLine(任务已暂停)}
公共虚拟无效StopTask(){Console.WriteLine(任务已停止)}
}公共类午餐:任务{
   公共覆盖无效ExecuteTask()
   {
     Console.WriteLine(午餐任务开始);
   }
}公共类ObjectCollection {
    字典<字符串,任务> objectStringMapper =新词典<字符串,字符串>();
    字典<字符串,任务> objectTimeMapper =新词典<字符串,字符串>();
    公共ObjectCollection(){
        objectMapper.Add(午餐,新LunchTask());
        objectTimeMapper.Add(12,新LunchTask());
    }
    公共任务的getObject(字符串OBJID){
        返回objectMapper.get(OBJID);
    }
    公共任务的getObject(INT时间){
        返回objectTimeMapper.get(时间);
    } }公共类人{
    ObjetCollection objectsFactory =新ObjectCollection();
    无效ToBeCalledEveryHour(){
        INT小时= someHour();
        如果(attributions.ContainsKey(小时))
            objectsFactory.getObject(小时).ExecuteTask();
      }
}

或者你可以选择工厂模式,在其中你可以使用任何反射或开关的情况下创建对象的类。

请注意:由于我是一个Java开发人员和新的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.

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.

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.

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.

Like this:

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();
      }
}

Or you can Opt for Factory Pattern , in which you can have a class for creating objects using either reflection or switch case.

Note: Since I'm a Java Developer and new to c# you may find some syntax's wrong.

这篇关于什么说得过去的价值,我可以使用,除了一个对象实例,访问多个功能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆