Dr. Tom WayCSC 47001 Design Patterns & Anti-Patterns CSC 4700 Software Engineering.

Slides:



Advertisements
Similar presentations
Softwaretechnologie für Fortgeschrittene Teil Thaller Stunde IV: Software Engineering I Köln 21. Januar 2010.
Advertisements

Design Patterns.
 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Overview of Design Patterns
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Chapter 6: Using Design Patterns
1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.
Design Patterns A brief introduction to what they are, why they are useful, and some examples of those that are commonly used.
CS CS 5150 Software Engineering Lecture 17 Object Oriented Design 3.
Design Patterns CS is not simply about programming
1 CS 501 Spring 2008 CS 501: Software Engineering Lectures 17 & 18 Object Oriented Design 3 & 4.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Design Patterns.
CS CS 5150 Software Engineering Lecture 16 Object Oriented Design 2.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
1 CS 501 Spring 2007 CS 501: Software Engineering Lectures 17 & 18 Object Oriented Design 3 & 4.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
An Introduction to Software Architecture
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
UML - Patterns 1 Design Patterns. UML - Patterns 2 Becoming Good OO Developers Developing good OO Software is hard Takes a lot of time to take advantage.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Structural Design Patterns
ECE450S – Software Engineering II
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
CSC 480 Software Engineering Design With Patterns.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
OO Methodology Elaboration Iteration 2 - Design Patterns -
FacadeDesign Pattern Provide a unified interface to a set of interfaces in a subsystem. Defines a high level interface that makes the subsystem easier.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Design Patterns ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 31. Review Creational Design Patterns – Singleton Pattern – Builder Pattern.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
CS 5150 Software Engineering Lecture 16 Program Design 3.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
COP 3331 Object-Oriented Analysis and Design 1 Design Overview  Design Overview (Ch 6)  Review of RAD  System Design  System Design Concepts  System.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Overview of Design Patterns
Design Patterns: MORE Examples
Design Patterns A brief introduction to what they are, why they are useful, and some examples of those that are commonly used.
Software Architecture and Quality BY
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Introduction to Design Patterns
Design Patterns.
Observer Design Pattern
Design Patterns with C# (and Food!)
Design Patterns Satya Puvvada Satya Puvvada.
Design Patterns - A few examples
Informatics 122 Software Design II
Patterns.
CSC 480 Software Engineering
Behavioral Patterns Part-I introduction UNIT-VI
Informatics 122 Software Design II
CSC 480 Software Engineering
Chapter 8, Design Patterns Introduction
Presentation transcript:

Dr. Tom WayCSC Design Patterns & Anti-Patterns CSC 4700 Software Engineering

SE, Design, Hans van Vliet, ©20082 Patterns Provide solutions to recurring problems Balance sets of opposing forces Document well-proven design experience Abstraction above level of a single component Provide common vocabulary and understanding Are a means of documentation Support software devel with desirable properties

Purpose A design pattern captures design expertise – patterns are not created from thin air, but abstracted from existing design examples Using design patterns is reuse of design expertise Studying design patterns is a way of studying how the “experts” do design Design patterns provide a vocabulary for talking about design

Why design patterns in SA? If you’re a software engineer, you should know about them anyway Design Patterns help you break out of first- generation OO thought patterns

The seven layers of architecture * Global architecture Enterprise architecture System architecture Application architecture Macro-architecture Micro-architecture Objects * Mowbray and Malveau ORB OO architecture Frameworks Subsystem Design patterns OO programming

How patterns arise Problem Context Solution Benefits Related Patterns Consequences Forces

Structure of a pattern Name Intent Motivation Applicability Structure Consequences Implementation Known Uses Related Patterns

Key patterns The following patterns are considered to be a good “basic” set of design patterns Competence in recognizing and applying these patterns will improve your design skills

Composite Component Operation() Add(Component) Remove(Component) Composite Operation() Add(Component) Remove(Component) Leaf Operation() Client children 0..* For all c in children c.Operation(); Construct part-whole hierarchy Simplify client interface to leaves/composites Easier to add new kinds of components

Composite (2) Figure paint() translate() getBounds() CompositeFigure paint() addFigure(Figure)) removeFigure(Figure)) BasicFigure paint() View children 0..* For all c in children c.paint(); Example: figures in a structured graphics toolkit LabelFigure paint() 0..* Controller parent

Adapter You have legacy code current client Adapter changes interface of legacy code so client can use it Adapter fills the gap b/w two interfaces No changes needed for either legacy code, or client

Adapter (2) class NewTime { public: int GetTime() { return m_oldtime.get_time() * ; } private: OldTime m_oldtime; };

Command You have commands that need to be executed, undone, or queued Command design pattern separates Receiver from Invoker from Commands All commands derive from Command and implement do(), undo(), and redo()

Facade Provide unified interface to interfaces within a subsystem Shield clients from subsystem components Promote weak coupling between client and subsystem components Facade Client

Facade (2) Entity CompositeEntityAtomicEntity PortRelation BufferedRelation Graph SchematicEditor Example: graph interface to a simulation engine Actor Director 0..* 2 Token

