Using a facade template

How can I find out that I need a facade template at the time of developing my application?

How to draw a line between a facade template and a template template?

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

+6
language-agnostic design-patterns facade
source share
3 answers

The façade deals with the interface, not the implementation. Its purpose is to hide internal complexity behind a single interface that looks just outside. In the example from your question, the facade hides four classes (Order, OrderLine, Address, BasketItem) behind a single method.

The template method refers to the implementation. Its purpose is to extract a general algorithm from several that differ only in "filling in the gaps." The template method in the superclass implements the general algorithm, and each subclass "fills the spaces" in its own way.

So why does the author not use the template template here?

It would be wise to make a placeOrder template method if there were several similar versions of the operation. Perhaps several methods like placePhoneOrder , placeInternetOrder , placeManuallyEnteredOrder can be reorganized into one placeOrder template with some subclasses that implement only specific differences {phone, internet, manual}.

+9
source share

The facade template is suitable when you have a complex system that you want to simplify for customers, or want to create an external level of communication over an existing system that is incompatible with your system. This is a block diagram. See here: http://en.wikipedia.org/wiki/Facade_pattern

The sample template, on the other hand, is a behavioral template that will help you when working with the component's internal implementation. See here: http://en.wikipedia.org/wiki/Template_method_pattern

+7
source share

Suppose you have several services, libraries, or something else. These libraries need interaction in order to perform certain higher-level services. You may then want to wrap these calls and initialization code, which usually go together, and offer many functions to hide this data and make it easier to use these services for certain scenarios. Then it is a good application for facade design.

UPDATE: In the above article, the PlaceOrder method has one single implementation that works for all orders. The template template is designed to prescribe the sequence of steps to be performed, but allowing subclasses to propose their own implementation of these fixed steps. For example, if you need orders for processing TVs differently from orders for microwaves, you can use the template template to override some imaginary DispatchParcel method (to send the microwave as a simple package, but an additional service TV to help lift a heavy device to the top floor). In our case, there is no need to re-implement the ProcessOrder steps, so there is no need for a template template, since one single implementation is suitable for all types of orders.

+3
source share

All Articles