b16 design patterns
play

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.


  1. B16 Design Patterns Lecture 1 Victor Adrian Prisacariu http://www.robots.ox.ac.uk/~victor

  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

  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

  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

  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…

  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

  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.

  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.

  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).

  10. UML Example Class Name Attributes Operations private: - public: + Italic denotes abstract

  11. Associations Associations capture relationships between objects: • Structural: An Anim imal has a Tail il • Interactions: An Anim imal plays with a Toy

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

  13. Associations An Animal (abstract class/interface here) - must eat Foo ood. - may dress with Clot Clothes. - must own a Tail il. - can own a Toy. A Ca Cat is a type of Anim imal.

  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

  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

  16. Example

  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 class Animal { { public string name; private string name; public void MakeSound() { } public void MakeSound() { } } } public class Animal public interface Animal { { private string name; void MakeSound(); public abstract void MakeSound(); } }

  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(); }

  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.

  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.

  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.

  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; } }

  23. Creational Patterns: Prototype • You want to create new objects quickly from previously created objects. • Example: – Same as before, but now you are allowed to have multiple instances of Co Confi figuratio ion class.

  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(); } }

  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.

  26. Creational Patterns: Factory Method

  27. Creational Patterns: Factory Method public interface SortingAlgorithm { public class BubbleSort: SortingAlgorithm { void Sort(); public void Sort() { /* bubble sort method */ } } } public class MergeSort : SortingAlgorithm { public void Sort() { /* merge sort 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"); } }

  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.

  29. Creational Patterns: Builder

  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); } }

Recommend


More recommend