Week 6, Day 3 The Gang of Four and more … A new design pattern SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.

Slides:



Advertisements
Similar presentations
SE2811 Week 7, Class 2 The Gang of Four and more … Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder.
Advertisements

March Ron McFadyen1 Composite Used to compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat.
Fall 2009ACS-3913 Ron McFadyen Composite Pattern Problem: How do we treat a composition structure of objects the same way as a non-composite object? Arises.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Design Patterns CS is not simply about programming
Software Engineering I Object-Oriented Design Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
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.
1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.
1 PH Chapter 1 (pp. 1-10) GoF Composite Pattern (pp ) PH Ch 2 through Fundamentals (pp ) Presentation by Julie Betlach 5/28/2009.
Composite Design Pattern. Motivation – Dynamic Structure.
Design Patterns Discussion of pages: xi-11 Sections: Preface, Forward, Chapter
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
ISP666 MVC & Design Patterns. Outline Review Event Programming Model Model-View-Controller Revisit Simple Calculator Break Design Patterns Exercise.
Aniruddha Chakrabarti
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
1-1 C Sc 335 Course Overview Object-Oriented Programming and Design Rick Mercer.
GoF: Document Editor Example Rebecca Miller-Webster.
Week 2, Day 2: The Factory Method Pattern Other good design principles Cohesion vs. Coupling Implementing the Strategy Pattern Changing strategies (behaviors)
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Week 6, Class 1 & 2: Decorators Return Exam Questions about lab due tomorrow in class? Threads Locking on null object invokeLater & the squares example.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
CS2852 Week 5, Class 2 Today Queue Applications Circular Queue Implementation Testing SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
SE-2811 Software Component Design Week 1, Day 2 Making teams Warm-up exercise Design pattern defined SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick.
Week 5, Day 3: Decorator Decorators Muddiest Point Tomorrow: Quiz on lab reading: web.msoe.edu/hasker/se2811/labs/5/ SE-2811 Slide design:
Week 5, Day 2: Decorator Decorators Muddiest Point Tomorrow: Quiz on lab reading: web.msoe.edu/hasker/se2811/labs/5/ SE-2811 Slide design:
Object-Oriented Software Engineering Practical Software Development using UML and Java Modelling with Classes.
Slide design: Dr. Mark L. Hornick
Week 10, Day 3 Review for the quarter SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Week 5, Class 3: Decorators Lab questions? Example: Starbuzz coffee Basic Pattern More examples Design Principles Compare with alternatives SE-2811 Slide.
SE2811 Week 8 – Class 2 Re: Lab due tonight SE-2811 Slide design: Dr. Mark L. Hornick Much Content: Dr. Hornick Most Errors: Dr. Yoder 1.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns: MORE Examples
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
MPCS – Advanced java Programming
Week 2, Day 1: The Factory Method Pattern
Design Patterns Introduction
Composite Pattern SE2811 Software Component Design
Design Patterns - A few examples
Jim Fawcett CSE776 – Design Patterns Summer 2003
Week 7, Class 1: The Command Pattern (cont.)
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
13. Composite Pattern SE2811 Software Component Design
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
13. Composite Pattern SE2811 Software Component Design
Slide design: Dr. Mark L. Hornick
11. MVC SE2811 Software Component Design
Slide design: Dr. Mark L. Hornick
Week 8, Class 3: Model-View-Controller
Slide design: Dr. Mark L. Hornick
Composite Design Pattern By Aravind Reddy Patlola.
11. MVC SE2811 Software Component Design
16. Visitors SE2811 Software Component Design
Slide design: Dr. Mark L. Hornick
Presentation transcript:

Week 6, Day 3 The Gang of Four and more … A new design pattern SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1

The Gang of Four © Head First Design Patterns -- Eric and Elisabeth Freeman 2

