Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Facade Pattern

How can I know that I need a facade Pattern at a point in my application development?

How can I draw the line between Facade Pattern and Template Pattern?

For example: In [this] article, we see that, int placeOrder(int CustomerID, List<BasketItem> Products) has a number of predefined steps in the algorithm. So why don't the author use Template Pattern here?

like image 345
user366312 Avatar asked Nov 23 '09 08:11

user366312


2 Answers

Facade deals with interface, not implementation. Its purpose is to hide internal complexity behind a single interface that appears simple on the outside. In the example from your question, the facade hides four classes (Order, OrderLine, Address, BasketItem) behind a single method.

Template method deals with implementation. Its purpose is to extract the common algorithm from several ones that differ only in a 'fill in the blanks' way. The template method in the superclass implements the common algorithm and each subclass 'fills in the blanks' in its own specific way.

So why don't the author use Template Pattern here?

It would make sense to make placeOrder a template method if there were several similar versions of the operation. Maybe a few methods like placePhoneOrder, placeInternetOrder, placeManuallyEnteredOrder could be refactored into a single template placeOrder with some subclasses implementing only the {phone,internet,manual}-specific differences.

like image 99
Rafał Dowgird Avatar answered Oct 01 '22 21:10

Rafał Dowgird


The facade pattern is appropriate when you have a complex system that you want to expose to clients in a simplified way, or you want to make an external communication layer over an existing system which is incompatible with your system. It is a structural pattern. See here: http://en.wikipedia.org/wiki/Facade_pattern

The template pattern, on the other hand, is a behavioral pattern that will help you when dealing with the inner implementation of a component. See here: http://en.wikipedia.org/wiki/Template_method_pattern

like image 22
Konamiman Avatar answered Oct 01 '22 20:10

Konamiman