nus-cs2103-AY1819S2 / forum

CS2103/T discussion forum
6 stars 1 forks source link

What is the difference between an interface and the façade design pattern? #69

Closed iwle closed 5 years ago

iwle commented 5 years ago

In this week's tutorial, we learnt about some common design patterns, such as the façade pattern. The façade pattern helps to resolve the problem of accessing a component without exposing the internal details of the class that implements that component.

Is it correct to think of the façade pattern as an interface?

Thank you.

rbth7e5 commented 5 years ago

I believe it can be a concrete class as well. Like how the logic manager works in AB4. Correct me if I’m wrong?

On 27 Mar 2019, at 7:42 PM, Ian Wu notifications@github.com wrote:

In this week's tutorial, we learnt about some common design patterns, such as the façade pattern. The façade pattern helps to resolve the problem of accessing a component without exposing the internal details of the class that implements that component.

Is it correct to think of the façade pattern as an interface?

Thank you.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

fterh commented 5 years ago

Yup, both interfaces and concrete classes will work. I think it's best to use both: an interface to specify the API, and a concrete class to implement the interface. Here are my reasons:

1) Java interfaces are meant for the design by contract paradigm since you specify only method signatures and not implementation (let's leave default methods out of this discussion for now :P). So, you use interfaces to specify your contract, and you use an implementing concrete class to provide implementation details.

2) If you use a concrete class directly without an interface, you are coupling the user with your class. If you were to ever switch your class out for something else in the future, you possibly have to modify code at the user's side (unless you retain your class name and the public methods). But if you retain your class name and public methods... you are effectively using your concrete class as an interface... so why not just use an interface instead? :D

3) This is a minor reason, but I personally like using interfaces to specify API, because in my actual implementing concrete class I would potentially have a bunch of private utility methods (for reasons like SRP, DRY, etc.) It makes it a lot more manageable when I can refer to my interface to know exactly what needs to be done.

ccristina commented 5 years ago

Hi,

A facade can be implemented in multiple ways: an interface, a class, a set of API calls in a class. You should think at facade as a concept, more than an actual class/interface (of course, you need to eventually have it as an instantiation of a class). The facade should act as a layer and relay calls between the client and the actual components implementing the functionalities.

Let us know if you still have questions.

Best regards, Cristina.

iwle commented 5 years ago

Thanks @rbth7e5, @fterh and @ccristina!