How Would You Solve This? l Suppose that you want to have one and only one instance of a class – Can you think of ever only wanting one instance of a class?

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Lecture 9 Design Patterns CSCI – 3350 Software Engineering II Fall 2014 Bill Pine.
CS590L - Lecture 6 1 CS590L Distributed Component Architecture References: - E. Gamma, R. Helm, R. Johnson, J. Vlissides, Design Patterns: Elements of.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
05/26/2004www.indyjug.net1 Indy Java User’s Group June Knowledge Services, Inc.
(c) 2009 University of California, Irvine – André van der Hoek1June 13, 2015 – 21:42:16 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Design Patterns CS is not simply about programming
Copyright © Active Frameworks, Inc.. - All Rights Reserved - V2.0 Introduction - Page L1-1 PS95&96-MEF-L1-1 Dr. M.E. Fayad Creationa l Paradigm.
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Design Patterns.
(c) 2010 University of California, Irvine – André van der Hoek1June 29, 2015 – 08:55:05 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Object-Oriented Design Patterns CSC 335: Object-Oriented Programming and Design.
Design Patterns Trends and Case Study John Hurst June 2005.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
Chapter Five An Introduction to Design Patterns Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information.
CSSE 374: Introduction to Gang of Four Design Patterns
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
January 12, Introduction to Design Patterns Tim Burke References: –Gamma, Erich, et. al. (AKA, The Gang of Four). Design Patterns: Elements of Reusable.
Design Patterns in Java Chapter 1 Introduction Summary prepared by Kirk Scott 1.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Powerpoint Templates Page 1 Powerpoint Templates What is Design Patterns ? by Indriati Teknik Informatika – UB.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
DESIGN PATTERNS CSC532 Adv. Topics in Software Engineering Shirin A. Lakhani.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Patterns Introduction General and reusable solutions to common problems in software design SoftUni Team Software University
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
1 Design Patterns Object-Oriented Design. 2 Design Patterns 4Reuse of design knowledge and experience 4Common in many engineering disciplines 4Avoids.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
Design Patterns in Context ©SoftMoore ConsultingSlide 1.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
1 Good Object-Oriented Design Dr. Radu Marinescu Lecture 4 Introduction to Design Patterns.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns: MORE Examples
How Would You Solve This?
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Introduction to Design Patterns
Design Patterns Lecture part 2.
Introduction to Design Patterns
Design Patterns Introduction
object oriented Principles of software design
Design Patterns in Game Design
Informatics 122 Software Design II
DESIGNING YOUR SYSTEM.
DESIGN PATTERNS : Introduction
Informatics 122 Software Design II
Presentation transcript:

How Would You Solve This? l Suppose that you want to have one and only one instance of a class – Can you think of ever only wanting one instance of a class? »Lock manager? »One PubSub Bus instance l So, how? CS 3505 L19 - 1

Singleton Class Diagram l Explain how/why this works l Will this work for any time that you want to have one and only one instance of any class? CS 3505 L19 - 2

CS 3505 L Here Is Another Problem l Suppose that you have a set of sibling classes and then you want to optionally add other functionality

CS 3505 L Or How About This Problem? l Suppose that you have an object that changes periodically. Suppose that there are other objects (some or many) that want to watch and be notified of the changes.

CS 3505 L Design Patterns Some slides “borrowed” from: CS335 Univ of Arizona SYSC2004 Carlton University CS Univ of Melbourne

CS 3505 L Becoming a software designer l First learn the rules – Algorithms, data structures and languages l Then learn the principles – Structured design, OO design l However to truly master software design, you must study the design of masters – These designs have patterns to be understood, remembered and re-used l Implies DESIGN PATTERNS!!!

CS 3505 L What is good design? l 30 years ago, Christopher Alexander (an architect currently at UC Berkeley) asked – Are beauty and quality objective? »Can we agree some things are beautiful and some are not? – What makes a good architectural design? – What makes bad architectural design? – Can we recognize good design? – Is there an objective basis for such a judgment? – Is there a basis for describing common consensus l Alexander believed that beauty can be described through an objective basis that can be measured. l Examples: – Symmetry is good – Use half-round arches to support bridges and in doors. l Same equivalent ideas exist in software design

CS 3505 L Cultural Anthropology l Within a culture, individuals agree what is good design, what is beautiful l There are patterns in cultures that serve as objective bases for judging design l Proposed: The quality of software can be measured objectively – This is the idea behind design patterns – Patterns are not invented, they are recognized as elegant solutions that have been used many times

CS 3505 L Two Questions l Alexander asks – What is present in good quality design that is not present in a poor quality design? – What is present in poor quality design that is not present in a good quality design? l The task is to identify the things that make a design good and those that make a design bad l What are the commonalities in what is viewed as good (and what is viewed as bad) – A software system that is relatively easy to maintain is considered good »A fragile software system is considered bad – A software system that is easy to understand is considered good »obfuscated spaghetti code is bad

CS 3505 L Back to Architecture l Alexander observed many buildings, towns, streets, gardens, and pieces of furniture l Good constructs had things in common l They can be different but still of high quality – They may solve different problems – Consider a porch »A porch may be used to provide shade on a hot sunny day »Another porch provides a transition from the outside to the inside and from the inside to the outside l Did you ever live where there was no roof over the door?

CS 3505 L Can Solve the Same Problem in Different Ways l Or two different porches might solve the same problem in two different ways – Both may provide a pleasing way to enter or exit a house and still look different l Alexander was trying to identify and describe the consistency of quality in design – Having a transition from room to room or from inside to outside is important – Those transitions can take many forms

CS 3505 L Patterns l Alexander looked at different structures that solved the same problem l Found similarities between designs of high quality l Alexander called these similarities patterns l A pattern is a solution to a problem in a context l "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." –Christopher Alexander, A Pattern Language: Towns/Buildings/Construction, 1977

CS 3505 L Does this relate to Software? l Early 1990s software developers wondered if the same approach could be used in our discipline – Are there problems that occur over and over again that could be solved in somewhat the same way? – Could software be developed in terms of patterns, writing solutions after the pattern was identified? l People started looking for patterns l Then came one of the biggest selling books in CS...

CS 3505 L Gang of Four The GoF Book l The book with the greatest influence is Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. “Design Patterns: Elements of Reusable Object-Oriented Software”. Reading Mass. Addison Wesley, This book catalogs 23 patterns. l They offer advice and help to develop quality software l An object-oriented design pattern systematically names, explains and evaluates an important and recurring design in object-oriented systems

CS 3505 L Patterns in the GoF book l The Gang of Four did not invent the patterns l They found patterns that already existed in the software community – These are the similarities found in the design of software viewed to be of high quality – They made a rule to list three different systems that solved a problem in the same manner l Thousands of patterns have been written since – telecommunications patterns, Pedagogical patterns, analysis patterns, indexing patterns l Patterns have been called the next best software improvement since object-oriented programming.

CS 3505 L Why Study Patterns? l Can reuse solutions – Gives us a head start – Avoids the gotchas later (unanticipated things) – No need to reinvent the wheel l Establish common terminology – Design patterns provide a common point of reference l Provide a higher level perspective – Frees us from dealing with the details too early

CS 3505 L Other advantages l Most design patterns make software more modifiable, less brittle – We are using time tested solutions l Helps increase the understanding of basic object- oriented design principles – Encapsulation, inheritance, interfaces, polymorphism l Designs of large systems can avoid large inheritance hierarchies (there are alternatives)

CS 3505 L Design Patterns l The idea of a pattern is fairly simple. – Represents a useful idea that can be reused in many context. l Patterns vary in structure, but they all contain similar kinds of information – This information helps us determine the whys and hows of the pattern

Design Pattern Information l Name: Should be evocative – So it is easy to recall a pattern l Intent: What this pattern tries to do l Motivation: Why this pattern exists – What problems does this solve l Documentation: How this pattern works – Examples a must l Applicability: Where this pattern is best used – and where is it maybe less effective CS 3505 L

CS 3505 L Design Patterns l The exact format varies from pattern collection to collection – Lots of collections out there. l One major variation is a Anti-pattern approach – Describe a bad situation so it’s easy to recognize – Then, present the solution in pattern form that solves the current problem

