design-pattern-java

Design Pattern w/ Java

Placing distinction on writing high quality code. Not good enough to just produce the correct output, but code that is easy to maintain, properly cohesive and loosely coupled. Instructor: Jason Fedin

Structure

Overview

History

Advantages

Types of Design Patterns

There 23 Patterns differ at levels of granularity, abstraction and how they relate to one another

Grouped into 3 main types. Organized by their purpose (Gang of four)

  1. Structural: deal with the composition of classes or objects
  2. Creational: concern the process of object creation
  3. Behavioral: the way objects/classes interact, relate with one other, and distribute responsibility. [Flow-tip]: structure class, create object, control its behavior

Organization by Scope (whether classes or objects)

  1. Class Patterns: applies to relationship between classes and their subs. These relationships are established through inheritance.
  2. Object Patterns: applies to relationship between objects. Can be changed at runtime making them more dynamic. Describes how objects can be composed into larger structures through object composition and polymorphism.

Creational Patterns

Structural Patterns

Behavioural Patterns

Selecting and Using Design Patterns

Principles and Strategies of Design

Overview

Design patterns use these principles. Helps understand design patterns

Design Smells

They are structures in the design that violate the fundamental design principles which negatively affect design quality. Need to be aware of design smells while following design principles. Common characteristics of design smells includes;

Focus: to achieve highly cohesive (responsibility) and loosely coupled (dependency) design, code and solution.

DESIGN PATTERS ARE AWESOME!…since they solve the above problems and ensure high quality design in application

Programming to an Interface

Simply means programming to a super type.

Polymorphism

Recall: Abstract class Animal having two concrete implementations, Cat and Dog
Program to an implementation;

Dog doga = new Dog();
doga.bark();

Forces to code to a concrete implementation. Doesn’t hide concrete implementation.

Program to an interface/supertype

Animal animal = new Dog();
animal.makeSound();

better, rather than hardcoding the instantiation of the subtype(new Dog() on the left side). calls method in abstract class. animal reference used polymorphically. assigns the concrete implementation of the object at runtime.

improved;

animal = getAnimal();
animal.makeSound()

actual animal subtype(Dog) is encapsulated, hidden, unknown

Abstract Classes Vs Interfaces

Composition over Inheritance

Another design principle which deals with using ‘has-a’ relationship over ‘is-a’. ‘is-a’ => tightly coupled ‘has-a’ => loosely couples

Delegation

One class ‘delegating’ its behavior to anther class

an example of object composition

makes you think of which message to forward/delegate

primary advantage is run-time flexibility

examples:

design patterns that uses delegation principle

Single Responsibility

This states that every class should have only one job.

closely related to the concepts of coupling and cohesion

aims: limiting the impact of change by designing low/loosely coupled classes that are highly cohesive

eg of responsibilities

Open Close Principle (OCP)

Classes and methods should be Open for extension(new funtionality) and Close for modification

Liskov Substitution Principle (LSP)

States that object of a superclass can be replaceable with objects of its subclasses without breaking the application

How to fix?: through inheritance

Interface Segregation Principle

Client should not be forced to implement an interface if there exist a method they do not use

How to fix?: split the interface into multiple based on one functionality

Dependency Inversion Principle

Higher level classes must not depend directly on lower level classes

“Inversion” in the name => inverts the way we think about OO design * ie, from top-to-bottom level, with bottom-to-top with abstraction

Similar to “Program to an Interface, not an actual implementation” * IDP places stronger emphasis on abstraction

It’s the central underlying principle in design pattern

This principle helps achieve the Open-Close Principle

Dependency Injection Principle

A class, A has a dependency on another class, B, if class A uses an instance of class B.

Dependency injection => technique whereby one object supplies the dependencies of another

guide 1 - 3 => achieves Dependency Inversion plus 4 => achieves Dependency Injection

UML

Documentation that describes the design pattern

NB: Cover the implementation before the theory

Design Patterns

======= Creational ========

1. Factory Method

2. Abstract Factory Method

3. Singleton Method

4. Builder Pattern

5. Prototype Pattern



====== Structural ========

Describes how classes and objects can be combined to form larger one.
* these patterns utilizes inheritance to interfaces and impl
* describes how to assemble objects * building a hierarchy made up of different kinds of objects * describes how relationships b/n classes and objects are defined * examples; bridge, decorator, composite, facade, proxy, and flyweight pattern * 7 structural design patterns

1. Adaptor Pattern

2. Bridge Pattern

3. Composite Pattern

4. Decorator Pattern

what?

Example:

How?

Why?

Con

5. Facade Pattern

6. Flyweight Pattern

This pattern uses sharing technique to support a large # of fined-grained objects efficiently

Intrinsic and Extrinsic state - need to be identified

When used?

Advantages and Drawbacks

7. Proxy Pattern

This pattern provides a rep for another object in order to control the client’s access to it

Adapter vs Bridge

8. Chain of Responsibility

how

TODO: challenge implementation

9. Command Pattern

No overview and impl

10. Interpreter Pattern

No overview and impl
used in implementation of simple language, language translator, regular expression, parsing

11. Iterator Pattern

No overview and impl

12. Mediator Pattern

Participants:

  1. Mediator: defines an interface for communicating with Colleague objects
    • Concrete mediator: impl cooperative behaviour by coordinating Colleague objects to route request btn appropriate colleagues, knows and maintains its colleague
  2. Colleagues:
    • each colleague has its mediator
    • each communicates with its mediator by sending and receiving request from the mediator
    • each comm with its mediator when an event of interest occurs
      • one way is to implement Mediator as an Observer
      • sends notification to mediator whenever a change occurs
      • mediator responds by propagating the effects of the change to other colleagues

Advantages:

When to use:

12. Momento Pattern

13. Observer Pattern

Observer design pattern defines a one-to-many relationship b/n objects so that when one object changes state, all of its dependents/listeners are notified and updated automatically * many objects need to be notified whenever an event occurs * notify-ers and notify-ees are decoupled * more flex with requirement change * mostly used pattern * if no subs exist, don’t publish even when event happens

How?

Examples:

When To Use:

Lang Specific:

14. State Design Pattern

Allows object to alter its behavior when its internal state changes

Example:

When?

why?

14. Strategy Design Pattern

Strategy defines a family of algorithms that are interchangeable

Object are created representing the various strategies + context object whose behaviour varies as per its strategy object

When?

Why?

vs State Pattern

14. Template Design Pattern

Template defines the skeleton of the step by step method calls that are need (algo) * helps us generalized a common process at abstraction level * template === just method calls in abstract class method * software reuse if the fundamental goal of this method

15. Visitor Design Pattern

drawback