Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重构具有多个具有相同签名的方法的类_C#_Oop_Design Patterns_Composite - Fatal编程技术网

C# 重构具有多个具有相同签名的方法的类

C# 重构具有多个具有相同签名的方法的类,c#,oop,design-patterns,composite,C#,Oop,Design Patterns,Composite,我有一个具有多个具有相同签名的方法的类: string MethodA(int id); string MethodB(int id); string MethodC(int id); 这些方法的实现明显不同,但我正在努力使其更加可靠。我对复合模式有了很大的了解,复合类将实现下面的IDOSthing接口: public Interface IDoSomething { string DoSomething(int id); } public class CompositeClass :

我有一个具有多个具有相同签名的方法的类:

string MethodA(int id);
string MethodB(int id);
string MethodC(int id);
这些方法的实现明显不同,但我正在努力使其更加可靠。我对复合模式有了很大的了解,复合类将实现下面的IDOSthing接口:

public Interface IDoSomething
{
   string DoSomething(int id);
}

public class CompositeClass : IDoSomething
{
    private readonly IEnumerable<IDoSomething> somethings;
    public CompositeClass(IEnumerable<IDoSomething> somethings)
    {
        this.somethings = somethings;
    }

    public string DoSomething(int id)
    {
        foreach (var s in somethings)
        {
            return s.DoSomething(id);
        }
    }
}
公共接口
{
字符串DoSomething(int-id);
}
公共类CompositeClass:IDoSomething
{
私有只读的IEnumerable something;
公共复合类(IEnumerable somethings)
{
this.something=某物;
}
公共字符串DoSomething(int-id)
{
foreach(某些事物中的var s)
{
返回s.DoSomething(id);
}
}
}

问题是返回的结果。这对于每个呼叫都是不同的。我可以将签名更改为string[],但这似乎是一个黑客行为。有什么想法吗?

我看不出您想要实现什么,类和方法看起来不错。您可以使用Decorator模式来链接方法,但似乎还需要几个结果

如果想要相同的方法名和签名,可以使用显式接口实现,这实际上不是一个好的实践。 您甚至可以采用非常肮脏的方式,使用带有Methodname和签名的接口。派生标记接口(这是一种反模式!)。显式实现派生接口。通过对自己使用反射并调用所有“其他”接口方法来实现基本接口。。。但不要为了……而那样做。。。好吧,一切都好

问题是返回的结果。这对于每个呼叫都是不同的

这似乎是对复合模式的不当使用。复合材料背后的一般想法是,你不在乎你是否使用复合材料或其任何成分,因为它们在逻辑上是可互换的


举例来说,假设你有一个抽象概念

interface IPainter
{
    double PaintedArea(double hours);
}
您有很多实现--

这里有一个关键的部分——想想看,你的客户可能不在乎他们雇佣的是一个画家还是一个团队。他们只在乎所有的墙都漆好了

因此,您可以制作一个复合的
PainterCrew
,并使其作为一个单独的画家工作--

class PainterCrew:ipainer
{
只读的可数画家;
画家画家(IEnumberpainters)
{
这个。画家=画家;
}
双喷漆区域(双小时)
{
返回这个.painters.Sum(p=>p.PaintedArea(hours));
}
}

请看,您不仅返回单个结果的集合——您还以一种有意义的方式对其进行聚合,这样,从调用方的角度来看,无论它们是使用组合还是单个实体都无关紧要。

也许一个更具体的示例会有所帮助。如果你使用像
MethodABC
DoSomething
这样的名称,很难知道你想做什么。谢谢你的回答。如果一个类有三个方法,它们都具有相同的签名,那么这个类似乎可以进行重构。通过重构到一个组合,我得到了很长的路,直到返回值。我就这样下课:)
class DiligentPainter : IPainter
{
    double PaintedArea(double hours) => hours * 100;
}

class LazyPainter : IPainter
{
    double PaintedArea(double hours) => hours * 20;
}
class PainterCrew : IPainter
{
    readonly IEnumerable<IPainter> painters;

    PainterCrew(IEnumerable<IPainter> painters)
    {
        this.painters = painters;
    }

    double PaintedArea(double hours)
    {
        return this.painters.Sum(p => p.PaintedArea(hours));
    }
}