Best way to reorganize shared code

Consider the following two classes that implement a bunch of properties from an interface:

Interface Code:

public interface ISample
{
    int x;
    string y;
}

Grade 1:

public class SampleA: ISample
{
    public int x { get; set; }
    public string y { get; set; }
}

Grade 2:

public class SampleB: ISample
{
    public int x { get; set; }
    [Decorated]
    public string y { get; set; }
}

The only difference is that it SampleBhas one property decorated with an attribute.

This is very simplified, and the classes in question have much more properties, but the main differences of one class have some properties decorated with attributes.

In the future, there will be situations when additional classes will be introduced that implement the interface ISampleand feel that these classes should probably inherit some common code, possibly from an abstract class or something else.

What would be a good approach to refactoring this code?

+4
source share
1

: Sample virtual, derrived , override .

public class Sample
{
    public virtual int x { get; set; }
    public virtual string y { get; set; }
}

public class SampleA : Sample
{
}

public class SampleB : Sample
{    
    [Decorated]
    public override string y { get; set; }
}
+5

Source: https://habr.com/ru/post/1658051/


All Articles