C # - creating a saved object

I want to create a class library that contains an interface from which objects can arise, say ISaveableObject.

Now this interface should implement the following functions:

  • Objects that display this interface must have a method ToSaveableObject(similar to a method System.Object.ToString()).
  • And it should contain a specific constructor that takes an array of objects as a parameter.

The connection between the two should be that the method ToSaveableObjectreturns a string that takes all the properties necessary for object[], and converts them to a readable string and returns it.

Am I going in the right direction, wanting to use an interface, or is an abstract class more suitable for this case?

Unfortunately, interfaces cannot be implemented by designers, is there any other way to achieve my goal?

+4
source share
1 answer

Use an abstract base class to form a contract for derived classes. You want more implementation details that more than the interface can provide.

An abstract base class might look like this:

public abstract class SaveableObject {
    protected object[] parameters = new object[0];

    protected SaveableObjectBase(object[] objects) {
        this.parameters = objects;
    }

    public abstract string ToSaveableObject();
}

So, now the derived classes must implement the method ToSaveableObject()and will have access to the objects passed in the constructor to generate the string.

ToSaveableObject , , .

+1

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


All Articles