Java Design Pattern

Modèle de conception notable.

Cette section présente des exemples de quelques modèles de conception (Design Pattern) qui sont plus représentatif et bien connu. En apprenant chacun de ces modèles, vous pourrez développer votre vocabulaire de modèle et de l'ajouter à votre boîte à outils de conception de logiciels. Chaque modèle comprend, et discuté par la suite, une description du problème, ce que le modèle permet de résoudre, les principes fondamentaux de la conception mise en œuvre dans le modèle, et les classes qui composent le modèle et comment ils travaillent ensemble.

Les modèles de conception abordes sont: Adapter, Model-View-Controller, Command, Strategy, and Composite. Chaque modèle est discuté avec un texte descriptif et un diagramme montrant la structure ainsi que les exemples de classes et leur rôle dans le modele correspondant. Les points principaux, dans chaque cas est de reconnaître la façon dont ces classes collaborent à résoudre un problème spécifique.

Adapter

An Adapter allows components with incompatible interfaces to communicate. The Adapter pattern is a great example of how to use object-oriented design concepts. For one reason, it's very straightforward. At the same time, it's an excellent example of three important design principles: delegation, inheritance, and abstraction. Figure 3-7 shows the class structure of the Adapter pattern as well as the example classes used in this example.


Figure 3-7

The four classes that make up the Adapter pattern are the Target, Client, Adaptee, and Adapter. Again, the problem the Adapter pattern is good at solving is incompatible interfaces. In this example, the Adaptee class does not implement the target interface. The solution will be to implement an intermediary class, an Adapter, that will implement the target interface on behalf of the Adaptee. Using polymorphism, the client can use either the Target interface or the Adapter class with little concern over which is which.

Target

Start off with the Target interface. The Target interface describes the behavior that your object needs to exhibit. It is possible in some cases to just implement the Target interface on the object. In some cases it is not. For example, the interface could have several methods, but you need custom behavior for only one. The java.awt package provides a Window adapter for just this purpose. Another example might be that the object you want to adapt, called the Adaptee, is vendor or legacy code that you cannot modify:

package wrox.pattern.adapter;

public interface Tricks {

public void walk();
public void run();
public void fetch();
}

Client

Next, look at the client code using this interface. This is a simple exercise of the methods in the interface. The compete() method is dependent on the Tricks interface. You could modify it to support the Adaptee interface, but that would increase the complexity of the client code. You would rather leave the client code unmodified and make the Adaptee class work with the Tricks interface:

public class DogShow {

public void compete(Tricks target){
target.run();
target.walk();
target.fetch();
}
}

Adaptee

Now the Adaptee is the code that you need to use, but it must exhibit the Tricks interface without implementing it directly:

package wrox.pattern.adapter;

public class OldDog {
String name;

public OldDog(String name) {
this.name= name;
}
public void walk() {
System.out.println("walking..");
}
public void sleep() {
System.out.println("sleeping..");
}
}

Adapter

As you can see from the OldDog class, it does not implement any of the methods in the Tricks interface. The next code passes the OldDog class to the Adapter, which does implement the Tricks interface:

package wrox.pattern.adapter;

public class OldDogTricksAdapter implements Tricks {
private OldDog adaptee;

public OldDogTricksAdapter(OldDog adaptee) {
this.adaptee= adaptee;
}
public void walk() {
System.out.println("this dog can walk.");
adaptee.walk();
}
public void run() {
System.out.println("this dog doesn't run.");
adaptee.sleep();
}
public void fetch() {
System.out.println("this dog doesn't fetch.");
adaptee.sleep();
}
}

The Adapter can be used anywhere that the Tricks interface can be used. By passing the OldDogTricksAdapter to the DogShow class, you are able to take advantage of all the code written for the Tricks interface as well as use the OldDog class unmodified.

The next section of code looks at how to establish the associations and run the example:

package wrox.pattern.adapter;

public class DogShow {
//methods omitted.

public static void main(String[] args) {

OldDog adaptee = new OldDog("cogswell");
OldDogTricksAdapter adapter = new OldDogTricksAdapter(adaptee);
DogShow client = new DogShow();
client.compete(adapter);

}
}

