SE-2811 Software Component Design Week 1, Day 1 Design pattern defined Code that needs a design pattern… SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick.

Slides:



Advertisements
Similar presentations
Strategy Pattern1 Design Patterns 1.Strategy Pattern How to design for flexibility?
Advertisements

Inheritance Java permits you to use your user defined classes to create programs using inheritance.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Inheritance issues SE-2811 Dr. Mark L. Hornick 1.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Visual Basic Introduction IDS 306 from Shelly, Cashman & Repede Microsoft Visual Basic 5: Complete Concepts and Techniques.
Chapter 8 Object Design Reuse and Patterns. Finding Objects The hardest problems in object-oriented system development are: –Identifying objects –Decomposing.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Chapter 25 More Design Patterns.
Week 5, Day 3: Observer Today Reducing coupling with the Observer The Observer pattern in Java APIs Posting events to a UI worker thread SE-2811 Slide.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 11 : Frameworks SWE 316: Software Design and Architecture  To understand.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
Week 2, Day 2: The Factory Method Pattern Other good design principles Cohesion vs. Coupling Implementing the Strategy Pattern Changing strategies (behaviors)
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
SE-2811 Software Component Design Week 1, Day 2 (and 1-3 and 2-1) SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 1.
CS-2852 Data Structures Week 5, Class 2 – Testing and Stacks Announcement: Lab Demos - 2/3 & 4 on Friday Testing  Definitions, Example  (tentative) Testing.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
Use Case Textual Analysis
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Systems Requirements SE 3821 Design? Algorithms CS 2852.
Week 7, Day 3 Half-Exam 2 A New Pattern SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
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.
Systems Requirements SE 3821 Design? Algorithms CS 2852.
SE-1021 Software Engineering II Week 9, Class 1 ByteBuffer Tomorrow Get quizzes back from last week… Wednesday Quiz at start of lab 1.
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 9, Day 2 Object-oriented Design Acknowledgement: These slides by Dr. Hasker SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
Model-View-Controller A Design Pattern SE-2030 Dr. Rob Hasker 1 Based on slides written by Dr. Mark L. Hornick Used with permission.
Slide design: Dr. Mark L. Hornick
High Level Design Use Case Textual Analysis SE-2030 Dr. Mark L. Hornick 1.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Week 6, Class 3: Composite Swing composites File composites Computer composites SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
CS-2852 Data Structures Week 5, Class 3 – Testing and Recursion Queue Implementing finite queues Binary Search Recursion Tomorrow – Quiz, Lab demos, Lab.
Slide design: Dr. Mark L. Hornick
Sections Inheritance and Abstract Classes
Slide design: Dr. Mark L. Hornick
Week 2, Day 1: The Factory Method Pattern
SE-2811 Software Component Design
Copyright © by Curt Hill
Interfaces I once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session,
Strategy Design Pattern
SE-2811 Software Component Design
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Model-View-Controller
1. Where to start SE2811 Software Component Design Dr. Josiah Yoder
SE-2811 Software Component Design
SE-2811 Software Component Design
Week 7, Class 1: The Command Pattern (cont.)
SE-2811 Software Component Design
SE2811 Software Component Design Dr. Rob Hasker
Objects First with Java A Practical Introduction using BlueJ
14. Factory Pattern SE2811 Software Component Design
SE2811 Software Component Design Dr. Rob Hasker
14. Factory Pattern SE2811 Software Component Design
Objects First with Java A Practical Introduction using BlueJ
Week 6, Class 2: Observer Pattern
Slide design: Dr. Mark L. Hornick
Week 8, Class 3: Model-View-Controller
SE-1021 Software Engineering II
Slide design: Dr. Mark L. Hornick
SE-1021 Software Engineering II
SE-1021 Software Engineering II
SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder
SE2811 Software Component Design Dr. Rob Hasker
16. Visitors SE2811 Software Component Design
Presentation transcript:

SE-2811 Software Component Design Week 1, Day 1 Design pattern defined Code that needs a design pattern… SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 1

A design pattern is a general reusable solution to a commonly occurring problem in software design. – Dr. Urbain SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 2

A design pattern is… A “standard” arrangement (structure) of certain classes that comprise a solution to a given problem Many different Patterns can appear in a single application During execution, the objects created from these classes interact in a specific way (behavior) to implement a specific function The same type of function that is typically needed by many types of applications SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick Slide credit: Dr. Urbain 3

