1

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.

6
  • 1
    So you want to abstract away construction of animals? Like a factory? Can you give an example how such a factory would be used? Commented Mar 16, 2014 at 14:18
  • Well pointed out, perhaps Animals weren't the best example! I'll change that. Commented Mar 16, 2014 at 14:25
  • Edited above, hopefully that's more understandable of an example. Commented Mar 16, 2014 at 14:31
  • Two things: 1) I agree with a factory but not sure it is really needed; 2) you should have a LoginWebPage and a SignUpWebPage. You are really selling the design short if you try to have an "either/or" object. What if tomorrow you decide to have other 'sign-up types': RequestInfoWebPage, GetEmailsWebPage, etc. Commented Mar 16, 2014 at 14:37
  • I can understand that, but please let's not read too far into the example. The question is more about the way that objects are instantiated with a constructor or factory method based upon implemented interfaces. This could apply generally to a number of situations past the example. Perhaps how I asked the question was a bit lazy - please see the edit for the last paragraph about the quesiton. Commented Mar 16, 2014 at 14:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, yeah that does make sense, sorry about that. Thank you for your answer.
To add to this, signup and login are different use cases, and should probably be separate pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.