DEV Community

jzfrank
jzfrank

Posted on

HFDP(7) - Adapter and Facade Pattern

In this post we will discuss two pattens: Adapter Pattern and Facade Pattern. Why we put them in the same post? Because their purpose is kinda similar: both "converting" something unsuitable/complex into something suitable/simple.

Adapter Pattern

Imagine you are on travel. You travel many countries: U.S., U.K., Switzerland, China etc. You realized that different areas are using different sockets. What do you do? You use adapters.

Adapter for Sockets

This is exactly the idea of Adapter Pattern. Say you were using the java's interface Enumeration. However, as java gets updated in new versions, it has a new interface Iterator. You want to use Iterator object in your code, however, instead of changing everything, you want to keep old code as much as possible. What do you do? You write an adapter to adapt Iterator to Enumeration.

An example of implementing:

public class IteratorEnumeration implements Enumeration { 
    Iterator iterator;

    public IteratorEnumeration(Iterator iterator) { 
        this.iterator = iterator;
    }

    public boolean hasMoreElements() { 
        return iterator.hasNext();
    }

    public Object nextElement() { 
        return iterator.next();
    }
}
Enter fullscreen mode Exit fullscreen mode

Formal Definition

The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

Adapter Pattern

In fact, there are two kinds of adapter patterns: object adapter and class adapter. While the object adapter pattern achieves this by passing object, the class adapter pattern achieves this by multiple inheritance.

Facade Pattern

the Facade

Imagine you are setting your personal home movie theater. You bought the equipments, you settled the locations, you connected the internet, you bought a popcorn machine, and you even bought Netflix. OK, everything seems cool. Let's sit down and enjoy our well-earned leisure of watching movie. Now what do you need to do? You need to 1) turn on the projector 2) close lights 3) turn on popcorn machine 4) open netflix and search for target movie 5) set volume to medium (say 15) etc. ... Hey, why it is so complicated? What's worse, when we want to quit, we need to undo everything. Wouldn't it be nice if we have a remote controller that turns on everything once we hit "on" mode, turns off everything once we hit "off" mode. This will need the facade pattern, which tries to provide a simplified interface for a large and complex subsystem. The facade pattern provides intuitive interfaces and reduces coupling with the subsystems.

movie theater all

manual steps

movie theater with facade

Formal Definition

The Facade Pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher- level interface that makes the subsystem easier to use.

Facade Pattern Diagram

Design Principle

Principle of Least Knowledge - talk only to your immediate friends.

Facade Pattern is a good illustration of this principle. No matter how complex the subsystem is, if the client code only talks to the simplified interface (the facade), things will become easier and coupling will be reduced.

Using this principle, we should not call methods on returned objects. For example,

Principle of Least Knowledge

Now it comes to the end of this post. We have discussed adapter pattern and facade pattern. Further, we introduced principle of least knowledge in light of facade pattern. Adapter Pattern: When we have two similar interfaces, we use Adapter Pattern if we want to use one pattern as another pattern. Facade Pattern: When we have a overly complex subsystem while we want simplified interface, we use Facade Pattern.

See you next post!

Top comments (0)