Proxy You want to delay expensive computations, use memory only when needed, or check access before loading an object into memory Proxy has same interface as Real object stores subset of attributes does lazy evaluation

Strategy Operation() ConcreteStrategy2 Operation() Context Make algorithms interchangeable---”changing the guts” Alternative to subclassing Choice of implementation at run-time Increases run-time complexity ContextInterface() ConcreteStrategy1 Operation()

Strategy (2) ConnectorRouter Shape recalculate(Pt, Pt) ArcRouter Shape recalculate(Pt, Pt) Connector route() StraightRouter Shape recalculate(Pt, Pt) ManhattanRouter Shape recalculate(Pt, Pt) shape=router.recalculate(start,end); redraw(shape); Example: drawing different connector styles

Bridge You have several different implementations need to choose one, possibly at run time Bridge decouples interface from implementation shields client from implementations Abstraction creates and initializes the ConcreteImplementations Example: stub code, slow code, optimized code

Bridge (2) Client Implementor Refined Abstraction Concrete ImplementorA Concrete ImplementorB Abstraction

Observer Many-to-one dependency between objects Use when there are two or more views on the same “data” aka “Publish and subscribe” mechanism Choice of “push” or “pull” notification styles Observer update() Subject attach(Observer) detach(Observer) notify() ConcreteObserver update() ConcreteSubject getState() state=subject.getState(); forall o in observers o.update()

22 Model-View-Controller (MVC) Objective: Separation between information, presentation and user interaction. When a model object value changes, a notification is sent to the view and to the controller. Thus, the view can update itself and the controller can modify the view if its logic so requires. When handling input from the user the windowing system sends the user event to the controller. If a change is required, the controller updates the model object.

23 Model-View-Controller Software Architecture: Foundations, Theory, and Practice; Richard N. Taylor, Nenad Medvidovic, and Eric M. Dashofy; © 2008 John Wiley & Sons, Inc. Reprinted with permission.

Factory Method Creator Product createProduct() Product Defer object instantiation to subclasses Eliminates binding of application-specific subclasses Connects parallel class hierarchies A related pattern is AbstractFactory operation() ConcreteCreator Product createProduct() ConcreteProduct operation() return new ConcreteProduct();

Factory Method (2) Example: creating manipulators on connectors Figure createManipulator() 0..1 Manipulator attach(Figure) ArcManipulator attach(Figure) BoundsManipulator attach(Figure) Connector createManipulator() RectFigure createManipulator() manip = new BoundsManipulator(); manip = new ArcManipulator(); Interactor

Chain of Responsibility Handler handleRequest() ConcreteHandler2 handleRequest() Client ContextInterface() ConcreteHandler1 handleRequest() Decouple sender of a request from receiver Give more than one object a chance to handle Flexibility in assigning responsibility Often applied with Composite successor

Chain of Responsibility (2) Figure handleEvent(Event) CompositeFigure Interactor children 0..* If interactor != null interactor.handle(event,this) else parent.handleEvent(event) 0..1 parent Example: handling events in a graphical hierarchy handle(Event,Figure) 0..*

Patterns vs “Design” Patterns are design But: patterns transcend the “identify classes and associations” approach to design Instead: learn to recognize patterns in the problem space and translate to the solution Patterns can capture OO design principles within a specific domain Patterns provide structure to “design”

Patterns vs Frameworks Patterns are lower-level than frameworks Frameworks typically employ many patterns: Factory Strategy Composite Observer Done well, patterns are the “plumbing” of a framework

Dr. Tom WayCSC Anti-Patterns CSC 4700 Software Engineering

Auntie Patterns Dr. Tom WayCSC Aunt “Patt”

Ant Tea Patterns Dr. Tom WayCSC

SE, Design, Hans van Vliet, © Anti-Patterns and Bad Smells Patterns describe desirable behavior Anti-patterns describe situations one had better avoid Refactoring is applied whenever an anti- pattern has been introduced Bad smells occur when something in your design seems “fishy” They are not necessarily indications of problems

SE, Design, Hans van Vliet, © Example anti-patterns God class: class that holds most responsibilities (also called The Blob) Lava flow: dead code Poltergeist: class with few responsibilities and a short life Golden Hammer: solution that does not fit the problem Stovepipe: (almost) identical solutions at different places Swiss Army Knife: excessively complex class interface Anti

More example anti-patterns Singletonitis – over-use of the singleton pattern Sequential coupling – requires methods to be called in particular order Object orgy – failing to properly encapsulate objects permitting unrestricted access to their internals Blind faith – neglecting to test error returns from methods Loop-switch sequence – implementing sequential code as a loop statement, i.e. first time through do A, second time do B etc, rather than doA(); doB(); Magic numbers/strings – unexplained number/string values in code

The Blob

Golden Hammer I have a hammer and everything else is a nail

Spaghetti Code

Cut−And−Paste Programming “Man, you guys work fast. Over 400,000 lines of code in three weeks is outstanding progress!” “Hey, I thought you fixed that bug already, so why is it doing this again?”