CSC/ECE 517 Fall 2010/ch3 3h az: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 20: Line 20:


==Generic Implementation and participant of Strategy pattern:==
==Generic Implementation and participant of Strategy pattern:==
 
Strategy:   
Strategy:   
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.
        – Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a  
ConcreteStrategy  
          ConcreteStrategy.
* Implements the algorithm using the Strategy interface.
 
Context
ConcreteStrategy  
* Is configured with a ConcreteStrategy object
        – Implements the algorithm using the Strategy interface.
* Maintains a reference to a Strategy object
 
* May define an interface that lets Strategy access its data.
Context
        – Is configured with a ConcreteStrategy object
        – Maintains a reference to a Strategy object
        – May define an interface that lets Strategy access its data.


=Illustration of Strategy pattern through real world example=
=Illustration of Strategy pattern through real world example=

Revision as of 22:22, 6 October 2010

Objective

Objective of this Wiki Chapter is that the reader should be get an overview of

  1. Strategy pattern, when and how to use strategy pattern.
  2. Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.

Introduction

A design pattern is a general reusable solution to a commonly occurring problem in software design. Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce multiple conditions statements. This helps in making the code more maintainable.

The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. As an algorithm evolves to handle more and more situations, it can become very complex and difficult to maintain, Strategy pattern can be used to resolve this situation.

When to use Strategy Pattern

  1. Many related classes differ only in their behavior.
  2. You need different variants of an algorithm. For example, defining different algorithms based on space-time tradeoffs.
  3. A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.

Generic Implementation and participant of Strategy pattern:

Strategy:

  • Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.

ConcreteStrategy

  • Implements the algorithm using the Strategy interface.

Context

  • Is configured with a ConcreteStrategy object
  • Maintains a reference to a Strategy object
  • May define an interface that lets Strategy access its data.

Illustration of Strategy pattern through real world example

To illustrate strategy pattern let’s consider this example of basic file transfer scenario,
where we want to compress the file before transmission. 
File compression can be achieved by different algorithm for different types of file. 
Image files and text files can be compresses by different algorithm,
here we would  like demonstrate how this algorithm can be encapsulated and 
helps in selecting the algorithms without the conditional statements.

Illustration of Strategy pattern in Static Object Oriented Language language

Conclusion