Java Design Concepts

Design Concepts in Enterprise Systems using Java
  • Home
  • Design Concepts
  • Design Patterns
  • GlassFish
  • Java Enterprise Edition
  • Systems & Hardware

Design Pattern - Strategy Pattern

Byadmin on Jun 9, 2008 | In Gang Of Four Patterns

Intent

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. - Gof

Type

Object Behavioral

Solution

The Strategy Pattern is one of the less complex patterns defined by the Gang of Four. The Strategy Pattern first identifies the behaviors or algorithms that vary and separate them from the system that stays the same. These behaviors or algorithms are encapsulated in classes that implement a common interface. This enables the developer to program to an interface and not an implementation. The different algorithms are encapsulated in a concrete class (ConcreteStrategy). Each of these objects are referenced by classes (Context) through the common interface (Strategy).

The Strategy Pattern consists of a Strategy, ConcreteStrategy, Context and Client.

  • The Strategy interface defines the behavior that is common to all the concrete implementations. The Context will invoke a specific implementation through the Strategy interface.
  • The ConcreteStrategy encapsulates an implementation of a specific algorithm or behavior that is defined through the Strategy interface.
  • The Context provides certain services that is defined through by the Strategy interface and implemented by different ConcreteStrategy classes depending on the behavior. The Context class contains a reference through the Strategy interface to a ConcreteStrategy object.
  • The Client makes use of the Context class to invoke the different services. Depending on the association between the Context and ConcreteStrategy classes, different implementations will be invoked.

Structure

Design Pattern, Strategy Pattern, GOF

Java Sample Code

Download: Game Character Actions

The following example illustrates the use of the Strategy Design Pattern. The Game Character example illustrates the family of algorithms that define the Weapon and Victory Dance behavior of each of the different game characters.

The example consists of the following classes:

  • GameCharacter.java - (Context)
  • VictoryDanceBehaviour.java - (Strategy)
  • WeaponBehavior.java - (Strategy)
  • GameCharacterGordonFreeman.java - (Context)
  • GameCharacterSnakePlissken.java - (Context)
  • GameCharactherMasterChief.java - (Context)
  • VictoryDanceAirGuitar.java - (ConcreteStrategy)
  • VictoryDanceHeadButt.java - (ConcreteStrategy)
  • VictoryDanceJumping.java - (ConcreteStrategy)
  • WeaponBFG9000.java - (ConcreteStrategy)
  • WeaponChainsaw.java - (ConcreteStrategy)
  • WeaponRocketLauncher.java - (ConcreteStrategy)
  • GameControllerClient.java - (Client)

GameCharacter.java
The GameCharacter class is configured with a VictoryDanceBehaviour and WeaponBehavior class which are both Strategy types. The GameCharacter class maintains references to the Strategy objects through the VictoryDanceBehaviour and WeaponBehavior interfaces.

WeaponBehavior.java
The WeaponBehavior is used to illustrate the Strategy Pattern of GoF. The WeaponBehavior is the Strategy that declares an interface that is used by all the Weapon Algorithms. The GameCharacter object makes use of the WeaponBehavior interface to call the algorithms of the different Weapon Algorithms.

WeaponBFG9000.java
The WeaponBFG9000 class defines an algorithm that is invoked by the GameCharacter object through the WeaponBehavior (Strategy) interface.

GameControllerClient.java
The GameControllerClient contains a reference to the three different GameCharacter objects each with different weapon and victory dance behavior. The GameControllerClient object invokes the fight method in the GameCharacter class. This method invokes the different weapon and victory dance algorithms through the Strategy interfaces that is defined in the GameCharacter class.

Class Diagram Example

Design Pattern, Strategy Pattern, GOF

References

  • Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley, 1995
Tags: design patterns, gof, java, strategy
1 2 3 4 5 6 7 8 9 10 11 >>
  • Java Design Concepts

  • Java Design Concepts will provide information on how to design, implement and deploy system in a high availability enterprise environment. The design concepts include design patterns, and Object Oriented Programming (OO). Java technologies include J2EE, EJB, JAF, JMS, JSP, JSTL, JDBC etc...

  • Author

    Andre Mare is a J2EE Specialist and has more than seven years experience in development and design of enterprise systems. His CV will be attached soon.
  • View André Maré's profile on LinkedIn
  • Pages

    • Design Concepts
    • Design Patterns
    • GlassFish
    • Java Enterprise Edition
    • Systems & Hardware
  • Java Design Concepts

  • Cool Blogs I Read

    Naschenweng.info
  • Search




  • Blog Directory - Blogged Blog Directory
    Add to Technorati Favorites
  • JavaBlogs
  • South Africa's Top Sites
  • Afrigator
  • Locations of visitors to this page
  • XML Feeds

    • RSS 2.0: Posts, Comments
    • Atom: Posts, Comments
    • _sitemap: Posts, Comments
    What is RSS?

Contact | Powered by b2evolution + evoCamp skin
Credits: Blog Design | blogging software | hosts UK | adsense