简体   繁体   English

通用基类

[英]Generic base class

I am doing a customization from an existing library, which has following structure: 我正在从现有库中进行自定义,该库具有以下结构:

public class BaseClass
{
    public Method1();
    public Method2();
}
public class A : BaseClass
{
    public Method1();
    public Method2();
}
public class B : BaseClass
{
    public Method1();
    public Method2();
}
public class C : BaseClass
{
    public Method1();
    public Method2();
}

The above are code that I cannot change . 以上是我无法更改的代码。

Now, I need to cumtom A, B, C. but ALL changes inside each inheritances are totally SAME. 现在,我需要对A,B,C进行累加,但是每个继承中的所有更改都完全相同。 logic and code are totally same changes. 逻辑和代码完全相同。

public class BaseClass1 : BaseClass
{
    public override Method1(); //some logic changed comparing to base
}
public class A1 : A
{
    public override Method1(); //Changes are totally  same with BaseClass1 
}
public class B1 : B
{
    public override Method1(); //Changes are totally  same with BaseClass1 
}
public class C1 : C
{
    public override Method1(); //Changes are totally  same with BaseClass1 
}

All code are duplicated, copy-pasted in each class. 在每个类中,所有代码都是重复的,复制粘贴的。

My Question is: How can I avoid copy-pasted code?? 我的问题是:如何避免复制粘贴的代码?

This code doesn't work clearly: 此代码无法正常工作:

public class BaseClass1<T> : T where T: BaseClass  //Base class cannot be T
{
    public override Method1(); 
    public override Method2(); 
}

The reason I don't want to duplicate code is that there are C,D,E,F and so on inheritances. 我不想重复代码的原因是有C,D,E,F等继承。

As one option you could use a variation of the decorator pattern, (the exact library signatures are unknown so I have added examples where methods are overridden or hidden) example below: 作为一种选择,您可以使用装饰器模式的一种形式(确切的库签名是未知的,因此我添加了一些示例,这些示例重写或隐藏了方法)示例如下:

// original libary code
public class BaseClass
{
    public virtual string Method1() => "m1 of base";
    public string Method2() => "m2 of base";
}
public class A : BaseClass
{
    public override string Method1() => "m1 A";
    public string Method2() => "m2 A";
}
public class B : BaseClass
{
    public override string Method1() => "m1 B";
    public string Method2() => "m2 B";
}
public class C : BaseClass
{
    public override string Method1() => "m1 C";
    public string Method2() => "m2 C";
}


// methods to be overridden
public interface IMethodOfBase
{
    void Method1();
    void Method2();
}

// common behaviour
public class Decorator:IMethodOfBase
{
    private BaseClass b;

    public Decorator(BaseClass b)
    {
        this.b = b;
    }

    public void Method1()
    {
        Console.WriteLine($"overridden behaviour in one place {b.Method1()}");
    }

    public void Method2()
    {
        Console.WriteLine($"overridden behaviour in one place {b.Method2()}");
    }
}

// example usage
public class WorkLoad
{
    private List<Decorator> _work; 

    public WorkLoad()
    {
        _work = new List<Decorator>
        {
            new Decorator(new A()),
            new Decorator(new B()),
            new Decorator(new C()) //etc
        };
    }

    public void DoWork()
    {
        _work.ForEach( n =>
        {
            n.Method1();
            n.Method2();
        });
    }

}

and to run it 并运行它

WorkLoad workLoad = new WorkLoad();
workLoad.DoWork();

and output: overridden behaviour in one place m1 A 和输出:在一处覆盖的行为m1 A

overridden behaviour in one place m2 of base 在一个位置m2的基础上的覆盖行为

overridden behaviour in one place m1 B 一个地方的覆盖行为m1 B

overridden behaviour in one place m2 of base 在一个位置m2的基础上的覆盖行为

overridden behaviour in one place m1 C 一个地方的覆盖行为m1 C

overridden behaviour in one place m2 of base 在一个位置m2的基础上的覆盖行为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM