Use of Facade Pattern

后端 未结 3 1297
孤街浪徒
孤街浪徒 2021-01-02 20:51

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?

Fo

3条回答
  •  盖世英雄少女心
    2021-01-02 21:44

    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.

提交回复
热议问题