Design Patterns Introduction

7 downloads 21810 Views 444KB Size Report
Design Patterns: Elements of Reusable. Object-Oriented Software, Addison– Wesley, 1994, ISBN. 78-0201633610. ‣ Elisabeth Freeman et al. Head First Design ...
Design Patterns Introduction Oliver Haase ‘An instrument, a tool, an utensil, whatsoever it be, if it be fit for the purpose it was made for, it is as it should be though he perchance that made and fitted it, be out of sight and gone.’ — Marc Aurel, Roman Emperor, 121 – 180 ad

1

Bibliography

The bible for design patterns. The authors are widely known as the Gang of Four (GoF), their patterns as GoF patterns.

‣ Erich Gamma et al. Design Patterns: Elements of Reusable

Object-Oriented Software, Addison–Wesley, 1994, ISBN 78-0201633610. ‣ Elisabeth Freeman et al. Head First Design Patterns, O'Reilly, 2004, ISBN 978-0596007126. ‣ Joshua Bloch. Effective Java, Second Edition, Addison Wesley, 2008, ISBN 978-0-321-35668-0.

2

Not that fast, buddy! Before we start, let’s do some ground work...

3

Relationships between Classes (and Interfaces) ‣ Specialization and generalization ‣ Association ‣ Aggregation ‣ Composition ‣ Creation

4

Association Weakest form of relationship: A is related to B

‣ Undirected Association: no information about navigability A

B

‣ Unidirectional Association: navigation from instances of A to instances of B

A

B

‣ Bidirectional Association: navigation both ways A

B

‣ in Java: A has reference to (collection of) B, or vice versa 5

Aggregation ‣ Somewhat stronger than an association ‣ Models part-of relationship, e.g. B is part of A: A

B

Course

Student

‣ Example:

‣ in Java: A has reference to (collection of) B 6

Composition ‣ Stronger than aggregation ‣ Part-of relationship, with ownership, i.e. lifespan of B depends on A

A

B

Building

Room

‣ Example:

‣ in Java: A creates (collection of) B, has reference to it/them. 7

Don’t you never forget this again, or... 8

Design Patterns - Definition Design Pattern: “Each [design] pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem" - Christopher Alexander, 1977.

Christopher Alexander: born 1936 in Vienna, studied mathematics and architecture (Cambridge), received PhD in architecture (Harvard), professor for architecture (UoC, Berkeley), known as the founder of design patterns

9

Design Patterns - Goals ‣ fast to develop, clean solution through solution reuse ‣ simplified maintenance through clean structures and déjà vu effects ‣ improved extensibility through anticipatory design ‣ (partly) opportunity for code generation or language support (e.g. singleton pattern) ‣ (partly) opportunity for class libraries (e.g. observer pattern) “Any fool can write code that a computer can understand. Good programmers write code that humans can understand." - Martin Fowler. 10

Principles of Reusable Design According to GoF, there are two fundamental principles of reusable software design (that also underly most if not all patterns): 1st Principle of Reusable Design: Program against an interface, not an implementation. 2nd Principle of Reusable Design: Prefer composition over inheritance.

11

st Excursion: 1

Principle in Java

How it’s usually done: public interface HelloSayer { String getGreeting(String name); } public class NiceHelloSayer implements HelloSayer { @Override public String getGreeting(String name) { return “hello “ + name + “, how are you?”; } }

Usage: HelloSayer helloSayer = new NiceHelloSayer(); helloSayer.getGreeting(“John Doe”);

...Or: NiceHelloSayer niceHelloSayer = new NiceHelloSayer(); niceHelloSayer.getGreeting(“John Doe”);

12

st Excursion: 1

Principle in Java

Implementation with nested class (J. Bloch, Effective Java): public final class NiceHelloSayer { private static final class Implementation implements HelloSayer { @Override public String getGreeting(String name) { return "hello " + name + ", how are you?"; } } public static Implementation getInstance() { return new Implementation(); } private NiceHelloSayer() { throw new AssertionError(); } }

Usage: HelloSayer helloSayer = NiceHelloSayer.getInstance(); String greeting = helloSayer.getGreeting(“John Doe”); 13

nd Excursion: 2

Principle

Inheritance breaks encapsulation, leads to strong coupling between super and subclass. Fragile Base Class Problem: Seemingly correct subclass can break when superclass is (correctly) changed.

14

nd Excursion: 2

Principle

Assume, you want to implement an InstrumentedHashSet that keeps book of the number of attempted add operations: @ThreadSafe public class InstrumentedHashSet extends HashSet { private int addCount = 0;





public InstrumentedHashSet() {} public InstrumentedHashSet(int initCap, float loadFactor) { super(initCap, loadFactor); } @Override public boolean add(E e) { addCount++; return super.add(e); } @Override public boolean addAll(Collection