Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction with a few design patterns

Similar presentations


Presentation on theme: "Introduction with a few design patterns"— Presentation transcript:

1 Introduction with a few design patterns
Copyright © 2016 Curt Hill

2 Introduction Design is a hard thing
It takes knowledge, experience and sometimes luck Designing a large program is no different This presentation introduces the notion of design patterns Copyright © 2016 Curt Hill

3 An Example I have a characteristic way to build a fstream file reading loop in C++ initialize file while(true) { read an item if(!thefile) break; Process item read } Copyright © 2016 Curt Hill

4 Commentary This is not the only way to do this but it is an easy pattern to use This a pattern but there are many variations to this pattern This is not a piece of code that I can code even as a template class There are too many possibilities for the thing to be read and the processing to be done Yet the pattern makes is easier and less complex Copyright © 2016 Curt Hill

5 Another Pattern You may have also seen a pattern involving container classes Three classes One encapsulates the object and has its own pointer type One is main class that has all the public methods One is an iterator class that must have two way communication with main class This may be used with lists, trees and other pointer structures of various forms Copyright © 2016 Curt Hill

6 List Example class LinkedNode{ friend class LinkedList;
friend class LinkedIterator; int key; wxString data; LinkedNode * next; }; class LinkedList{ LinkedNode * root; … }; class LinkedIterator{ LinkedNode * current; LinkedList * thelist; void changed(); Copyright © 2016 Curt Hill

7 Similarly In the design process we want to do a similar thing
When we recognize a situation that may suggest a series of classes that have handled similar situations We may not use any code we have used before, but we reuse the roles we have used before We use a pattern that we are confident will work We try to avoid inventing a new way Copyright © 2016 Curt Hill

8 References Design Patterns came into popularity after publication of a book of the same name Authors: Gamma, Helm, Johnson, Vlissides AKA Gang of Four The concepts were used prior to this, but the book applies a name and creates a catalog of patterns Copyright © 2016 Curt Hill

9 Christopher Alexander
An architect who said this: 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. His environment is not ours but the statement still holds Copyright © 2016 Curt Hill

10 More Description A pattern is a stylized description of good design practice, which has been tried and tested in different environments A pattern should have at least four distinct pieces Pattern name Problem it describes Solution it proposes Results and trade-offs of using this Copyright © 2016 Curt Hill

11 Elements Revisited The pattern name is a few words
It allows us to discuss options with others It is possible that a pattern has an alias, but standard naming is helpful The problem gives the context for application of the the pattern Might contain a list of conditions that should be present to apply Copyright © 2016 Curt Hill

12 Elements Revisited The solution gives the pieces that make up the designs An abstract description of what is needed, rather than a specific existing set of classes Usually classes and interfaces Trade-offs Required for evaluating design choices Often are about space and time trade-off, but others are also possible Copyright © 2016 Curt Hill

13 Examples We will next look at a few example patterns
First we will consider the Adapter Next we will consider the MVC, which is also discussed in the Sommerville text Copyright © 2016 Curt Hill

14 Adapter Purpose of the adapter pattern is to convert the interface of one class into that which another class expects This allows two classes that are different to work together Wrapper is an alias of Adapter Copyright © 2016 Curt Hill

15 Adapter Context 1 Suppose we are constructing a drawing editor
We create an abstract base class Shape and derive from it the shapes that we are interested in The base class demands that we have a Draw method so that the descendent classes can draw themselves Most of the descendent classes are normal shapes like Square and Circle which are easy to implement Copyright © 2016 Curt Hill

16 Adapter Context 2 So far so good
Next we want to implement a shape that displays a box of text We already have a TextView object but it does most of what we need but does not conform to the requirements of Shape What we do is create an Adapter class to fit the TextView into the Shape hierarchy Copyright © 2016 Curt Hill

17 TextView Adapter We have two possibilities
A class adapter An object adapter The class adapter uses multiple inheritance from both Shape and TextView to make a TextShape The object adapter makes a derivation of Shape, called TextShape, that includes a TextView object as a property Copyright © 2016 Curt Hill

18 Participant Classes Target – this is the Shape class
Client – the editor that uses Shapes Adaptee – the TextView class Adapter – the TextShape Copyright © 2016 Curt Hill

19 Commentary Regardless of whether this is a class adapter or an object adapter, we implement the required Shape methods by calling TextView methods or combination of methods Next we look at the class diagrams for both the multiple inheritance approach and the composition approach Copyright © 2016 Curt Hill

20 Multiple Inheritance Client Target Adaptee Adaptor
Request() XRequest() Adaptor Adapts Request form to XRequest form Request() Copyright © 2016 Curt Hill

21 Composition Client Target Adaptor Adaptee Adaptee->XRequest
Copyright © 2016 Curt Hill

22 STL Example You should have seen examples of this already
In the Standard Template Library the Stack is an adapter It implements methods Push and Pop (needed by Stack) and converts them into container class methods push_back and pop_back (provided by a vector or list) It also connects empty method with size() == 0 Copyright © 2016 Curt Hill

23 Java Integer Class Java has several wrapper classes
Including Integer and Double These classes take a primitive with no methods and makes it into a class that has the methods that every class should have The Object class, which is the root of the single inheritance tree provides (among others) the following methods: equals toString Copyright © 2016 Curt Hill

24 Java Wrappers Again Most of Java’s container classes, such as lists, trees, vectors, take contained classes of type Object Thus they use polymorphism instead of templates to accomplish what they need Templates did not appear until Java 5 They cannot use primitives, but can use wrappers of primitives Copyright © 2016 Curt Hill

25 MVC Model View Controller
Separates presentation and interaction from the system data Used when there are multiple ways to view and interact with data Often used when the future requirements for interaction and presentation of data are unknown Copyright © 2016 Curt Hill

26 MVC Three components that interact with each other
The Model manages the system data and associated operations on that data The View defines and manages how the data is presented to the user The Controller manages user interaction and passes these interactions to the View and the Model. Copyright © 2016 Curt Hill

27 MVC Picture Controller View Select view
Chooses the view. Sends user changes to model Displays model. Sends user actions to controller Requests updates User action Notify change State change based on user action Request state Model Maintains internal state of the data Notifies view of state changes Copyright © 2016 Curt Hill

28 Pros and Cons Data is independent of its representation
Presentation of the same data in different ways Changes made in one representation shown in all of them If the data and interactions are simple it can make the code more complex Copyright © 2016 Curt Hill

29 Commentary You see the need for this in many applications
Most of the office suite This is a very common form in SmallTalk It is generalized into the Observer design pattern Copyright © 2016 Curt Hill

30 Observer Define a one-to-many relationship between objects
One subject with many observers When the one object changes state all of the others are notified This is also known as Publish-Subscribe or Dependents As we saw in MVC, very common in the display and editing of data Copyright © 2016 Curt Hill

31 The Subject Subject – only one Observer – may be many
Maintains the central data structure Responds to requests to change Maintains a list of objects that need to be notified when something changes Observer – may be many Requests notification of state changes May also request that a change be made Copyright © 2016 Curt Hill

32 When to use Data has two or more aspects, where all are dependent on one Changing one forces a change in another Notifications of change need to be made, but there is no assumption on the behavior of the object to be notified Loose coupling of the objects Copyright © 2016 Curt Hill

33 Consider a word processor
The subject keeps the internal form in which the text resides Observers would include All possible views of this data An Undo/Redo processor Backup processor Print processor Copyright © 2016 Curt Hill

34 Pros and Cons Notification may be easily broadcast to many observers
All of the observers are ignorant of how many other observers there may be Thus an update may cascade into lots of work The observers all comply with a simple interface They may be widely varying in function and the observer does not know or care Copyright © 2016 Curt Hill

35 Next Next is a class diagram It has an abstract Subject and Observer
Derived or concrete versions of both Copyright © 2016 Curt Hill

36 Observer Structure Subject Observer
Attach(Observer) Detach(Observer) Notify() Update() for all o in observers o->Update() ConcreteObserver ConcreteObserver ConcreteSubject observerState observerState subjectState Update () Update () GetState() SetState() Copyright © 2016 Curt Hill

37 Design Patterns Book lists 23 patterns of three types Creational
Abstract factory, Builder, Factory method, Prototype, Singleton Structural Adaptor, Bridge, Composite, Decorator, Façade, Flyweight, Proxy Behavioral Chain of responsibility, Command, Interpreter Iterator, Mediator, Mementor, Observer, State, Strategy, Template Method, Visitor Copyright © 2016 Curt Hill

38 Other Books Sommerville includes design patterns from differing levels of abstraction Layered, Client Server, Pipe and filter McConnell’s Code Complete consider some of the same patterns as Design Patterns and mention others as well Copyright © 2016 Curt Hill

39 Finally The dragon that needs slaying is complexity
It exists at design and coding times Design patterns give us tried and true solutions to those problems that commonly come up This makes for simpler solutions More likely to work as well We may have to deal with these further Copyright © 2016 Curt Hill


Download ppt "Introduction with a few design patterns"

Similar presentations


Ads by Google