Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.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/8/design-patterns/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# 棘手的架构问题。4基类需要一些可扩展性_C#_Design Patterns_Architecture_Polymorphism - Fatal编程技术网

C# 棘手的架构问题。4基类需要一些可扩展性

C# 棘手的架构问题。4基类需要一些可扩展性,c#,design-patterns,architecture,polymorphism,C#,Design Patterns,Architecture,Polymorphism,我有4个基本类: class A { virtual SomeMethod () { <code> } } class A<T> { virtual SomeMethod () { <code> } } class B { virtual SomeMethod () { <code> } } class B<T2> { virtual SomeMethod () { <code> } } 现在,我有4个实现(每个实现都派

我有4个基本类:

class A { virtual SomeMethod () { <code> } }
class A<T> { virtual SomeMethod () { <code> } }

class B { virtual SomeMethod () { <code> } }
class B<T2> { virtual SomeMethod () { <code> } }
现在,我有4个实现(每个实现都派生自相应的基类型)

现在,我需要添加SomeMethod实现(它应该是基类的重写)。对于所提到的所有派生类都是相同的

最好的解决方案是什么?(问题解决后,我会立即分享我的所有想法。因为如果我把我的实现放在这里,讨论很可能会朝着我的方向进行,但我不太确定我是否正确)


谢谢你的好主意

那么您希望对某些4个类使用一个实现,而对其他4个类使用不同的实现?也许这个办法行得通

interface ISomeMethodStrategy {
    string SomeMethod(string a, string b);
}

class DefaultStrategy : ISomeMethodStrategy {
    public string SomeMethod(string a, string b) { return a + b; }
    public static ISomeMethodStrategy Instance = new DefaultStrategy();
}
class DifferentStrategy : ISomeMethodStrategy {
    public string SomeMethod(string a, string b) { return b + a; }
    public static ISomeMethodStrategy Instance = new DifferentStrategy();
}

class A {
    private ISomeMethodStrategy strategy;
    private string a, b, c;
    public A() : this(DefaultStrategy.Instance) {}
    protected A(ISomeMethodStrategy strategy){
        this.strategy = strategy;
    }
    public void SomeMethod() {
        a = strategy.SomeMethod(b, c);
    }
}

class Aa : A {
    public Aa() : base(DifferentStrategy.Instance) {}
}

我在这里用一个接口实现了这个模式,但是你也可以用委托来实现它。

那么你想为一些4个类实现一个模式,而为一些其他4个类实现一个不同的模式吗?也许这个办法行得通

interface ISomeMethodStrategy {
    string SomeMethod(string a, string b);
}

class DefaultStrategy : ISomeMethodStrategy {
    public string SomeMethod(string a, string b) { return a + b; }
    public static ISomeMethodStrategy Instance = new DefaultStrategy();
}
class DifferentStrategy : ISomeMethodStrategy {
    public string SomeMethod(string a, string b) { return b + a; }
    public static ISomeMethodStrategy Instance = new DifferentStrategy();
}

class A {
    private ISomeMethodStrategy strategy;
    private string a, b, c;
    public A() : this(DefaultStrategy.Instance) {}
    protected A(ISomeMethodStrategy strategy){
        this.strategy = strategy;
    }
    public void SomeMethod() {
        a = strategy.SomeMethod(b, c);
    }
}

class Aa : A {
    public Aa() : base(DifferentStrategy.Instance) {}
}
我在这里用一个接口实现了这个模式,但您也可以对委托执行同样的操作。

A)。如果您确实发布了您的实现,而不管您认为它会扭曲什么,这会更好

B) 。拥有
a类
和通用形式
a类
的概念似乎真的很陌生。这就像你的泛型类对一个特定的情况有一个排除,这意味着它比其他泛型(即wierd)更不通用

C) 。如果您的意思是每个基类上都有相同的方法,那么为什么不让所有4个基类都从更高的类X继承,或者如果您的意思是让它们实现这个通用方法,为什么不让它们从接口继承呢。似乎太明显了,我在你的问题中遗漏了什么吗。如果您确实发布了您的实现,而不管您认为它会扭曲什么,这会更好

B) 。拥有
a类
和通用形式
a类
的概念似乎真的很陌生。这就像你的泛型类对一个特定的情况有一个排除,这意味着它比其他泛型(即wierd)更不通用


