Java Reference
In-Depth Information
The decorator pattern allows the addition and combination of behavioral extensions to an
original interface.
The façade pattern aims to provide a simplified interface around a collection of other
interfaces.
The façade pattern is like the adapter pattern on steroids: the façade class will contain an instance of
every class it wraps in order to provide a unified and simpler interface for the client code that needs
it. For example, let's say you have a class to search for flights between two locations and a class
to search for hotels between two locations. Instead of using these two classes as is in your code, it
might be a good idea to define a third class ( TripPlanner ) that wraps around these two classes in
order to find a list of suitable flights and hotels for a given set of locations and time period. The cli-
ent can then use this façade class, which adds an extra layer of decoupling.
NOTe A word of warning for all these “wrapping” patterns. Note that all
these patterns connect the wrapped class to their wrappers, meaning that if
changes are made to the wrapped subsystems, all adapters, bridges, decora-
tors, facades, and proxies might need to be changed and updated as well.
Therefore, don't go too far when connecting classes together by wrapping
them up around each other.
Composite Pattern
The last structural pattern discussed in this chapter is less about wrapping around objects and more
about grouping objects together. Luckily, the description of this pattern is easy to understand:
Allows a group of objects to be treated like a single object.
Basically, following the composite pattern boils down to implementing your own collection class,
which is subject to the same methods as the members of that collection. This pattern is pretty inter-
esting since it is easy to understand in essence, but there are multiple ways to implement it, all with
their advantages and disadvantages.
This idea is illustrated with a simple example. Imagine you're programming an application for the
human resources department. The department oversees a number of employees—project managers
and programmers. You need to keep track of their names and salaries. As such, you set out to create
the following class:
public class Employee {
private String name;
private double salary;
private boolean manager;
public Employee(String name, double salary, boolean manager) {
this.name = name;
this.salary = salary;
this.manager = manager;
}
 
Search WWH ::




Custom Search