B16 Design Patterns Lecture 1 Victor Adrian Prisacariu - - PowerPoint PPT Presentation

b16 design patterns
SMART_READER_LITE
LIVE PREVIEW

B16 Design Patterns Lecture 1 Victor Adrian Prisacariu - - PowerPoint PPT Presentation

B16 Design Patterns Lecture 1 Victor Adrian Prisacariu http://www.robots.ox.ac.uk/~victor Course Content I. Code Design Patterns 1. Motivation, Classification, UML 2. Creational Patterns 3. Structural Patterns 4. Behavioral Patterns II.


slide-1
SLIDE 1

B16 Design Patterns

Victor Adrian Prisacariu

http://www.robots.ox.ac.uk/~victor

Lecture 1

slide-2
SLIDE 2

Course Content

I. Code Design Patterns

  • 1. Motivation, Classification, UML
  • 2. Creational Patterns
  • 3. Structural Patterns
  • 4. Behavioral Patterns
  • II. Algorithm Design Patterns

Slides on Weblearn

slide-3
SLIDE 3

Course Content

I. Code Design Patterns

  • 1. Motivation, Classification, UML
  • 2. Creational Patterns
  • 3. Structural Patterns
  • 4. Behavioral Patterns
  • II. Algorithm Design Patterns

Slides on Weblearn

slide-4
SLIDE 4

Patterns

Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. Timeless Way of Building, Christopher Alexander, 1979

slide-5
SLIDE 5

Illustration [Shalloway and Trott, Aldrich]

  • Carpenter 1: How do you think we should build these drawers?
  • Carpenter 2: Well, I think we should make the joint by cutting

straight down into the wood, and then cut back up 45 degrees, and then going straight back down, and then back up the other way 45 degrees, and then going straight down, and repeating…

slide-6
SLIDE 6

Illustration [Shalloway and Trott, Aldrich]

  • Carpenter 1: Should we use a dovetail joint or a miter joint?
  • Subtext:

– mitre joint: cheap, invisible, breaks easily; – dovetail joint: expensive, beautiful, durable.

mitre dovetail

slide-7
SLIDE 7

(Software) Design Patterns

A (software) design pattern is a time-tested solution to a common problem.

  • captures design experience;
  • allow that expertise to be transferred;
  • enable a shared design language, improving communication,

easing documentation.

slide-8
SLIDE 8

Code Design Pattern Books

Ori riginal l Gang of f Four Book

  • Design Patterns: Elements of Reusable

Object-Oriented Software, Erich Gamma, John Vlissides, Richard Helm, Ralph Johnson

  • Uses C++ (and Smalltalk)

Many, many other online res esources, , e. e.g. . C# 3.0 Design Patterns, Judith Bishop; lots of Java books, etc.

slide-9
SLIDE 9

Before we can start describing patterns … UML

  • The Unified Modeling Language (UML) is a standard modeling

language (language for drawing diagrams) that allows developers to express software designs using a visual notation.

  • UML is (somewhat) universally disliked.
  • We don’t care about all of UML – just UML class diagrams.

– Allow us to visualise each design pattern at a high level. – Inform us about the cla class str tructure and the rela lationships betw tween cla classes (inheritance and containment).

slide-10
SLIDE 10

UML Example

Class Name Attributes Operations private: - public: + Italic denotes abstract

slide-11
SLIDE 11

Associations

Associations capture relationships between objects:

  • Structural: An Anim

imal has a Tail il

  • Interactions: An Anim

imal plays with a Toy

slide-12
SLIDE 12

Associations

Ge Generic asso associa iation A and B call and access each other’s elements On One way ay asso association A can access B’s elements, but B cannot access A Aggr ggregation A has B (B is “part of” A) B does not depends on A Com Composition A has a B (B is “part of” A) B depends on A Inh Inheritance B inherits from A Realiz ization B implements A A B A B A B A B A B A B A B

slide-13
SLIDE 13

Associations

An Animal (abstract class/interface here)

  • must eat Foo
  • od.
  • may dress with Clot

Clothes.

  • must own a Tail

