Dept. of Computer Engineering, Amirkabir University of Tech. 1 Design Patterns Dr. Noorhosseini Introduction
Dept. of Computer Engineering, Amirkabir University of Tech. 2 Introduction Designing OO software is not easy. Designing Reusable OO software is really hard. Why? Reusable OO software is not generated in the first attempt. Experienced designers however make good design. It takes a long time for a novice designer to learn what a good OO design is. So there is something that an experienced designer must know that a novice one doesn’t. What’s that?
Dept. of Computer Engineering, Amirkabir University of Tech. 3 Introduction (cont) Experienced designers know NOT to solve every problem from scratch. Rather they use (reuse) solutions that have worked for them in the past. ( e.g. setting up a socket connection using c lib) In other words they have developed a pattern in their mind. A good designer will also polish these patterns on every use and even a better designer documents these patterns for future use by himself or others.
Dept. of Computer Engineering, Amirkabir University of Tech. 4 Introduction (cont..) This is the subject of this course, patterns that get used repeatedly in many software projects. We want to prevent re-invention of the wheels A designer who is familiar with these patterns can apply them immediately to design problems without having to re-discover them.
Dept. of Computer Engineering, Amirkabir University of Tech. 5 The benefits of knowing these patterns More elegant design More readable design. Easier communication with co-designer (better understanding among team members) Easier understanding of existing code (over %80 of an engineer’s job) Faster and more reliable design etc..
Dept. of Computer Engineering, Amirkabir University of Tech. 6 What is a design pattern? Design patterns are not about designs such as linked lists and hash tables that can be coded in classes and reused as is. Nor are they complex, domain-specific designs for an entire application Design patterns are description of communicating objects and classes that are customized to solve a general design problem in a particular context. They are flexible, elegant, proven and reusable solutions to design problems.
Dept. of Computer Engineering, Amirkabir University of Tech. 7 MVC (quick review) MVC Model: application object View: screen presentation Controller: defines the way the user interface reacts to user input Diagram on board
Dept. of Computer Engineering, Amirkabir University of Tech. 8 An example: MVC model Model/View/Controller (MVC) triad of classes is an example of a structural pattern used to build user interfaces Three kind of classes: The model (application objects), the view (its screen presentation), the controller (how view reacts to user input) Before, UI design tend to lump these together Decoupling of MVC, many advantages, e.g. one model and several views, adding new views without changing model, changing model without affecting views, …
Dept. of Computer Engineering, Amirkabir University of Tech. 9
10 MVC (cont..) The ideas behind MVC: - decoupling objects so that changes to one can affect many others without the need for changed object to know details of the others. The more general design is described by Observer pattern. - MVC let you change responses to input without affecting the views. Depending on the context we can have different reaction to the same input. Having a controller object let us encapsulate the response. The view-controller relationship is an example of the Strategy design pattern.
Dept. of Computer Engineering, Amirkabir University of Tech. 11 MVC Design Pattern Examples Observer Pattern: decoupling of model from view changes to one object can affect multiple views, without requiring object to know details of view Composite Pattern: nesting of views class hierarchy in which some subclasses define primitive objects and other classes define composite objects that assemble primitives into more complex objects Strategy Pattern: view-controller relationship allows controller to replaced statically or dynamically
Dept. of Computer Engineering, Amirkabir University of Tech. 12 Main elements of a design pattern Name: Facilitates communication Problem: context of application, when to apply Solution: Elements that makeup the Design Consequences: Results and trade offs of applying the pattern
Dept. of Computer Engineering, Amirkabir University of Tech. 13 Design Pattern Template Classification Intent Other names Motivation Applicability Structure Participants Collaborations Implementation Sample code Known uses Related Patterns
Dept. of Computer Engineering, Amirkabir University of Tech. 14 Template cont.. Pattern Name and Classification: Succinct, captures essences of pattern, reflects scheme Intent: Short statement that answers the following: What does it do? What is its rationale and intent What particular design issue or problem does it address?
Dept. of Computer Engineering, Amirkabir University of Tech. 15 Template cont.. Also Know As: aliases Motivation: Scenario which illustrates the design problem and how class and object structures in the pattern solve the problem. Applicability: When design pattern can be applied Examples of poor design the pattern can address How to recognize design patterns
Dept. of Computer Engineering, Amirkabir University of Tech. 16 Template cont.. Structure: Graphical representation of classes in the design pattern using OMT (Object Modeling Technique). Also utilizes interaction diagrams. Participants: Classes and/or objects participating in the design pattern and their responsibilities. Collaborations: How participants carry out their responsibilities.
Dept. of Computer Engineering, Amirkabir University of Tech. 17 Template cont.. Consequences: How a design pattern supports its objectives Trade-offs, results of using design pattern Aspects of system which can vary independently Implementation: pitfalls, hints or techniques language-specific issues Sample Code: Code fragments in C++ or Java
Dept. of Computer Engineering, Amirkabir University of Tech. 18 Template cont.. Known Uses: At least two examples of the pattern found in real systems. Related Patterns: What patterns are closely related? Important differences between closely related patterns. Which patterns is this design pattern used with?
Dept. of Computer Engineering, Amirkabir University of Tech. 19 Organization of Catalog Design patterns vary in granularity and level of abstraction. Families of related patterns help to programmer to learn patterns faster and can direct efforts to find new patterns. 23 design patterns total
Dept. of Computer Engineering, Amirkabir University of Tech. 20 Two classifications of a design pattern Purpose: What a design pattern does Scope: Specifies whether a design pattern applies primarily to classes or to objects
Dept. of Computer Engineering, Amirkabir University of Tech. 21 Design Pattern Purpose Creational (5): Concerns object creation Structural (7): Deals with composition of classes or objects Behavioral (11): Characterizes ways in which classes or objects interact and distribute responsibility.
Dept. of Computer Engineering, Amirkabir University of Tech. 22 The Catalog of Design Patterns CreationalStructuralBehavioral Factory MethodAdapterInterpreter Template Method Abstract Factory Builder Prototype Singleton Adapter (object) Bridge Composite Decorator Façade Flyweight Proxy Chain of Responsibility Command Iterator Mediator Observer State Strategy Visitor Scope Class Object
Dept. of Computer Engineering, Amirkabir University of Tech. 23 Design Pattern Scope Class Patterns (4) Deal with relationships between classes and their subclasses Relationships established through inheritance, so they are fixed at compile time (static) Object patterns (20) Deal with object relationships Relationships can be changed at runtime (dynamic)
Dept. of Computer Engineering, Amirkabir University of Tech. 24 Six types of design patterns 1. Creational class patterns defer some part of object creation to subclasses 2. Creational object patterns defer some part of object creation to another object 3. Structural class patterns use inheritance to compose classes 4. Structural object patterns describe ways to assemble objects 5. Behavioral class patterns use inheritance to describe algorithms and flow of control 6. Behavioral object patterns describe how a group of objects cooperate to perform a task that no single object can carry out alone
Dept. of Computer Engineering, Amirkabir University of Tech. 25 Additional ways to organize design patterns Patterns which are used together Some patterns are alternatives for one another Some patterns result in similar design although they have different intents Patterns which reference one another (see Figure 1.1, p. 12) Additional ways to organize design patterns
Dept. of Computer Engineering, Amirkabir University of Tech. 26 What don’t these design patterns apply to? Concurrency Distributed programming Real-time programming Domain-specific patterns User interface design Device drivers Object-oriented DB
Dept. of Computer Engineering, Amirkabir University of Tech. 27 Course Outline Introduction to the course Review of some OO design problems and some OO design concepts. How design patterns can solve design problems A case Study. Showing some design problems and solutions leading to some patterns. Reviewing pattern structure and other attributes and some sample code related to the use case. Review of the rest of patterns, finding use cases (collaboratively with class) Your presentation of use cases in your assignments Midterm & Final Exam
Dept. of Computer Engineering, Amirkabir University of Tech. 28 What you need to know to excel in this course Good knowledge and experience with OO design Knowledge of Java or C++ Design and coding experience. You must have experienced solving a problem over and over to appreciate the patterns presented
Dept. of Computer Engineering, Amirkabir University of Tech. 29 Course requirements Group assignments %30 Student presentations of the assignment solutions Class work %15 Midterm & Final Exams %25 &%25 Class participation %10 Students will be penalized for being absent from lectures and for late delivery of assignments