Model-View-Controller

The purpose of the Model-View-Controller (MVC) pattern is to separate your user interface logic from your business logic. By doing this it is possible to reuse the business logic and prevent changes in the interface from affecting the business logic. MVC, also known as Model-2, is used extensively in web development. For that reason, Chapter 8 is focused completely on this subject. You can also learn more about developing Swing clients in Chapter 4. Figure 3-8 shows the class structure of the Model-View-Controller pattern along with the classes implementing the pattern in this example.

Click to collapse
Figure 3-8

This pattern example will be a simple Swing application. The application will implement the basic login functionality. More important than the functionality is the separation of design principles that allow the model (data), controller (action), and the view (swing form) to be loosely coupled together.

Model-View-Controller is actually more than a simple pattern. It is a separation of responsibilities common in application design. An application that supports the Model-View-Controller design principle needs to be able to answer three questions. How does the application change the model? How are changes to the model reflected in the view? How are the associations between the model, view, and controller classes established? The next sections show how these scenarios are implemented in this example using a Swing application.

Scenario 1: Changing the Model

Changes to the model are pushed from the outside in. The example uses Java Swing to represent the interface. The user presses a button. The button fires an event, which is received by the controlling action. The action then changes the model (see Figure 3-9).

Figure 3-9

Scenario 2: Refreshing When the Model Changes

The second scenario assumes that the model has been updated by an action. The views might need to know this information, but having the model call the view directly would break the MVC separation principle requiring the model to have knowledge of the view. To overcome this, Java provides the Observer Design pattern, allowing changes from the model to "bubble out" to the view components. All views that depend on the model must register as a ChangeListener. Once registered, the views are notified of changes to the model. The notification tells the view to pull the information it needs directly from the model (see Figure 3-10).

Figure 3-10

Scenario 3: Initializing the Application

The third scenario shows how to initialize the action, model, and view objects and then establish dependencies between the components (see Figure 3-11).


Figure 3-11

The views are registered with the model and the actions are registered with the views. The application class coordinates this.

Having discussed the collaboration scenarios between the model, view, and controller components, the next sections delve into the internals of each component, starting with the model.

Model

The Model can be any Java object or objects that represent the underlying data of the application, often referred to as the domain model. This example uses a single Java object called Model.

The functionality of the Model in this example is to support a login function. In a real application, the Model would encapsulate data resources such as a relational database or directory service:

package wrox.pattern.mvc;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class Model {

The first thing of interest in the Model is the PropertyChangeSupport member variable. This is part of the Event Delegation Model (EDM) available since JDK 1.1. The EDM is an event publisher-subscriber mechanism. It allows views to register with the Model and receive notification of changes to the Model's state:

private PropertyChangeSupport changeSupport= new PropertyChangeSupport(this);
private boolean loginStatus;
private String login;
private String password;
public Model() {
loginStatus= false;
}
public void setLogin(String login) {
this.login= login;
}
public void getPassword(String password) {
this.password= password;
}
public boolean getLoginStatus() {
return loginStatus;
}

Notice that the setLoginStatus() method fires a property change:

public void setLoginStatus(boolean status) {
boolean old= this.loginStatus;
this.loginStatus= status;
changeSupport.firePropertyChange("model.loginStatus", old, status);
}
public void login(String login, String password) {
if (getLoginStatus()) {
setLoginStatus(false);
} else {
setLoginStatus(true);
}
}

This addPropertyChangeListener() is the method that allows each of the views interested in the model to register and receive events:

  public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
}

Notice that there are no references to any user interface components from within the Model . This ensures that the views can be changed without affecting the operations of the model. It's also possible to build a second interface. For example, you could create an API using Web Services to allow automated remote login capability.

View

The View component of the application will consist of a Swing interface. Figure 3-12 shows what the user will see when the application is run.


Figure 3-12

There are two JPanel components that make up the user interface. The first is the CenterPanel class that contains the login and password text boxes. The second is the WorkPanel that contains the login and exit command buttons as well as the CenterPanel.

The CenterPanel is a typical user data entry form. It's important to notice that there is no code to process the login in this class. Its responsibility is strictly user interface:

package wrox.pattern.mvc;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CenterPanel extends JPanel {

private JTextField login= new JTextField(15);
private JTextField password= new JTextField(15);

public CenterPanel() {
setLayout(new GridLayout(2, 2));
add(new JLabel("Login:"));
add(login);
add(new JLabel("Password:"));
add(password);
}
public String getLogin() {
return login.getText();
}
public String getPassword() {
return password.getText();
}
}

The next user interface component, WorkPanel, contains CenterPanel. Notice that there are no references to the WorkPanel from the CenterPanel. This is an example of composition, allowing the CenterPanel to be switched out for another form or viewed in a different frame:

package wrox.pattern.mvc;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

As you can see from the class declaration, the WorkPanel is a Swing component. In addition, it also implements the PropertyChangeListener interface. This allows the WorkPanel to register with the application model and have change notifications published to it when the Model changes. The WorkPanel is registered with the Model as a PropertyChangeListener. This allows the interface to change without affecting the domain Model, an example of low-coupled design:

public class WorkPanel extends JPanel implements PropertyChangeListener {
private Model model;

private JPanel center;
private JPanel buttonPanel= new JPanel();
private JLabel loginStatusLabel= new JLabel(" ");

public WorkPanel(JPanel center, Model model) {
this.center= center;
this.model= model;
init();
}
private void init() {
setLayout(new BorderLayout());
add(center, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
add(loginStatusLabel, BorderLayout.NORTH);
}

When the Model changes, the propertyChange() method is called for all classes that registered with the Model:

public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("model.loginStatus")) {
Boolean status= (Boolean)evt.getNewValue();
if (status.booleanValue()) {
loginStatusLabel.setText("Login was successful");
} else {
loginStatusLabel.setText("Login Failed");
}
}
}

The addButton() method allows you to do two things. First, you can configure any number of buttons. Second, it provides the action classes. They specify the work each performs when the button is pressed. The action represents the final part of the MVC pattern: the Controller. The Controller is discussed in the next section:

  public void addButton(String name, Action action) {
JButton button= new JButton(name);
button.addActionListener(action);
buttonPanel.add(button);
}

}

Controller

The purpose of the Controller is to serve as the gateway for making changes to the Model. In this example, the Controller consists of two java.swing.Action classes. These Action classes are registered with one or more graphical components via the components' addActionListener() method. There are two Action classes in this application. The first attempts to login with the Model. The second exits the application:

package wrox.pattern.mvc;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;

The LoginAction extends the AbstractionAction and overrides the actionPerformed() method. The actionPerformed() method is called by the component, in this case the command button, when it is pressed. The action is not limited to registration with a single user interface component. The benefit of separating out the Controller logic to a separate class is so that the action can be registered with menus, hotkeys, and toolbars. This prevents the action logic from being duplicated for each UI component:

public class LoginAction extends AbstractAction {

private Model model;
private CenterPanel panel;

It is common for the Controller to have visibility of both the Model and the relevant views; however, the model cannot invoke the actions directly. Ensuring the separation of business and interface logic remains intact:

  public LoginAction(Model model, CenterPanel panel) {
this.model= model;
this.panel = panel;
}
public void actionPerformed(ActionEvent e) {
System.out.println("Login Action: "+ panel.getLogin() +" "+ panel.getPassword()
);
model.login(panel.getLogin(), panel.getPassword());
}
}

The ExitAction strictly controls the behavior of the user interface. It displays a message when the Exit button is pressed confirming that the application should close:

package wrox.pattern.mvc;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ExitAction extends AbstractAction {

public void actionPerformed(ActionEvent e) {

JFrame frame= new JFrame();
int response= JOptionPane.showConfirmDialog(frame,
"Exit Application?",

"Exit",JOptionPane.OK_CANCEL_OPTION);
if (JOptionPane.YES_OPTION == response) {
System.exit(0);
}
}
}

Finally, you can view the Application class. The Application class is responsible for initialization, and it creates the associations that establish the MVC separation of logic design principles:

package wrox.pattern.mvc;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

public class Application extends JFrame {
private Model model;

The Swing application creates an association to the Model class, shown in the following code in the application constructor:

  public Application(Model model) {
this.model= model;

Then, create the Views to display the Swing interface:

    CenterPanel center= new CenterPanel();
WorkPanel work= new WorkPanel(center, model);

Create the Action classes that represent the controller and register them with the command buttons:

    work.addButton("login", new LoginAction(model, center));
work.addButton("exit", new ExitAction());
model.addPropertyChangeListener(work);
setTitle("MVC Pattern Application");

Use Swing housekeeping to display the application:

    getContentPane().add(work);
pack();
show();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
Model model= new Model();
Application application= new Application(model);
}
}

The Model-View-Controller pattern is a combination of best practices in software design. It prompts a separation of concern between the user interface and business layers of an application. This example covered a number of design patterns: composition, action, and event publish-subscribe. The next pattern is the Command pattern, which provides a consistent means of handling user requests.

Command

The Command pattern provides a standard interface for handling user requests. Each request is encapsulated in an object called a command. Figure 3-13 shows the classes involved in the Command pattern.


Figure 3-13

The three classes of the command pattern are the Command, CommandManager, and Invoker. The Command class represents an encapsulation of a single behavior. Each behavior in an application, such as save or delete, would be modeled as a command. In that way the behavior of an application is a collection of command objects. To add behavior to an application, all a developer needs to do is implement additional command objects. The next component in the Command pattern is the CommandManager. This class is responsible for providing access to the commands available to the application. The final component is the Invoker. The Invoker is responsible for executing the command classes in a consistent manner. The next section looks at the anatomy of the Command class.

Command

The first part of the Command pattern is the Command interface identified by a single method:

package wrox.pattern.command;

public interface Command {

public void execute();
}

The life cycle is different from calling a typical method. For example, if you need to pass in an object parameter like the following method:

public void getTotal(Sale) {
//calculate the sale.
}

As a command you would write the following:

public CalculateSale implements Command {
private Sale sale;

public void setSale(Sale sale) {
this.sale = sale;
}
public void execute() {
// calculate the sale.
}

For the purpose of the example, use an empty command to demonstrate the interaction between the classes in this pattern:

package wrox.pattern.command;

public class DefaultCommand implements Command {

public void execute() {
System.out.println("executing the default command");
}
}

The next section looks at the class that manages the command for an application.

CommandManager

The CommandManager class will process all requests. Using a HashMap, all of the commands will be initialized before requests are processed, then retrieved by name. They are stored using the add() method, and retrieved through the getCommand() method:

package wrox.pattern.command;
import java.util.HashMap;
import java.util.Map;
public class CommandManager {
private Map commands= new HashMap();

public void add(String name, Command command) {
commands.put(name, command);
}
public Command getCommand(String name) {
return (Command)commands.get(name);
}
}

Invoker

A standalone client will demonstrate the execution of the Command pattern. When the Client constructor is called it adds the DefaultCommand to the manager:

package wrox.pattern.command;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Client {
private CommandManager manager= new CommandManager();

public Client() {
manager.add("default", new DefaultCommand());
}

Here, the command mapping has been hard coded. A more robust implementation would initialize the command map from a resource file:

 <commands>
<command name="default" class="wrox.Pattern.command.DefaultCommand" />
</commands>

Then, as requests are received by the invoke(String name) method, the command name is looked up in the CommandManager and the Command object is returned:

  public void invoke(String name) {
Command command= manager.getCommand(name);
command.execute();
}

public static void main(String[] args) {
Client client= new Client();
client.invoke("default");
}
}

This is an important part of most web frameworks like Struts or WebWork. In WebWork there is a specific Command pattern component called xWork, which is described in detail in Chapter 8. By handling each request as a Command object, it is possible to apply common services to each cmmand. Some common services could be things such as security, validation, and auditing. The next section of code extends the current Command pattern and implements a ManagedLifecycle interface. This interface will define a set of methods that are called during each request:

package wrox.Pattern.command;

import java.util.Collection;
import java.util.Map;

public interface ManagedLifecycle extends Command {

public void initialize();
public void setApplicationContext(Map context);

public boolean isValidated();
public Collection getErrors();
public void destroy();
}

The ManagedLifecycle interface is a contract between the Command object and the client code.

The following is an example command that implements the ManagedLifecycle interface:

package wrox.pattern.command;
import java.util.Collection;
import java.util.Map;
import java.util.HashMap;

public class ManagedCommand implements ManagedLifecycle {
private Map context;
private Map errors= new HashMap();
public void initialize() {
System.out.println("initializing..");
}
public void destroy() {
System.out.println("destroying");
}
public void execute() {
System.out.println("executing managed command");
}
public boolean isValidated() {
System.out.println("validating");
return true;
}
public void setApplicationContext(Map context) {
System.out.println("setting context");
this.context= context;
}
public Collection getErrors() {
return errors.getValues();
}
}

The following code shows initialization and invocation of two types of commands, the standard and managed:

package wrox.pattern.command;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Client {
private Map context= new HashMap();
private CommandManager manager= new CommandManager();

public Client() {
manager.add("default", new DefaultCommand());

A new ManagedCommand has been added to the CommandManager:

    manager.add("managed", new ManagedCommand());
}
public void invoke(String name) {
Command command= manager.getCommand(name);

Next, a check is put in place to determine whether the command being executed implements the ManagedLifecycle interface:

  if (command instanceof ManagedLifecycle) {
ManagedLifecycle managed= (ManagedLifecycle)command;
managed.setApplicationContext(context);
managed.initialize();
if (managed.isValidated()) {
managed.execute();
} else {
Collection errors = managed.getErrors();
}
managed.destroy();
} else {
command.execute();
}
}

The calling sequence of the ManagedLifecycle is richer with functionality compared with its single method version. First it passes required application data, calls the initialize method, performs validation, and then calls the execute() method.


Important 

Allowing the client Invoker to pass resources to the command is a very powerful concept referred to as inversion of control (IOC), or dependency injection. This eliminates the need for the Command class to look up services and resources that are available to the invoker. (A good example of this is shown Chapter 8.)

Strategy

The Strategy pattern allows you to replace algorithms on the fly. To implement the solution, you represent each algorithm as a Strategy class. The application then delegates to the current Strategy class to execute the strategy-specific algorithm. Figure 3-14 shows the UML for the Strategy pattern alongside the example for this section.


Figure 3-14

A common mistake in domain modeling is the overuse of subtyping. A subtype should be created only when a specific "is-a" type relationship can be described between a subtype and its super-type. For example, when modeling a person within a domain model, it is tempting to create a subtype for each type of person. There is no wrong way of modeling a problem, but in this case each person can take on several roles. This example looks at buyer and seller roles any person might participate in at a given time. This doesn't pass the "is-a" relationship test for subtyping. It is fitting that a person's behavior varies by his role; this concept can be expressed using the Strategy pattern.

The example application in this section looks at the roles of buyers and sellers, showing how their differing behavior can be abstracted out into a strategy.

Locking each person into one role or the other is a mistake. The ability to switch between the behaviors of classes in a class hierarchy is the motivation for using the Strategy pattern. Figure 3-15 shows the wrong way to model the "plays a role" relationship.



Figure 3-15

The Strategy pattern is made up of an interface that defines the pluggable behavior, implementing subclasses to define the behavior and then an object to make use of the strategy.

Strategy

The solution is to model each role as a class and delegate role-specific behavior from the Person class to the Role current state. First, look at the behavior that will differ by the current state object. The example uses the interface Role to declare the strategy behavior, and the two concrete classes, Buyer and Seller, to implement the differing behavior.

To provide a little context to the example, the Buyer and Seller are trying to agree on a product price. The isSatisified() method is passed a Product and a Price and both parties must determine if the deal is acceptable:

package wrox.pattern.strategy;

public interface Role {

public boolean isSatisfied(Product product, double price);
}

Of course, the Seller and Buyer have differing objectives. The Seller is looking to make a profit, setting a 20 percent profit margin on any products sold. The following code makes that assumption:

package wrox.pattern.strategy;
public class Seller implements Role {

/*
* Seller will be happy if they make 20% profit on whatever they sell.
* (non-Javadoc)
* @see wrox.Pattern.strategy.Role#isSatisfied(wrox.Pattern.strategy.Product,
double)
*/
public boolean isSatisfied(Product product, double price) {
if (price product.getCost() > product.getCost() * .2) {
return true;
} else {
return false;
}
}
}

The Buyer, on the other hand, is looking for a product that is within a spending limit. It is important to note that the Buyer class is not limited to the methods described by the Role interface, making it possible to establish the limit member variable in the Buyer class that is not present in the Seller class.

The algorithm for what is acceptable is an arbitrary part of this example, but it is set so the Buyer cannot spend above the chosen limit and will not pay more that 200 percent of the initial product cost. The role of Buyer is expressed in the isSatisfied() method:

package wrox.Pattern.strategy;
public class Buyer implements Role {

private double limit;

public Buyer(double limit) {
this.limit= limit;
}
/*
* The buyer is happy if he can afford the product,
* and the price is less then 200% over cost.
* @see wrox.Pattern.strategy.Role#isSatisfied(wrox.Pattern.strategy.Product,
double)
*/
public boolean isSatisfied(Product product, double price) {
if ( price < limit && price < product.getCost() * 2) {
return true;
} else {
return false;
}

}
}

The code example that follows uses a class for the abstraction of a product. It's a data object that is part of the scenario. The code is as follows:

package wrox.pattern.strategy;
public class Product {
private String name;
private String description;
private double cost;

public Product(String name, String description, double cost) {
this.name = name;
this.description = description;
this.cost = cost;
}
// Setters and Getter Omitted.

The next section looks at the class that uses the pluggable strategy.

Context

Next, examine the Person class that manages the Role objects. First, the Person class has an association with the Role interface. In addition, it is important to note that there is a setter and getter for the Role.This allows the person's roles to change as the program executes. It's also much cleaner code. This example uses two roles: Buyer and Seller. In the future, other Role implementing objects such as Wholesaler, Broker, and others can be added because there is no dependency to the specific subclasses:

package wrox.pattern.strategy;

public class Person {
private String name;
private Role role;
public Person(String name) {
this.name= name;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role= role;
}

Another key point is that the satisfied method of the Person class delegates the Role-specific behavior to its Role interface. Polymorphism allows the correct underlying object to be chosen:

  public boolean satisfied(Product product, double offer) {
return role.isSatisfied(product, offer);
}
}

Now, the code of the pattern has been implemented. Next, view what behavior an application can exhibit by implementing this pattern. To start, you can establish Products, People, and Roles:

package wrox.pattern.strategy;

public class Person {
// previous methods omitted.

public static void main(String[] args) {
Product house= new Product("house", "4 Bedroom North Arlington", 200000);
Product condo= new Product("condo", "2 Bedroom McLean", 100000);
Person tim= new Person("Tim");
Person allison= new Person("Allison");

You are buying and selling houses. The next step is to establish initial roles and assign the roles to the people. The people will then exhibit the behavior of the role they have been assigned:

    tim.setRole(new Buyer(500000));
allison.setRole(new Seller());

if (!allison.satisfied(house, 200000)) {
System.out.println("offer of 200,000 is no good for the seller");
}
if (!tim.satisfied(house, 600000)) {
System.out.println("offer of 600,000 is no good for the buyer");
}
if (tim.satisfied(house, 390000) && allison.satisfied(house, 390000)) {
System.out.println("They Both agree with 390,000 ");

To further demonstrate the capabilities of the Strategy pattern, switch the initial Seller to the Buyer by calling setRole() on the Person object. It is possible to switch to a Buyer without modifying the Person object:

      allison.setRole(new Buyer(190000));
if (allison.satisfied(condo, 110000)) {
System.out.println("As a buyer she can afford the condo ");
}
}
}
}

By implementing the Strategy pattern, it is possible to change an object's behavior on the fly with no effect on its implementation. This is a very powerful tool in software design. In the next section, the composite patterns build on the same principle of abstracting behavior to treat a class hierarchy with a single common interface.

Composite

The Composite design pattern allows you to treat a collection of objects as if they were one thing. In this way you can reduce the complexity of the code required if you were going to handle collections as special cases. Figure 3-16 shows the structure of the Composite pattern in conjunction with the classes implementing the pattern in this example.


Figure 3-16

The example used here to demonstrate this behavior is a portfolio management system that consists of stocks and mutual funds. A mutual fund is a collection of stocks, but you would like to apply a common interface to both stocks and mutual funds to simplify the handling of both. This allows you to perform operations such as calculate Fair Market Value, buy, sell, and assess percent contribution with a common interface. The Composite pattern would clearly reduce the complexity of building these operations. The pattern consists of the Component, Leaf, and Composite classes. Figure 3-16 should look similar to Figure 3-6, where you were first introduced to the inheritance loop concept.

Component

First is the component interface; it declares the common interface that both the single and composite nodes will implement. The example is using fairMarketValue, an operation that can be calculated over stocks, mutual funds, and portfolios:

package wrox.pattern.composite;

public interface Asset {

public double fairMarketValue();
}

Leaf

The Leaf class represents the singular atomic data type implementing the component interface. In this example, a Stock class will represent the leaf node of the pattern. The Stock class is a leaf node in that it does not hold a reference to any other Asset objects:

package wrox.pattern.composite;

public class Stock implements Asset {

private String name;

private double price;
private double quantity;

public Stock(String name, double price, double quantity) {
this.name= name;
this.price= price;
this.quantity= quantity;
}

Stock price is calculated by multiplying share price and quantity:

  public double fairMarketValue() {

return price * quantity;
}
}

Composite

The following section declares the Composite object called CompositeAsset. Notice that CompositeAsset is declared abstract. A valid composite asset, such as a mutual fund or portfolio, extends this abstract class:

package wrox.pattern.composite;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public abstract class CompositeAsset implements Asset {
private List assets= new ArrayList();

public void add(Asset asset) {
assets.add(asset);
}

Iterate through the child investments. If one of the child investments also happens to be a composite asset, it will be handled recursively without requiring a special case. So, for example, it would be possible to have a mutual fund comprising mutual funds:

  public double fairMarketValue() {
double total = 0;
for (Iterator i= assets.iterator(); i.hasNext();) {
Asset asset= (Asset)i.next();
total = total + asset.fairMarketValue();
}
return total;
}
}

Once that is complete, what follows is to build the concrete composite objects: MutualFund andPortfolio. Nothing significant is required for the MutualFund class; its behavior is inherited from theCompositeAsset:

package wrox.pattern.composite;

public class MutualFund extends CompositeAsset{

private String name;

public MutualFund(String name) {
this.name = name;
}

}

The Portfolio class extends CompositeAsset as well; the difference is that it calls the superclass directly and modifies the resulting calculation for fair market. It subtracts a two-percent management fee:

package wrox.pattern.composite;

public class Portfolio extends CompositeAsset {
private String name;
public Portfolio(String name) {
this.name= name;
}
/* Market value Management Fee
* @see wrox.Pattern.composite.CompositeAsset#fairMarketValue()
*/
public double fairMarketValue() {
return super.fairMarketValue() super.fairMarketValue() * .02;
}

}

The only thing left to do is exercise the code. The next class is of an Investor. The Investor is the client code taking advantage of the Composite design pattern:

package wrox.pattern.composite;

public class Investor {
private String name;
private Portfolio porfolio;
public Investor(String name, Portfolio portfolio) {
this.name= name;
this.porfolio= portfolio;
}

By calling the fair market value on the investor's portfolio, the Composite pattern will be able to traverse the collection of stocks and mutual funds to determine the value of the whole thing without worrying about the object structure:

  public double calcNetworth(){

return porfolio.fairMarketValue();

}

public static void main(String[] args) {
Portfolio portfolio= new Portfolio("Frequently Used Money");
Investor investor= new Investor("IAS", portfolio);

portfolio.add(new Stock("wrox", 450, 100));

MutualFund fund= new MutualFund("Don Scheafer's Intellectual Capital");
fund.add(new Stock("ME", 35, 100));
fund.add(new Stock("CV", 22, 100));
fund.add(new Stock("BA", 10, 100));
portfolio.add(fund);

double total =investor.calcNetworth();

System.out.println("total ="+ total);
}
}

With the Composite pattern, it is very easy to simplify operations over complex data structures.

Comments