Analogy 4 Problem In every house and every workplace there is a daily "traffic" of the objects which are handled most. Unless such things are immediately at hand, the flow of life is awkward, full of mistakes; things are forgotten, misplaced. Solution Build waist-high shelves around at least a part of the main rooms where people live and work. Make them long, 9 to 15 inches deep, with shelves or cupboard underneath. Interrupt the shelf for seats, windows, and doors. SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick Slide credit: Dr. Urbain

Analogy (cont.) 5 Waist high shelves are everywhere They look different Different materials Different colors Different dimensions Different names Yet, they solve a very important recurring problem. SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick Slide credit: Dr. Urbain

Consider a (badly written) generic Duck class //Duck daffy = new Duck(Duck.MALLARD, “daffy”); //Duck donald = new Duck(Duck.REDHEAD, “donald”); //Duck clyde = new Duck(Duck.DECOY, “clyde”);... public void swim() { if( type == REDHEAD || type == MALLARD ) // swim in circles else // swim randomly } public void quack() { if( type == MALLARD || type == REDHEAD ) // real ducks quack else // do nothing; decoys don’t quack } SE-2811 Dr. Mark L. Hornick 6 SimUDuck v1

Duck is an example of a class that exhibits low cohesion Cohesion: A measure of how focused or strongly related the responsibilities of a class are Does a class do many unrelated things? If “yes”, then it has low cohesion (bad) Does a class represent only one thing? If “yes”, then it has high cohesion (good) SE-2811 Dr. Mark L. Hornick 7

Class Exercise With your neighbor for 3 minutes: Sketch a new design that solves this problem Quantity, not quality: Try to include as many details in your design as you can Use  a UML class diagram with just the key methods,  sketches about what code will go where  anything you want SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 8

Refactoring to improve cohesion via use of OO Inheritance Duck: an abstract class Abstract classes can define attributes and (default) behaviors common to all Duck-derived classes. Concrete classes implement type-specific adaptations (overrides) These may also include additional type-specific attributes (none in this case) SE-2811 Dr. Mark L. Hornick 9 SimUDuck v2

Is inheritance always a solution? What if some ducks (Mallards, Redheads) had similar behavior: Quacking sound Circular swimming pattern While others (Pintail) had different behaviors: Quacking sound Random swimming (ie floating) pattern And still others (Decoys) had: No quacking sound Random swimming (ie floating) pattern Lots of overriding (and maybe duplication of code)! Q1) Should we bother implementing any behaviors at all in the Duck abstract class if many will be overridden anyway (and possibly duplicated)? Q2) Can we modify the class hierarchy to group similar behaviors together? SE-2811 Dr. Mark L. Hornick 10 Code duplication?

What about multiple levels of abstraction? SE-2811 Dr. Mark L. Hornick 11 Here, the swim() and quack() behavior is defined, but not implemented, in Duck… …instead, swim() and quack() behaviors are implemented in a second level of abstract classes... …and finally inherited in concrete classes. SimUDuck v3 And what about a quiet, swimming Duck?

BUT: Multiple inheritance is not even allowed in Java! (FYI: it IS allowed in C++, but is EVIL) SE-2811 Dr. Mark L. Hornick 12 Here, the swim() and quack() behavior is defined, but not implemented, in Duck… …instead, swim() and quack() behaviors are implemented in a second level of abstract classes... …and finally inherited in concrete classes. SimUDuck v3

This can also lead to messy “class explosions” SE-2811 Dr. Mark L. Hornick 13 SimUDuck v3

Some reflections on inheritance… Allan Holub (a noted computer technology author) once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session, he asked him [Gosling]: Q: "If you could do Java over again, what would you change?" Gosling: "I'd leave out classes.” After the laughter died down, he explained that the real problem wasn't classes per se, but rather implementation inheritance (the extends relationship). Interface inheritance (the implements relationship), Gosling explained, is preferable. SE-2811 Dr. Mark L. Hornick 14

We can use interface inheritance to address these concerns… SE-2811 Dr. Mark L. Hornick 15 We eliminate implementation in abstract classes, and force concrete classes to supply it instead. …but now we’re back to the duplication of code problem that we saw in v2! SimUDuck v4