C) 。如果您的意思是每个基类上都有相同的方法,那么为什么不让所有4个基类都从更高的类X继承,或者如果您的意思是让它们实现这个通用方法,为什么不让它们从接口继承呢。看起来太明显了,我在你的问题中遗漏了什么吗?

我假设你想要一个在所有实现之间共享(并且可以重写)的方法。非泛型类采用默认类型,例如int

因此,您可以创建单个原始基类

public class Origin {
   public virtual void SomeSharedOverrideMethod(int data) { }
}

class A : Origin {

   public void MethodForAImpl() {
       base.SomeSharedOverrideMethod(23);
   }
}

class A<T> : Origin {

   public void MethodForGenericAImpl() {
       base.SomeSharedOverrideMethod(45);
   }
}

class B : Origin
class B<T> : Origin
公共类来源{
公共虚拟void someSharedVerrideMethod(int-data){}
}
A类:原产地{
public void MethodForAImpl(){
基数法(23);
}
}
A类:原产地{
public void methodforgenericampl(){
基数法(45);
}
}
B类:原产地
B类:原产地

这就是您所需要的吗?

我假设您想要一个在所有实现之间共享(并可重写)的方法。非泛型类采用默认类型,例如int

因此,您可以创建单个原始基类

public class Origin {
   public virtual void SomeSharedOverrideMethod(int data) { }
}

class A : Origin {

   public void MethodForAImpl() {
       base.SomeSharedOverrideMethod(23);
   }
}

class A<T> : Origin {

   public void MethodForGenericAImpl() {
       base.SomeSharedOverrideMethod(45);
   }
}

class B : Origin
class B<T> : Origin
公共类来源{
公共虚拟void someSharedVerrideMethod(int-data){}
}
A类:原产地{
public void MethodForAImpl(){
基数法(23);
}
}
A类:原产地{
public void methodforgenericampl(){
基数法(45);
}
}
B类:原产地
B类:原产地

这就是您所需要的吗?

如何处理接口和组合而不是继承

例如,创建一个新接口

interface myinterface
{
   void doSomethingCool();
}
而不是实现它的新类

class interfaceImpl: myinterface
{
    public void doSomethingCool()
    {
         ... some code
    }
}

class A : myinterface
{
  private interfaceImpl interf = MyInterfaceFactory.Build(args);

  public void doSomethingCool()
  {
      interf.doSomethingCool();
  }
}
因此,在类A、B和所有派生类中可以有相同的行为,也可以重写它


hth

如何处理接口和组合而不是继承

例如,创建一个新接口

interface myinterface
{
   void doSomethingCool();
}
而不是实现它的新类

class interfaceImpl: myinterface
{
    public void doSomethingCool()
    {
         ... some code
    }
}

class A : myinterface
{
  private interfaceImpl interf = MyInterfaceFactory.Build(args);

  public void doSomethingCool()
  {
      interf.doSomethingCool();
  }
}
因此,在类A、B和所有派生类中可以有相同的行为,也可以重写它


hth

问题是如何在C#中进行混音


你可以用一把大锤,采用类似的方法;还有其他方法涉及打开基本定义以添加额外的泛型参数(mix-in对象)。它们都涉及到在语言设计中的精心选择。

问题是如何在C#中进行混合


你可以用一把大锤,采用类似的方法;还有其他方法涉及打开基本定义以添加额外的泛型参数(mix-in对象)。它们都涉及到在语言设计中的一个深思熟虑的选择。

你不是指Bb:B类吗?你不是指Bb:B类吗?我已经有了这个方法。它以虚拟的形式存在于所有基类中。在所有派生类中,它都应该是重写。让我换个问题,只是一个迷你们已经有了这个方法。它以虚拟的形式存在于所有基类中。在所有派生类中,它都应该是重写。让我来改变这个问题,仅仅一个minc基本上是不可能的,因为我从一些基本MVC类派生所有的类。B这对你来说可能是陌生的,我并没有为此责备你。但这确实有一些原因。A.实际上,实施工作正在进行中((不幸的是,我不同意拥有A和A是外来的。这就像IEnumerable和IEnumerable一样。不是真的,IEnumerable是一种先于泛型形式的宿醉。c实际上是不可能的,因为我是从一些基本MVC类派生所有类的。b.这对你来说似乎是外来的,我不是在责怪你。但这有一些原因事实上,实施工作正在进行中(不幸的是,我不同意哈维的说法