Design Patterns Uncovered: The Facade Pattern

This article will focus on the Facade pattern. So far in our design patterns we’ve already looked at the Observer and Adapter patterns. Facade has some similarities with the Adapter, so it’s a logical next step in our series.

Facades in the Real World

Facades are all around us in the real world. Operating systems are one such example - you don’t see all the inner workings of your computer, but the OS provides a simplified interface to use the machine. Buildings also have a facade - the exterior of the building. Wikipedia gives us a nice link between software architecture and standard architecture:

In architecture, the facade of a building is often the most important from a design standpoint, as it sets the tone for the rest of the building

So, in a nutshell, a Facade aims to make things look cleaner and more appealling.

The Facade Pattern

Like the Adapter pattern, Facade is known as a structural pattern, as it’s used to identifying a simple way to realize relationships between entities. The definition of Facade provided in the original Gang of Four book on Design Patterns states:

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

The diagram definition of the Facade pattern is quite simple - all you’re really doing is insulating client from the subsystem:
The diagram definition of the Facade pattern is quite simple - all you're really doing is insulating client from the subsystem
Like the adapter pattern, the Facade can be used to hide the inner workings of a third party library, or some legacy code. All that the client needs to do is interact with the Facade, and not the subsystem that it is encompassing.
The following sequence diagram illustrates how the pattern is used by a client:
The following sequence diagram illustrates how the pattern is used by a client:

Where Would I Use This Pattern?

As the concept behind facade is to simplify an interface, service oriented architectures make use of the facade pattern. For example, in web services, one web service might provide access to a number of smaller services that have been hidden from the caller by the facade. Similarly, a typical pattern in OSGi bundles is to provide an interface package that is exposed to users of the bundle. All other packages are hidden from the user.

So How Does It Work In Java?

Let’s put together a simple example in Java code to illustrate the pattern. Let’s take a travel agent site for example, that allows you to book hotels and flights. We have a HotelBooker:

public class HotelBooker
{

  public ArrayList<Hotel> getHotelNamesFor(Date from, Date to) 
  {
      //returns hotels available in the particular date range

  }

}

And a FlightBooker:

public class FlightBooker
{

  public ArrayList<Flight> getFlightsFor(Date from, Date to) 
  {
      //returns flights available in the particular date range

  }

}

Both of these have Hotel and Flight datatypes, which the client has knowledge about. They could be provided in the same package as the Facade for example.
The TravelFacade class allows the user to get their Hotel and Flight information in one call:

public class TravelFacade
{

   private HotelBooker hotelBooker;
   private FlightBooker flightBooker; 

  public void getFlightsAndHotels(Date from, Data to)
  {
         ArrayList<Flight> flights = flightBooker.getFlightsFor(from, to);
         ArrayList<Hotel> hotels = hotelBooker.getHotelsFor(from, to);

         //process and return

   }

}

All that the client needs to worry about is the Facade class:

public class Client
{

   public static void main(String[] args)
   {
        TravelFacade facade = new TravelFacade(); 
        facade.getFlightsAndHotels(from, to);
   }
}

As you can see, it’s just a simple approach to encapsulating data.

Watch Out for the Downsides

By introducing the Facade into your code, you will be hardwiring subsystems into the Facade. This is fine if the subsystem never changes, but if it does, your Facade could be broken. Therefore, developers working on the subsystem should be made aware of any Facade around their code.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《设计模式:可复用的面向对象软件的基础》(Design Patterns: Elements of Reusable Object-Oriented Software)是一本经典的计算机科学书籍,由Erich Gamma、Richard Helm、Ralph Johnson和John Vlissides联合编写。 该书首次系统地介绍了23种常见的设计模式,这些模式是面向对象软件开发的重要指导和实践。设计模式是一种对重复出现的问题的解决方案,通过它们可以提供一种通用、可重用和可扩展的设计方法,用于解决软件系统中的常见问题。 《设计模式:可复用的面向对象软件的基础》描述了每个设计模式的结构、目的、应用场景和实现方式。它通过示例代码和详细解释来帮助读者理解每种模式的用途和优缺点。 这本书的主要贡献之一是将设计模式分为三个类别:创建型、结构型和行为型模式。其中,创建型模式关注如何通过不同的方式创建对象,结构型模式关注如何组合对象以形成新功能,行为型模式关注对象之间的通信和协作方式。通过这种分类方式,读者可以更好地理解和应用设计模式。 《设计模式:可复用的面向对象软件的基础》已成为软件工程师必读的经典之作。它提供了一种设计思维的范式,可以帮助开发人员提高软件的扩展性、可读性和可维护性。无论是初学者还是经验丰富的开发人员,阅读并理解设计模式都对他们的软件设计和开发能力有很大的帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值