il.

  • can own a Toy.

A Ca Cat is a type of Anim imal.

slide-14
SLIDE 14

Multiplicity

  • We can represent various multiplicities, eg. 0..*, 1 to 1, …
  • Example:

– A Human can have an arbitrary number of Anim imals ls – An Anim imal has 0 or 1 Humans

slide-15
SLIDE 15

Multiplicity

  • We can represent various multiplicities, eg. 0..*, 1 to 1, …
  • Example:

– An Anim imal l can have 0 or 1 Tail ils – A Tail il belongs to exactly one Anim imal and cannot exist without the Anim imal

slide-16
SLIDE 16

Example

slide-17
SLIDE 17

My pseudocode

  • I will show lots of simple code examples.
  • For that I will use a simplified version of C++ (no pointers ☺).

public class Animal { public string name; public void MakeSound() { } } public class Animal { private string name; public void MakeSound() { } } public class Animal { private string name; public abstract void MakeSound(); } public interface Animal { void MakeSound(); }

slide-18
SLIDE 18

My pseudocode

  • I will show lots of simple code examples.
  • For that I will use a simplified version of C++ (no pointers ☺).

public interface Animal { void MakeSound(); } public class Cat : Animal { public string name; public void MakeSound(); }

slide-19
SLIDE 19

Code Design Pattern Categories

The GoF books defines 23 patterns, split into three fundamental groups:

  • Creational

– They abstract the instantiation process. – Make systems independent on how objects are compared, created and represented.

  • Str

truct ctural

– Focus on how classes and objects are composed to form (relatively) large structures. – Generally use inheritance.

  • Beh

ehavioral

– Describe how different objects work together. – Focus on

  • The algorithms and assignment of responsibilities among objects.
  • The communication and interconnection between objects.
slide-20
SLIDE 20

Elements of a Pattern

  • Name: Important as a part of the design vocabulary.
  • Problem: When the apply the pattern.
  • So

Solu lution: Design elements, along with their relationships, responsibilities and collaborations.

slide-21
SLIDE 21

Creational Patterns: Singleton

  • We can only have a single instance of one class.
  • Example:

– We have global data and operations:

  • configuration files: you may only have one file on disk containing app settings.

– We can only have a single instance of a resource

  • a library to access specialized neural network hardware.
slide-22
SLIDE 22

Creational Patterns: Singleton

public class Configuration { // Internal singleton variable private static Configuration config = null; // Private constructor private Configuration() { } // Public access method public static Configuration Instance() { if (config == null) config = new Configuration(); return config; } }

slide-23
SLIDE 23

Creational Patterns: Prototype

  • You want to create new objects quickly from previously created
  • bjects.
  • Example:

– Same as before, but now you are allowed to have multiple instances

  • f Co

Confi figuratio ion class.

slide-24
SLIDE 24

Creational Patterns: Prototype

public interface Prototype { Prototype Clone(); } public class Configuration : Prototype { public string setting; public Prototype Clone() { Configuration newConfig = new Configuration(); newConfig.setting = this.setting; return newConfig; } } class Example { static void Main(string[] args) { Configuration conf1 = new Configuration("setting"); Configuration conf2 = (Configuration)conf1.Clone(); } }

slide-25
SLIDE 25

Creational Patterns: Factory Method

  • You need to create objects of a certain type.
  • Each subclass must be able to define its own domain.
  • Example:

– We have an abstract So Sort rtin ingAlg lgorit ithm class, and we want to deal with the algorithm in a way that is agnostic of the specific algorithm. – We need to create various types of So Sort rtin ingAlg lgorit ithms including, e.g. Bu Bubble leSort and MergeSo Sort.

slide-26
SLIDE 26

Creational Patterns: Factory Method

slide-27
SLIDE 27

Creational Patterns: Factory Method

public class AlgorithmCreator { public static SortingAlgorithm FactoryMethod(string type) { if (type == "bubbleSort") return new BubbleSort(); return new MergeSort(); } } class AppThatNeedsSorting { static void Main(string[] args) { SortingAlgorithm algorithm = AlgorithmCreator.FactoryMethod("bubbleSort"); } } public class BubbleSort: SortingAlgorithm { public void Sort() { /* bubble sort method */ } } public class MergeSort : SortingAlgorithm { public void Sort() { /* merge sort method */ } } public interface SortingAlgorithm { void Sort(); }

slide-28
SLIDE 28

Creational Patterns: Builder

