使用立面图案 [英] Use of Facade Pattern

查看:100
本文介绍了使用立面图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我如何知道我在应用程序开发中需要一个外观模式?如何在Facade Pattern和Template Pattern之间画线? / p>

例如:在 [这个] 文章,我们看到, int placeOrder(int CustomerID,List< BasketItem> Products)在算法中有许多预定义的步骤。那么为什么作者在这里不使用模板模式?

解决方案

Facade处理界面,而不是实现。其目的是隐藏在外部简单的单一界面背后的内部复杂性。在你的问题的例子中,立面隐藏了一个方法后面的四个类(Order,OrderLine,Address,BasketItem)。



模板方法处理实现。其目的是从几个不同于填写空白的方式中提取常见算法。超类中的模板方法实现了通用算法,每个子类都以自己的特定方式填充空白。


所以为什么作者在这里使用模板模式?


使 placeOrder 一个模板方法,如果有几个类似版本的操作。也许有几种方法,如 placePhoneOrder placeInternetOrder placeManuallyEnteredOrder 被重构成一个单独的模板 placeOrder ,其中一些子类仅实现{手机,互联网,手动}具体的差异。


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?

解决方案

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.

这篇关于使用立面图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