Question

Imagine that we have classes as such:

public abstract class WebPage
{ 
    public WebPage() 
    { ... } 
}

public class LoginOrSignUpWebPage : WebPage, ILogin, ISignUp
{
    private Info _loginInfo;
    private Info _signUpInfo;
    public readonly Info LoginInfo { get { return _meats; } }
    public readonly Info SignUpInfo { get { return _legs; } }

    public class LoginOrSignUpWebPage(Info loginInfo, Info signUpInfo) : base()
    { ... }
}

We can see that WebPages would want to have different ways of being instantiated based on different interfaces they implement.

Whilst it'd feel okay implementing individual constructions for each class, I would prefer to use inheritance to base the object construction upon. The reason for this is because another object may implement the same interfaces and would have the same way of being instantiated.
I have thought about using some sort of (abstract?) Factory method, but I'm not sure how this would work.

Question:

Right to the point, what do you think would be the best way to base construction of an object based upon what interfaces it inherits? This would include (potentially) different parameters, and at minimum different data passed depending on the implemented interface.

Was it helpful?

Solution

We can see that WebPages would want to have different ways of being instantiated based on different interfaces they implement.

No, they wouldn’t. Interfaces define how types should look like to the outside. They provide no implementation detail and also no information about the constructor or the construction process. If you have an object of the type of an interface, all you know is that you can access the properties and methods that were defined in the interface.

The way you declare your WebPage type, it is fixed to implement both ILogin and ISignUp. As such it is absolutely required to implement whatever these two interfaces specify. And every object of type WebPage will always provide what both interfaces require.

It is not necessary to make the construction of an object based on the interfaces it is implementing, simply because the interfaces you are implementing is known at compile time and can’t be changed later. So for the type implementing an interface, you just specify directly how that is constructed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top