Wednesday, 19 August 2009 13:05

Exploiting Patterns in Java

Rate this item
(1 vote)

Why Patterns Are Important

A pattern is a proven solution to a software problem enabling reuse of software at the design level. The purpose of a pattern is to conceptually pair a problem with its design solution and then apply the solution to similar problems. Code-level reuse of software is desirable, but design-level reuse is far more flexible.

Patterns are one of the greatest resources you will have in the design of object-oriented software. They will definitely help you to master the Java programming language, be more productive, and develop effective Java solutions.

Building Patterns with Design Principles

At the core of any pattern is a collection of design principles. This section looks at a simple and unconventional approach to building patterns from the ground up. The approach is to start with a simple design and gradually make changes so the design is more flexible. Each step uses the object-oriented tools available to you in Java, as well as one or more of the design principles discussed in the previous section. Each design change becomes a step in building more complex design patterns. By following the exercises in this section, it will be clear how applying design principles makes software more flexible. This allows you to understand the mechanics behind patterns a small piece at a time.

This section starts off with the design of a single class. From this single class design, an association is added, followed by an interface. These two steps add flexibility to the single class design. Understanding this flexibility has important ramifications for understanding design patterns. The final section shows an example of merging the concepts of association and inheritance, which is common in a number of design patterns.

Designing a Single Class

A single class doesn’t constitute a design pattern, but it is a design. And there is nothing wrong with simplicity. Part of the design process is assigning responsibility to an object as in Figure 3-1.

Single Class Pattern


It is very common for a class to become bloated with several methods not related to the abstraction the class represents. This can cause dependency problems down the line and does not fit with the high cohesion design principle. In this example, the Teacher class contains several methods related to teacher responsibilities. The solution is to push to the right or delegate the methods that do not belong with the abstraction. The phrase “do not belong” is subjective. Any design decision could be wrong. As long as you justify it with sound OO principles, don’t worry—you can always change it later when the problem is clearer.

Creating an Association between Classes

All the teacher responsibilities have been delegated to a class called TeacherResponsibilities. Again visualize the methods being pushed to the right or delegated to another class. Figure 3-2 shows how responsibility has been delegated through an association.
Creating an Association between Classes
For the TeacherResponsibilities class to do work on behalf of the Teacher class, an association has to be created. The Teacher object holds a reference to the TeacherResponsibilities.

There are basically three ways this can happen:

1. The TeacherResponsibilities object is passed to the Teacher object as a parameter:
Teacher teacher = new Teacher(“Heather”);
TeacherResponsibilities responsibilities= new TeacherResponsibilities ();
teacher.setResponsibilities ( responsibilities);
 
 
2. The Teacher object creates the TeacherResponsibilities object:
public class Teacher {
private TeacherResponsibilities responsibilities = new TeacherResponsibilites();
}
 
 
3. The TeacherResponsibilites object is passed back from a method call:
public class Teacher {
private TeacherResponsibilities responsibilities;
public Teacher() {
Administration admin = new Administration();
responsibilities = admin.getResponsibilites();
}
}

These three methods determine the visibility an object shares with another in making up an association. The design might be done, but there is another design principle to address: loose-coupling. In specifying an association, a tight dependency between the Teacher and the TeacherResponsibilites classes has been created. The relationship is restricted to the Teacher and the TeacherResponsibilites types. That would be fine, except that it may be felt that the responsibilities will change over time. How do you loosen the relationship and address this volatility? The answer is to push up an interface.

Creating an Interface

An interface is a software contract between classes. By using the interface, the Current class is allowed to provide the implementation. If in the future the implementation changes, you can replace the current class with a new class. Because the Teacher class only depends on the Responsibilities interface, the Teacher class will not need to be modified. The UML for this design is shown in Figure 3-3.
 
Creating an Interface

The next section combines delegation and inheritance, the concepts of the previous two sections, to create powerful object structures. An inheritance loop combines the pluggable functionality of inheritance with the separation of concerns gained with an association.

Creating an Inheritance Loop

By relating two classes with both an association and an inheritance, it is possible to create trees and graphs. Think of this as reaching up the class hierarchy. The inheritance relationship causes the nodes in the object structure to be polymorphic. In the example shown in Figure 3-4, a WorkFriends group can be manipulated using the same interface declared by the Person class. Another common example would be how files and folders on a file system have similar behavior. They both use common functionality such as copy, delete, and more. Composition, in the “Important Java Patterns” section of this chapter, is a good example of using an inheritance loop to allow type independent functionality.
Figure 3-4 shows the resulting class and object view of an inheritance loop. This is a common structure used in many design patterns including composition.

Interface Loop

An inheritance loop is referred to as reaching up the hierarchy, as depicted in Figure 3-4. By reaching up the hierarchy, you create a relationship known as reverse containment. By holding a collection of a superclass from one of its subclasses it is possible to manipulate different subtypes as well as collections with the same interface.

Figure 3-5 shows one subtle change to the example in Figure 3-4. By changing the cardinality of the association between the super- and subtypes to many-to-many, it is possible to represent graphs as well as trees.
Interface Loop 2
 

Finally, Figure 3-6 adds subtype relationships to the inheritance loop, allowing the representation of a complex data structure with methods that can be invoked with a polymorphic interface.You have also created a common interface for each responsibility, allowing you to add new responsibilities with limited impact to the application.

The purpose of this section was to learn tricks to understanding patterns. By creating associations and using inheritance, you have been able to build some complex designs from these principles. You learned to apply these principles by remembering simple actions: push to the right, push up, and reach up. Learning these tricks will help you understand the well-known patterns in the next section.
 

Important Java Patterns

This section shows examples of very important and well-known patterns. By learning each of these patterns, you will develop your pattern vocabulary and add to your software design toolbox. Each pattern discussed subsequently includes a description of the problem the pattern solves, the underlying principles of design at work in the pattern, and the classes that make up the pattern and how they work together.

The focus of this section is not to describe patterns in a traditional sense, but instead to provide code and concrete examples to demonstrate the types of problems that each pattern can solve. All the patterns discussed in this section are oft-adapted GoF patterns.

The patterns in this section include Adapter, Model-View-Controller, Command, Strategy, and Composite. Each pattern is discussed with a text description and a diagram showing the pattern as well as the example classes fulfilling their corresponding pattern role. The key takeaway in each case is to recognize how these classes collaborate to a solve specific problem.

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.

 
Adapter

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.

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.

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

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.

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 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);
}

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.Command Pattern

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 command. 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.

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.
 
Strategy

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.
Strategy Pattern

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.


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.
 
Composite Pattern
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 Composite Asset 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 and Portfolio. Nothing significant is required for the MutualFund class; its behavior is inherited from the CompositeAsset:

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);
}
}


Last modified on Thursday, 20 August 2009 09:36
Vicky

Vicky

E-mail: This e-mail address is being protected from spambots. You need JavaScript enabled to view it