The Gang of Four Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, 1994Erich GammaRichard HelmRalph JohnsonJohn Vlissides SE-2811 Dr. Mark L. Hornick 3 and more … Portland Pattern Repository ( Portland Pattern Repository (

Gang of Four Patterns 23 patterns (See handout) divided into three categories: Creational Involve object instantiation and all provide a way to decouple a client from the objects it needs to instantiate Behavioral Are concerned with how classes and objects interact and distribute responsibility Structural Let you compose classes or objects into larger structures 4

Patterns so far Strategy Singleton Factory Method Observer Decorator Exercise: For each pattern, decide which category it falls in, and highlight it on the handout. SE-2811 Dr. Mark L. Hornick 5 Behavioral Structural Creational

SE-2811 Dr. Josiah Yoder 6 Behavioral Structural Creational

And more… The Portland Pattern Repository (at c2.com) contains all of the GoF patterns, and more* The Portland Pattern Repositoryc2.com ObjectPoolPattern – for reusing e.g. threads ObjectPoolPattern ExternalizeTheStack – For implementing recursion with a custom stack ExternalizeTheStack LockPattern – Temporarily lock (or perhaps queue?) changes to an object LockPattern SE-2811 Dr. Mark L. Hornick 7

Final Project From the syllabus… “…As a final assignment, students conduct research on software design patterns and present and demonstrate the results of their investigations to the class.” SE-2811 Dr. Mark L. Hornick 8

Details about final project Choose a pattern not covered in this course First-come, first serve (Only one team per pattern) Must be approved by instructor Implement an example of that pattern Teach the rest of the class about that pattern (short time period limit TBD, but maybe 15 min or maybe 30 min) SE-2811 Dr. Mark L. Hornick 9

Applications of a new pattern… Graphics drawing applications Renders graphic primitives But also “groups” (this week’s lab) Tablet Communication tool for kiddos Child selects general topic (e.g. “fruit”) from first screen Zooms in to select more specific topic (e.g. “banana”)

Exercise How many program structures can you think of where different “parts” are grouped together to become a “part” themselves? SE-2811 Dr. Mark L. Hornick 11

The Composite Pattern is applied in situations involving object hierarchies The problem A collection of objects forms a hierarchy Each object may be An individual (primitive, leaf, or part) object A composition of other objects (composite) We want to treat all objects uniformly No special treatment (if, instanceof) for sub-composite objects Solution Compose objects into recursive tree structures via the Composite Pattern

The Composite Pattern allows you to compose objects into tree structures to represent part-whole hierarchies aComposite aPart This pattern allows clients to treat individual objects (Parts) and compositions of objects (Composites) uniformly.

Example (see code) computer System Unit monitor You want to build a new computer. Let’s configure the computer as a system of hierarchical components. HDDCabinet Chassis CPU keyboard MemoryGPU Fan Motherboard composite part

Composite Pattern class diagram and key classes/interfaces SE-2811 Dr. Mark L. Hornick 15 Client app ClientApp uses the Component Interface to manipulate objects in the composition by calling add(), remove(), and context-specific operations. Component defines an interface (or abstract class) for all objects: both Leaf and Composite A part/leaf can have no children; methods like add(), remove() don’t make sense for this class, but are still inherited from Component. Composite defines the behavior of those Components having children and stores the child Components.

The Component interface defines the behavior that both Parts and Composites must implement public interface Component { // behaviors for Part and Composite public void add(Component c); public void remove(Component c); public List getChildren(); public abstract double getPrice(); // Part-specific... } Exercise: Decide: Should/can Component be defined as an Interface rather than an Abstract Class? Explain your answer.

public class Composite implements Component{ private String name; private double price; private List components; Composite (String name, double basePrice) { // ctor details components = new ArrayList (); } // essential Composite behaviors: public void add(Component c) { components.add(c); } public void remove (Component c){ components.remove(c); } public List getChildren() { return components; }

...public class Composite implements Component{...continued from previous slide // context-specific behavior public double getPrice() { double compositePrice = price; for( Component c: components ){ compositePrice += c.getPrice(); } return compositePrice ; }

public class Part implements Component{ // Part is a “Leaf” private String name; private double price; Part(String name, double price) { this.name = name; this.price = price; } // Composite-related behaviors public void add(Component c) { // what should we do here?? // do nothing? Throw exception? Return a true/false? } public void remove(Component c){ // same as above; what should we do here?? } public Collection getChildComponents(); // what can we do here?? // Throw an exception? // Return a null? // return Collections.EMPTY_LIST; } }

Design Principles Which ones does composite achieve? Reduce coupling (dependencies reduced) Increase cohesion Encapsulate what varies Favor composition over inheritance Program to interfaces, not implementations Classes should be open for extension but closed for modification “transparency” SE-2811 Dr. Mark L. Hornick 20

SE-2811 Dr. Mark L. Hornick 21

Choosing a Design Pattern (1) We would like to only create as many threads as we have cores on this machine… if part of our program wants a thread, it should go to one spot to get it. You are processing a file while reading it, and you would like to know what the next byte is that will be read, without “reading” it (so the next byte read will still give you that byte) SE-2811 Dr. Mark L. Hornick 22

Choosing a Design Pattern (2) You’ve got a class that implements Comparable, and it’s great for storing in VariousTrees, but for some rare objects, the Comparable option takes WAY too long. You’d like to modify the compare method so that if some fixed amount of time elapses, a runtime exception is thrown so you can handle the rogue objects. Only problem – you can’t modify the original class – it’s in compiled code. SE-2811 Dr. Mark L. Hornick 23

Choosing a Design Pattern (3) You want your clients to be able to write plugins to represent new shapes in your shape application. You don’t know what these shapes are (yet), but you want to be able to select them from a shape toolbar. You are designing a new version of Enterprise Architect. You would like to be able to represent various components of your program, such as classes and methods, modeling sub-classes naturally. SE-2811 Dr. Mark L. Hornick 24

Choosing a Design Pattern (4) Throughout your program, you would like to play sounds, but you only have one audio card. You are implementing a graphical user interface toolkit. You would like to create “sections of the screen” that can contain arbitrary buttons, text fields, etc. SE-2811 Dr. Mark L. Hornick 25

Choosing a Design Pattern (5) You have this great class (again, no source) that does cool stuff. But some methods take a long time to run. You’d like to have a version of that class which saves a computed value, and only recomputes the value if the state of the class changes. Depending on which operating system your File is on, it may be implemented differently. The user should get the right implementation automatically. SE-2811 Dr. Mark L. Hornick 26

SE-2811 Dr. Mark L. Hornick 27