Design Patterns

If you have THIS situation, then THIS programming technique might help.

System Modelling

Better to design first, then implement

Formal technique: UML(Unified Modelling Language)

A UML class is a box

name of class Vec
|

Coupling and Cohesion

Coupling: Degree to which modules depend on each other

Low Coupling: desirable

High Coupling: depend on low level implementation

Cohesion: how related are components within a module

Singleton Pattern

We have a class C and we want only one instance (object) to be created (throughout life of the program) Example: database connection, error log

See provided example code

Observer Pattern

Publish-Subscribe model

Some entities which publishes/generates data: The Subject

Other entities which subscribe to data to react/consume it: The Observer

See provided example code

Decorator Pattern

See provided example code

Factory Method Pattern

Enemies: Turtle, Bullet, Boss (singleton) Level: Normal, Castle

while(Game not end) Enemy *e = l->createEnemy();

Singleton and factory method pattern often work together. Imagine a Boss enemy, there is only one Boss.

while(Game not end) Enemy *e = e->createEnemy();

See wiki

Template Pattern

Put same code in a superclass. No subclass can override non-virtual private methods.

See provided example code

Visitor Pattern

Double Dispatch: make a decision on which method to call based on the type of tweet objects.

See provided sample code