2009-10-24

OO Wisdom

OO Basics
Abstraction
Encapsulation
Polymorphism
Inheritance

OO Principles
Encapsulate what varies
Favor composition over inheritance
Program to interfaces, not to implementations
Strive for loosely coupled designs between objects that interact
Classes should be open for extension but closed for modification
Depend on abstractions. Do not depend on concrete classes.
Only talk to your friends.
Don't call us, we call you.
A class should have only one reason to change.

Patterns' description
Abstract Factory - Allows a client to create families of objects without specifying their concrete classes
Adapter - Wraps an object and provides a different interface to it
Command - Encapsulates a request as an object
Composite - Clients treat collections of objects and individual objects uniformly
Decorator - Wraps an object to provide new behavior
Facade - Simplifies the interface of a set of classes
Factory Method - Subclasses decide which concrete classes to create
Iterator - Provides a way to traverse a collection of objects without exposing its implementation
Observer - Allows objects to be notified when state changes
Proxy - Wraps an object to control access to it
Singleton - Ensures one and only object is created
State - Encapsulates state-based behaviors and uses delegation to switch between behaviors
Strategy - Encapsulates interchanging behaviors and uses delegation to decide which one to use
Template method - Subclasses decide how to implement steps in an algorithm

Other patterns
Bridge - use to vary not only your implementations, but also your abstractions
Builder - use to encapsulate the construction of a product and allow it to be constructed in steps
Chain of Responsibility - use when you want to give more than one object a chance to handle a request
Flyweight - use when one instance of a class can be used to provide many "virtual instances"
Interpreter - use to build an interpreter for a language
Mediator - use to centralize complex communication and control between related objects
Memento - use when you need to be able to return an object to one of its previous states ("undo")
Prototype - use when creating an instance of a given class is either expensive or complicated
Visitor - use when you want to add capabilities to a composite of objects and encapsulation is not important

--

Read more about Design Patterns in the great book: Head First Design Patterns. I highly recommended it for all programmers with more than 2 years of experience.

No comments: