Sunday, May 17, 2009

23 Design Patterns

Reference book: Head First Design Patterns.
Reference Wiki article: Design Pattern (Computer Science)

Abstraction:

In an object-oriented software system,objects are entities used to represent or model a particular piece of the system.

A class is a template or recipe for the creation of a particular type of object. That is, one can use a class to create ("instantiate") objects of the type described by the class.

new is a keyword in Java. It is an example of what is called a class operator. It operates on a class and creates an instance (also called object) of the given class.


People have identified and separated the variant and invariant pieces of a systems and defined abstract representations whenever needed, for example

  1. Abstract Structure -- abstract classes, interfaces
  2. Abstract Behavior -- abstract methods, strategies, visitors.
  3. Abstract Construction -- factories
  4. Abstract Environments -- anonymous inner classes, closures

Inheritance and Polymorphism:


"Is-a" or "inheritance" (sometimes also called "generalization") relationships capture a hierarchal relationship between classes of objects.

We can never truly know what an object truly is, only how it acts. Java has a the keyword implements which is used to show generalization of a pure behavioral abstraction called an interface. An interface has a similar syntax to a class, but only specifies behaviors in terms of the "signatures" (the input and output types) of the methods.


Inheritance and polymorphism are really just two ways of looking at the same class relationship.

Inheritance is looking at the class hierarchy from the bottom up. A subclass inherits behaviors and attributes from its superclass. A subclass automatically possesses certain behaviors and/or attributes simply because it is classified as being a subclass of an entity that possesses those behaviors and/or attributes.


Polymorphism, on the other hand, is looking at the class hierarchy from the top down. Any subclass can be used anywhere the superclass is needed because the subclasses are all abstractly equivalent to the superclass. Different behaviors may arise because the subclasses may all have different implementations of the abstract behaviors defined in the superclass.


Inheritance and Composition:

An OO system has two distinct mechanisms to express relationship notions between objects: "is-a" which is technically referred to as "inheritance" and "has-a" which is technically referred to as "composition".

"Has-a" or "composition" (sometimes referred to as an "associative") relationships capture the notion that one object has a distinct and persistant communication relationship with another object. for instance, we can say a car "has-a" motor.


23 Design Patterns

In the book Design Patterns: Elements of Reusable Object-Oriented Software published in 1994 (Gamma et al.), there are 23 design patterns.

Creational patterns:

1 Abstract factory

2 Factory method

3 Builder

4 Prototype

5 Singleton


Structural patterns:

6 Adapter

7 Bridge

8 Composite

9 Decorator

10 Facade

11 Flyweight

12 Proxy


Behavioral Patterns:

13 Chain of responsibility

14 Command

15 Interpreter

16 Iterator

17 Mediator

18 Memento

19 Observer

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

Example

20 State

21 Strategy

Defines a family of algorithms, encapsulates each one, and make them interchangable. Strategy lets the algorithm change independently from clients that use it.

Example

22 Template method

23 Visitor

No comments:

Post a Comment

meta.ai impression

Meta.ai is released by meta yesterday, it is super fast you can generate image while typing! You can ask meta.ai to draw a cat with curvy fu...