Posts tagged: Strategy Pattern

Quick and Short: Design Pattern

By , March 10, 2010

This article is written under ‘Quick and Short’ edition. In this article I will explain about verious Design Patterns.

Read more Quick and Short articles.

Design Pattern

Design patterns are recognized solutions to common problems defined originally by the Gang of Four programmers.

Design patterns are tried and tested solutions for recurring problems in a given context. So basically you have a problem context and the proposed solution for the same. Design patterns existed in some or other form right from the inception stage of software development. Let’s say if you want to implement a sorting algorithm the first thing comes to mind is bubble sort. So the problem is sorting and solution is bubble sort. Same holds true for design patterns.

The various patterns are commonly divided into several different groups depending on the nature of the design problem they intend to solve.

Creational Patterns

  • Factory - This pattern is used to create concrete class instances without specifying the exact class type.
  • Abstract Factory – This pattern is used to create concrete class instances without specifying the exact class type. The Abstract Factory Pattern provides a way to encapsulate a group of individual factories that have a common theme.
  • Singleton - This pattern insures that only a single instance of a given object can exist.
  • Builder – This pattern separate the construction of a complex object from its representation so that the same construction process can create different representations.
  • Prototype:- A fully initialized instance to be copied or cloned

Structural Patterns

  • Adapter – Convert the interface of a class into another interface clients expect. Adapter lets the classes work together that couldn’t otherwise because of incompatible interfaces
  • Bridge – Decouples an abstraction from its implementation so that the two can vary independently.
  • Composite – Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
  • Decorator – Allows an objects behavior to be altered at runtime.
  • Facade – Used to provide a simpler interface into a more complicated portion of code.
  • Proxy – Provides a Placeholder for another object to control access to it.

Behavioral Patterns

  • Flyweight – A fine-grained instance used for efficient sharing.
  • Chain of Responsibility – The chain of responsibility pattern is a way of communicating between objects.
  • Command - Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
  • Iterator – Provides a way to sequentially access aggregate objects without exposing the structure of the aggregate.
  • Mediator – The mediator pattern encapsulate the interaction between a set of objects.
  • Memento – Allows you to save the state of an object externally from that object.
  • Observer – Allows a single object to notify many dependent objects that its state has changed.
  • State – Allows an object to change its behaviour when its internal state changes.
  • Strategy – Allows multiple algorithms to be used interchangeably at runtime.
  • Visitor – The visitor design pattern enables us to create new operations to be performed on an existing structure.
  • Template Method -  Defines the skeleton of an algorithm then lets subclasses implement the behaviour that can vary.
  • Interpreter - A way to include language elements in a program.

——————————————————————————————————————————————————————

Readers are requested to add comment, and add more information in case they want.

Read more Quick and Short articles.

Strategy Pattern

By , September 14, 2009
Both Array and ArrayList provide the capability to sort the objects contained in the collection via the Sort method. In fact, ArrayList.Sort just calls Sort on the underlying array. These methods use the QuickSort algorithm. By default, the Sort method will use the IComparable implementation for each element to handle the comparisons necessary for sorting. Sometimes, though, it is useful to sort the same list in different ways. For example, arrays of strings might be sorted with or without case sensitivity. To accomplish this, an overload of Sort exists that takes an IComparer as a parameter; IComparer.Compare is then used for the comparisons. This overload allows users of the class to use any of the built-in IComparers or any of their own making, without having to change or even know the implementation details of Array, ArrayList, or the QuickSort algorithm.
Leaving the choice of comparison algorithm up to the user of the class like this is an example of the Strategy pattern. The use of Strategy lets a variety of different algorithms be used interchangeably. QuickSort itself only requires a way to compare objects to each other. By calling Compare through a provided interface, the caller is free to substitute whatever particular comparison algorithm fits its specific needs. The code for the QuickSort can remain unchanged. Read more »

Discover the Design Patterns You’re Already Using in the .NET Framework

By , September 14, 2009
Recently, Microsoft has placed increasing emphasis on design patterns. If you are unfamiliar with patterns, suddenly being inundated with new terms and foreign-looking UML diagrams can be overwhelming. This emphasis on patterns, however, doesn’t represent a shift in methodology so much as a change in vocabulary. The Microsoft® .NET Framework base class library (BCL) already makes extensive use of patterns, and you are probably familiar with the most common ones, even though you might not realize it yet.
In this article, I’ll cover a basic overview of several common design patterns and how they are used in the BCL and other areas of the .NET Framework. In doing so, you can discover some of the motivation for why the Framework is designed the way it is, as well as make the abstract concepts of the patterns themselves more intuitively understandable.
Most of the patterns I’ll be covering come from the canonical reference, Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, (Addison-Wesley, 1995). These authors are collectively known as the Gang of Four. The Gang of Four didn’t invent these patterns, but they documented and formalized the good work others had been doing since the beginning of software development. Read more »