Posts tagged: Observer 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.

Observer Pattern

By , September 14, 2009
Good object-oriented design emphasizes both encapsulation and loose coupling. In other words, classes should keep internal details private and also minimize their strict dependencies. In most applications, classes don’t work in isolation; they interact with many other classes. A common scenario of class interaction occurs when one class (the Observer) needs to be notified when something changes in another (the Subject). For example, several Windows® Forms controls might need to update their display after a button is clicked. A simple solution would be to have the Subject call a specific method of the Observer whenever a change in state occurs. This introduces a host of problems, however. Now, since the Subject needs to know which method to call, it is tightly coupled to that specific Observer. Furthermore, if you need to add more than one Observer, you have to continue to add code for each method call to the Subject. If the number of Observers changes dynamically, this gets even more complex. You’ll quickly end up with a brittle mess that’s difficult to maintain. 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 »