  • You need to create objects of a certain type.
  • You want to decouple the creation of the object from their specific

parameters.

  • Example:

– We have an abstract SortingAlgorithm class, and we want to deal with the algorithm in a way that is agnostic of the specific algor – We need to create various types of SortingAlgorithms including, e.g. BubbleSort and MergeSort. – You want to specify the parameters for the creator as a class, e.g. SortParameters.

slide-29
SLIDE 29

Creational Patterns: Builder

slide-30
SLIDE 30

Creational Patterns: Builder

public interface SortingAlgorithm { void Sort(); } public class BubbleSort : SortingAlgorithm { public void Sort() { /* bubble sort method */ } } public class MergeSort : SortingAlgorithm { public void Sort() { /* merge sort method */ } } public class SortParameters { public string type; public SortParameters(string type) { this.type = type; } } public class AlgorithmCreator { public static SortingAlgorithm FactoryMethod(SortParameters parameters) { if (parameters.type == "bubbleSort") return new BubbleSort(); return new MergeSort(); } } class AppThatNeedsSorting { static void Main(string[] args) { SortParameters sortParams = new SortParameters("bubbleSort"); SortingAlgorithm algorithm = AlgorithmCreator.FactoryMethod(sortParams); } }

slide-31
SLIDE 31

Creational Patterns: Abstract Factory

  • We now need to create similar classes, as before.
  • We need separate, similar but different, object creators.
  • Example:

– As before, an abstract SortingAlgorithm class, but now we also have a SearchingAlgorithm class, both (potentially) inheriting on the Alg lgorithm class. – We need to create various types of SortingAlgorithms including, e.g. BubbleSort and MergeSort. – We need to create various types of SearchingAlgorithms including, e.g. DumbSearch and BinarySearch.

slide-32
SLIDE 32

Creational Patterns: Abstract Factory

slide-33
SLIDE 33

Creational Patterns: Abstract Factory

public interface Algorithm { } public interface SortingAlgorithm : Algorithm { void Sort(); } public class BubbleSort : SortingAlgorithm { public void Sort() { /*bubble*/ } } public class MergeSort : SortingAlgorithm { public void Sort() { /*merge*/ } } public interface SearchingAlgorithm : Algorithm { void Search(); } public class DumbSearch : SearchingAlgorithm { public void Search() { /*dumb*/ } } public class BinarySearch : SearchingAlgorithm { public void Search() { /*binary*/ } }

slide-34
SLIDE 34

Creational Patterns: Abstract Factory

public interface AlgorithmCreator { Algorithm FactoryMethod(string type); } public class SortingCreator:AlgorithmCreator { public Algorithm FactoryMethod(string type) { if (type == "bubbleSort") return new BubbleSort(); return new MergeSort(); } } public class SearchingCreator : AlgorithmCreator { public Algorithm FactoryMethod(string type) { if (type == "dumbSearch") return new DumbSearch(); return new BinarySearch(); } } class AppThatNeedsSorting { static void Main(string[] args) { AlgorithmCreator sortingCreator = new SortingCreator(); Algorithm algorithm = sortingCreator.FactoryMethod("bubbleSort"); } }

slide-35
SLIDE 35

Creational Patterns – Summary

  • Sin

ingle leton: limits object creation to only one instance.

  • Prototype: creates objects by cloning existing objects.
  • Factory

ry method: method that creates related objects.

  • Build

ilder: separates construction and representation.

  • Abstract factory: groups object factories that have a common

theme.

https://en.wikipedia.org/wiki/Design_Patterns

slide-36
SLIDE 36

Summary of Lecture 1

  • Generic motivation of using design patterns.
  • Overview of UML diagrams.
  • Classification the various types of design patterns.
  • Creational design patterns.