Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Expo on Google News

SYS-CON.TV
Cloud Expo & Virtualization 2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Cloud Computing & Enterprise IT: Cost & Operational Benefits
How and Why is a Flexible IT Infrastructure the Key To the Future?
Click For 2008 West
Event Webcasts
Java Feature: Java GoF Behavioral Design Patterns
Solutions for object interactions

While creational patterns decouple a client from the objects it instantiates, behavioral patterns dictate the object interaction. If not carefully planned, coupling and cohesion can pose major design issues. My previous article, "Java GoF Creational Design Patterns" (JDJ, Vol. 9, issue 9), discussed creational patterns. This article will focus on behavioral patterns.

Knowledge of the behavioral pattern vocabulary will help designers come up with appropriate solutions for object interactions, minimizing or eliminating these issues. One basic thing to understand is that there are two kinds of patterns:

  • Class: Describes compile-time relationship between classes via inheritance
  • Object: Describes the runtime relationship between classes via composition
The patterns elicited in this article have been classified as either object or class based. Generally, object patterns allow more flexibility in design.

When working with design patterns, it's important to know not just how but also when to use them. A design pattern should occur naturally as a part of a design and shouldn't drive the design, but guide it. Overuse of any pattern is harmful, especially in a situation where you're biased, desperately trying to fit a pattern into the design. If you look at the Golden Hammer anti-pattern (an anti-pattern is the reverse of a design pattern), it states just that: when you are already well versed with a particular pattern, and overuse a technology or pattern, then learn or implement a more befitting technology/pattern to the domain/problem in question. It is prudent to look at the domain and problem context first and then use patterns to help in the design process.

At a bare minimum, you should strive for a loosely coupled design and program to an interface rather than an implementation. This leads to a good design without forcing you to apply design patterns in a problem context. Patterns are meant to aid designers, not compel the designers to use them.

It's also not a good idea to override an implemented method of a base class or derive from a concrete class. If you also adhere to not having a variable holding a reference to a concrete class, together this qualifies the dependency (or inversion) principal that can also be used as a guideline for design.

In accordance with my earlier article on creational patterns, I'll attempt to keep this one terse as well, simultaneously keeping ease of understanding as the denominator.

Table 1 lists the behavioral patterns with their intent. Our next step will be to validate each one with a laconic, yet usable template. Each pattern will have a standard definition, template, and description attached to it. I will then attach a real-world example in which the respective pattern shall be applicable. Together, these elements will help you understand the patterns from the perspective of a software manager, architect, designer, or developer.

Strategy Pattern
This pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It allows algorithms to vary independently of the clients that use it.

The Strategy pattern is widely used and suggests programming to an interface over implementation. It allows building dynamic software that can be changed with minimal impact on existing code.

Let's look at the template below:

  1. The first step is to define an interface Strategy. This defines a method that each ConcreteStrategy shall implement (there can be more than one ConcreteStrategy).
  2. The Context class encapsulates the Strategy interface.
  3. Now this is ready to be used. A client can create a Context object and dynamically assign the ConcreteStrategy at runtime.
Strategy is an object pattern. If this solution were to be approached via inheritance (as in a class pattern), it would not allow the flexibility to dynamically bind ConcreteStrategy to Context.

Template


package strategy;

public interface Strategy {

/**
* This method declaration must be implemented by the ConcreteStrategy
implementations.
*/
public void algorithmInterface();
}

package strategy;

public class Context {

/** stores the Strategy instance of the Context */
private final Strategy fStrategy;

/** * Constructor */
public Context(Strategy strategy) {super()
fStrategy = strategy;
}

/**
* This method invokes the algorithm interface of the current Strategy
*/
public void contextInterface() {fStrategy.algorithmInterface();
}
}

package strategy;

public class ConcreteStrategy implements Strategy {

public ConcreteStrategy() {super();
}

/**
* This method implements the algorithm operation defined by the Strategy
interface.
*/
public void algorithmInterface() {
// Write your algorithm code here ...
}

}
Example
Imagine a scenario in which a company is interviewing for different positions. The recruiter wants to schedule the interviews with the flexibility to change the position that a candidate might interview for. To apply the above template:

Strategy = Position
Context = Candidate
ConcreteStrategy1 = DotNET
ConcreteStrategy2 = J2EE
ConcreteStrategy3 = CPlusPlus
About Puneet Sangal
Puneet Sangal has been working with Java for more than six years, focusing on enterprise Web-based Java applications. He is a Sun Certified Programmer for platform 1.4.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Java Feature: Java GoF Behavioral Design Patterns. While creational patterns decouple a client from the objects it instantiates, behavioral patterns dictate the object interaction. If not carefully planned, coupling and cohesion can pose major design issues. My previous article, 'Java GoF Creational Design Patterns' (JDJ, Vol. 9, issue 9), discussed creational patterns. This article will focus on behavioral patterns.


Your Feedback
JDJ News Desk wrote: Java Feature: Java GoF Behavioral Design Patterns. While creational patterns decouple a client from the objects it instantiates, behavioral patterns dictate the object interaction. If not carefully planned, coupling and cohesion can pose major design issues. My previous article, 'Java GoF Creational Design Patterns' (JDJ, Vol. 9, issue 9), discussed creational patterns. This article will focus on behavioral patterns.
Latest Cloud Developer Stories
Swisscom, the Swiss telecom, is going into the cloud business. Its subsidiary Swisscom IT Services AG has signed up with Red Hat as a Certified Cloud Provider and launched a public cloud Infrastructure-as-a-Service (IaaS) cloud targeting enterprise-class customers primarily in ...
Apache Deltacloud, the Red Hat-contributed ReSTful API that abstracts differences between clouds so services on any cloud can be managed – provided of course there’s a driver – has graduated from the Apache Foundation’s incubator and is now a full-fledged Top-Level Project (TLP)....
In a surprise move on Tuesday, January 10, Oracle wheeled out its Big Data Appliance. That’s the one it said in October would be ready sometime in the first half. Only nobody believed it meant early in the first half. Heck, it’s not even clear anybody thought Oracle could make ...
Rackspace Hosting, the service leader in cloud computing, on Thursday announced its acquisition of SharePoint911, an industry leader in SharePoint consulting, training, and "JumpStart" services within SharePoint. The unification of both companies provides capabilities to deliver ...
CloudLinux, Inc., on Thursday released CafeFS 3, a virtualized file system for shared hosters that cages each customer within its own virtualized file system. CageFS becomes part of CloudLinux OS at no additional charge. CloudLinux OS, the only commercially-supported Linux OS m...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE

Breaking Cloud Computing News
As client demand for engagements increases, Revel Consulting (www.revelconsulting.com), a Kirkland, ...