CS 3505 L GoF Pattern Types l Creational Patterns – Deal with initializing and configuring classes and objects. l Structural Patterns – Deal with decoupling interface and implementation of classes and objects. l Behavioral Patterns – Deal with dynamic interactions among societies of classes and objects.

CS 3505 L Creational Patterns l Abstract Factory – Creates an instance of several families of classes l Builder – Separates object construction from its representation l Factory Method – Creates an instance of several derived classes l Prototype – A fully initialized instance to be copied or cloned l Singleton – A class of which only a single instance can exist – Ha!!!

CS 3505 L Structural Patterns l Adapter – Match interfaces of different classes l Bridge – Separates an object’s interface from its implementation l Composite – A tree structure of simple and composite objects l Decorator – Add responsibilities to objects dynamically l Façade – A single class that represents an entire subsystem l Flyweight – A fine-grained instance used for efficient sharing l Proxy – An object representing another object

CS 3505 L Decorator l Intent – Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. l Problem – You want to add behavior or state to individual objects at run-time. Inheritance is not feasible because it is static and applies to an entire class. l Example – The Decorator attaches additional responsibilities to an object dynamically. The ornaments that are added to pine or fir trees are examples of Decorators. Lights, garland, candy canes, glass ornaments, etc., can be added to a tree to give it a festive look. The ornaments do not change the tree itself which is recognizable as a Christmas tree regardless of particular ornaments used. As an example of additional functionality, the addition of lights allows one to "light up" a Christmas tree.

CS 3505 L Decorator l UML class diagram:

CS 3505 L Decorator

CS 3505 L Behavioral Patterns l Chain of Responsibility – A way of passing a request between a chain of objects l Command – Encapsulate a command request as an object l Interpreter – A way to include language elements in a program l Iterator – Sequentially access the elements of a collection l Mediator – Defines simplified communication between classes l Memento – Capture and restore an object's internal state l Observer – A way of notifying change to a number of classes l State – Alter an object's behavior when its state changes l Strategy – Encapsulates an algorithm inside a class l Template Method – Defer the exact steps of an algorithm to a subclass l Visitor – Defines a new operation to a class without change

CS 3505 L Observer l Intent – Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. l Problem – A large monolithic design does not scale well as new graphing or monitoring requirements are levied. l Example – The Observer defines a one-to-many relationship so that when one object changes state, the others are notified and updated automatically. Some auctions demonstrate this pattern. Each bidder possesses a numbered paddle that is used to indicate a bid. The auctioneer starts the bidding, and "observes" when a paddle is raised to accept the bid. The acceptance of the bid changes the bid price which is broadcast to all of the bidders in the form of a new bid.

CS 3505 L Observer l UML class diagram:

CS 3505 L Observer

Observer Sample // Create investors Investor s = new Investor("Sorros"); Investor b = new Investor("Berkshire"); // Create IBM and attach investors IBM ibm = new IBM("IBM", ); ibm.Attach(s); ibm.Attach(b); // Change price, which notifies investors ibm.Price = ; ibm.Price = ; abstract class Stock { protected double price; private ArrayList investors = new ArrayList(); public void Notify() { foreach (Investor investor in investors) { investor.Update(this); } } // Properties public double Price { get { return price; } set { price = value; Notify(); } }... } CS 3505 L

CS 3505 L When to use patterns l Solutions to problems that recur with variations. – No need to reuse if the problem occurs only once. l Solutions that require several steps. – Not all problems need all steps – Patterns can be an overkill is problems have simple solutions.

CS 3505 L When not to use Design Patterns l When applications that you build will not change… l Application code requirements are unique l Plenty of time to prototype your ideas l When other people don’t follow them!

CS 3505 L Benefits of Design Patterns l Enable large-scale reuse of software architecture l Explicitly capture expert knowledge and make it more widely available l Help improve developer communication

CS 3505 L Drawbacks to Design Patterns l Do not lead to direct code reuse l Are deceptively simple l Patterns may be overkill l Are validated by experience and discussion l Pattern integration is a human-intensive activity

CS 3505 L Summary l Roots in “real” architecture – Christopher Alexander l Well known solutions that work l Different patterns for different tasks l Design patterns help to achieve a flexible, maintainable and reusable software design l Keep pitfalls and drawbacks in mind! l – GoF patterns in C# with examples