Tuesday, May 15, 2007

Inversion of Control (IoC) / Dependency Injection

from: http://martinfowler.com/articles/injection.html

When I first ran into inversion of control, it was in the main control of a user interface. Early user interfaces were controlled by the application program. You would have a sequence of commands like "Enter name", "enter address"; your program would drive the prompts and pick up a response to each one. With graphical (or even screen based) UIs the UI framework would contain this main loop and your program instead provided event handlers for the various fields on the screen. The main control of the program was inverted, moved away from you to the framework.

Sunday, May 13, 2007

Singleton

ensure a class only has one instance and provide a global access point

in java: private constructor, static method with static variable

eg. choco-o-holic

real-world:
- VB form instance

Factory Method and Abstract Factory

eg
pizzastore
pizza
pizzaingredient

realworld
- chartfactory
- chart

Decorator

Attaches additional responsibilities to an object dynamically. Provides a flexible alternatives to subclassing for extending functionality.

eg (Star-Buzz Coffee)

real-world

- java.io (FileInputStream -component (Buffered Input Stream -decorator)

Saturday, May 12, 2007

Observer Pattern

defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

eg: weather-o-rama
real-world:
-publisher/subscribers - newspaper
-msmq message notification

Strategy Pattern

Defines a family of algo, encapsulates each one, and makes them interchangeable.

program to interface not to implementation.

eg: Duck
Duck -> FlyBehavior & QuackBehavior
+ fly
+ quack
-setBeh()
-setQua()

real world:
Parser -> Lex

Friday, May 11, 2007

Mediator

The mediator pattern promotes looser coupling between the classes by being the only class that has detailed knowledge of the methods of other classes. Classes send messages to the mediator when needed and the mediator passes them on to any other classes that need to be informed.

Real-world example
1) In Complex GUI where you enable/disable Controls based on user action.