difference between patterns RRS feed

  • Question

  • I have confusion on difference between Strategy,abstract factory, bridge and visitor pattern.

    I have visited but couldn't understand the difference. Can somebody please give me concrete differentiation betweeen bridge and visitor, bridge and abstract factory, visitor and startegy.

    If you could guide me to any of your favorite website on these design patterns which can give me idea about the difference, that would also be helpful.





    Thursday, June 24, 2010 9:50 AM

All replies

  • I am no pattern expert, I only really know about 10 that are fairly common.  Let me give you my 2 cents on Strategy and Abstract Factory.

    I think of Strategy as nothing more than fronting an "algorithm" with an interface, the idea being is that if you decide to swap out that "algorithm" with another, as long as the interface is maintained you don't have to change any code.  For example say you have a class that uses a class for compressing data such as ZipAlgorithm.Compress(something); What if tomorrow you need to swap out ZipAlgorithm for RarAlgoritm?  You'd have to change your client class that makes use of Zip Algorithm.  Instead you can have both ZipAlgorithm and RarAlgorithm implement some sort of ICompression interface.  Now since the interface is identical, you could inject either and make use of either algorithm without changing your client class:

    MyClientClass myC = new MyClientClass(new RarAlgorithm()); or you could do MyClientClass myC = new MyClientClass(new ZipAlgorithm());

    Now, the Abstract Factory is completely different.  It serves more of a purpose to create "families" or related classes.  I won't go into details of how it works because it's readily available on Google.  In essence I think of using AF when I come a cross a situation when I need to create "families" of like objects that I want to maintain the same interface.  For example.  If I am creating portfolio classes, and I have a family of 4 classes (TradeFile, AccountFile, SecurityFile, PositionFile).  Now the business requirement that I create these classes for different client systems such as Broker1 and Broker2, etc.  The catch is, I want all of them to maintain the same interfaces so I can easily swap them out when necessary. So at the end of the day my client class will work with some sort of ITradeFile, without having to care if it's from Broker1 or Broker2.  This once again makes it easy to add new broker implementations when we introduce say Broker3 without having to change the client code.


    Thursday, June 24, 2010 1:49 PM
  • I woyld recommend to visit this web-site for more information on Gang-of-four patterns.

    Vidya Vrat Agarwal. http://dotnetpassion.blogspot,com
    Thursday, June 24, 2010 7:09 PM
  • hi,

    with reference to and even gang of four check this categorization of patterns which may prove bit more informative

    Manish Patil Posting is provided AS IS with no warranties, and confers no rights.
    Friday, June 25, 2010 6:47 AM
  • Hallo TicArch,

    GoF categorizes abstract factory as creational, bridge as structural, strategy and visitor as behavioral patterns. They are different from behavior and from usage.

    Abstract Factory : If you have different families of factories, and you must decide at the runtime, which one of them must be initialized, then use abstract factory. Abstract Factory provides an interface for other interfaces which are realized as factories or any other creational classes. Abstract Factories are mostly implemented as singletons.

    Bridge : If you have an interface (or an abstract class) and implementation of it. But you know, not only the implementation but also the interface itself can be changed through configuration or environment settings, then use bridge to vary not only the implementations but also the interfaces. The relationship between the main abstraction and second abstraction is called a bridge.

    Strategy : If you have a common interface but several implementations (different strategies), which you would like change, through for example getters or setters, then use strategy pattern. Strategy makes the implementations interchangeable.

    Visitor : If you like to give a group of classes a new behavior or functionality without changing their implementation, then use visitor. As the name says, a visitor "visits" the related classes, get the needed informations and returns the needed functionality.


    I hope that was helpful. To see examples please visit GoF website, the website of Martin Fowler (for Enterprise Patterns, which are strongly related to the base patterns) or read one of the several design pattern books like "design patterns elements of reusable object-oriented software" or "head first design patterns".


    Friday, June 25, 2010 7:50 AM
  • Please have a look at - This is one of my favorite.
    Monday, June 28, 2010 6:50 AM
  • get an idea about categories of patterns i.e. creational, behavioral and structural Patterns.
    Then you can grasp why/when  ‘factory’ (creational) and why/when ‘strategy’ (behavioral)

    Saturday, July 10, 2010 4:34 PM
  • Greetings,


    One suggestion, if you a software engineering problem , then investigate which design pattern should you use , simply studying the design pattern is a waste of time.


    so think before you act .



    Helping People To Solve Technical Problems
    Sunday, July 11, 2010 2:14 PM