Engineering of Software II

34
Engineering of Software II Spring 2003

description

Engineering of Software II. Spring 2003. Why Design Patterns? Problems. The hard part about object-oriented design is decomposing a system into objects The task is difficult because many factors influence the decomposition, often in conflicting ways: encapsulation , granularity, - PowerPoint PPT Presentation

Transcript of Engineering of Software II

Page 1: Engineering  of Software II

Engineering of Software II

Spring 2003

Page 2: Engineering  of Software II

Why Design Patterns?Problems

The hard part about object-oriented design is decomposing a system into objects The task is difficult because many factors influence the decomposition, often in conflicting ways:

encapsulation, granularity, dependency, aggregation, flexibility, performance, evolution, reusability

Page 3: Engineering  of Software II

Why Design Patterns?Solutions

Expressing proven techniques as design patterns makes them more accessible to developers of new systems. Design patterns:

Make it easier to reuse successful designs and architectures

Can even improve the documentation and maintenance of existing systems

Help you identify less-obvious abstractions and the objects that can capture them

Put simply, design patterns help a designer get a design "right" faster.

Page 4: Engineering  of Software II

Introduction to Design Patterns

History of Design PatternsSummary of PatternsCharacterizing Design PatternsVisualizing Design PatternsCreational Design Patterns - concern the process of object creationBehavioral Design Patterns - deal with the composition of classes or objectsStructural Design Patterns - characterize the ways in which classes or objects interact and distribute responsibility

Page 5: Engineering  of Software II

History of Design Patterns

The history of design patterns in object-oriented programming is most often attributed to Erich Gamma, the initiating author of “Design Patterns: Elements of Reusable Object-Oriented Software.”The true father of design patterns is Christopher Alexander, an architect educated in mathematics, science, and engineering. Alexander believes you can design a building by simply applying one pattern after another. His theory does not deny the necessity of creativity in design and implementation, but his precise description of how patterns generate design is a clear implication that a pattern language can make the design process deterministic and repeatable.

Page 6: Engineering  of Software II

Model View Controller (MVC)

Page 7: Engineering  of Software II

Summary of PatternsPurpose

Creational Structural Behavioral

Scope

Class Factory Method Adapter Interpreter,Template Method

Object

Abstract Factory, Builder, Prototype, Singleton

Adapter Bridge, Composite, Decorator, Facade, Proxy

Chain of Responsibility, Command, Iterator, (Enumerator), Mediator, Memento, Flyweight, Observer, State, Strategy

Scope: Class patterns deal with static relationships between classes and their subclasses, which are fixed at compile-time. Object patterns deal with dynamic object relationships, which can be changed at run-time.

Page 8: Engineering  of Software II

Characterizing Design Patterns

1. name to communicate the design element.2. problem describes when to apply the

pattern. 3. solution describes the elements that make

up the design, their relationships, responsibilities, and collaborations. The solution doesn't describe a particular concrete design or implementation, because a pattern is like a template that can be applied in many different situations.

4. consequences are the costs/benefits of applying the pattern.

Page 9: Engineering  of Software II

Characterizing Design PatternsMore Detailed Characterization

IntentAlso Known AsMotivationApplicabilityStructureParticipantsCollaborations

ConsequencesImplementationSample CodeKnown UsesRelated Patterns

Page 10: Engineering  of Software II

Visualizing Design Patterns

Initially, the Object Modeling Technique (OMT) was used to represent design patterns, however, the Unified Modeling Language (UML) has replaced OMT.

UML captures real-world concepts (objects), their attributes, and the associations between these concepts.

UML is used to visualize and represent design patterns.

Page 11: Engineering  of Software II

Creational Design Patterns

Creational Design Patterns abstract the instantiation processBy abstracting the instantiation process, the system is independent of how its objects are created, composed, and representedCreational Patterns can be implemented by objects, or by class inheritance

Page 12: Engineering  of Software II

Creational Design Patterns

Encapsulates knowledge about which concrete classes a system usesHides how objects are instantiated and how they are composedCreational patterns are closely related, sometimes they complement one and another or are competitors

Page 13: Engineering  of Software II

Behavioral Design Patterns

Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objectsBehavioral patterns describe patterns of objects and classes, but also the communication between themBehavior patterns are implemented using inheritance and object composition

Page 14: Engineering  of Software II

Behavioral Design Patterns

Behavioral patterns use inheritance to distribute behavior between classesBehavioral object patterns use object composition rather than inheritanceSome describe how a group of peer objects cooperate to perform a task that no single object can carry out by itself Other behavioral object patterns are concerned with encapsulating behavior in an object and delegating requests to it

Page 15: Engineering  of Software II

Structural Design Patterns

Structural Patterns are concerned with how classes and objects are composed to form larger structuresStructural class patterns use inheritance to compose classes and implementationsStructural objects describe ways to compose patterns to realize new functionalityStructural patterns are related to some degree

Page 16: Engineering  of Software II

Introducing Design Patterns into the Data Structures Class

Labs – Patterns introduced in laboratory exercises Allow simple hands on experience using patterns Demonstrate the power and usefulness of

patterns

Lectures – Patterns introduced in lecture, and explained by example Allow the introduction of design patterns used

by Java but not used directly in the course

Tests – Students evaluated on pattern knowledge

Page 17: Engineering  of Software II

Introduction by Labs

Lab #

Description Design Patterns

1 Program using an Abstract Data Type Dense List implementation of a List interface

Iterator, Factory method

2 Same program substituting Linked list implementation of a List interface

Iterator, Factory method

3 Program sorting data using a Linked List implementation of a Priority Queue (n2 sort)

Enumerator, Adapter

4 Program to convert infix equations into prefix form using Stacks and Queues (various implementations)

Singleton

5 Program sorting data using the Heap Sort with a Dense List tree. nLog2n sort

Decorator

6 Program to insert, search, traverse and delete a binary tree using nodes with links

Visitor

Page 18: Engineering  of Software II

Lab 1 – Dense List

OBJECTIVE: To introduce the Abstract Data Type (ADT) To introduce the class implementation of a ADT To reinforce simple Java Applications, interfaces, and problem domain classes To reinforce inheritance and composition To introduce design patterns: Factory Method (Creational) and Iterator (Behavioral)

Page 19: Engineering  of Software II

Lab 1 – Dense List

Page 20: Engineering  of Software II

Lab 2 – Link List

OBJECTIVE: To demonstrate the Abstract Data Type (ADT) implementation independence To introduce the dynamic linked list implementation of a basic data structure To introduce the pointer concept To reinforce simple Java Applications To reinforce inheritance, polymorphism, and composition To reinforce design patterns: Factory Method and Iterator

Page 21: Engineering  of Software II

Lab 2 – Link List

Page 22: Engineering  of Software II

Lab 3 – Priority Queue

OBJECTIVE: To introduce a sorting technique (priority queue sorting) To introduce the measurement of algorithm performance To reinforce the Abstract Data Type (ADT) To reinforce the class implementation of a ADT To introduce the Enumeration (Behavioral) and Adapter (Structural) design patterns

Page 23: Engineering  of Software II

Lab 3 – Priority Queue

Page 24: Engineering  of Software II

Lab 4 – Prefix Parser (Stack and Queue implementations)

OBJECTIVE: To introduce the stack and queue ADT. To reinforce the structured approach to programming To reinforce the Abstract Data Type (ADT) To reinforce the class implementation of a ADT To introduce the concept of a Singleton Pattern (Creational) To introduce the concept of a History List

Page 25: Engineering  of Software II

Lab 4 – Prefix Parser (Stack and Queue implementations)

Page 26: Engineering  of Software II

Lab 5 – Iterative Heap Sort

OBJECTIVE: To reinforce the measurement of algorithm performance To introduce a sorting technique (heap sorting) To reinforce the concept of an Iterator pattern (Behavioral) To reinforce the concept of an Factory Method pattern (Creational) To introduce the concept of Decorator pattern (Structural) To reinforce the concept of Code Reuse

Page 27: Engineering  of Software II

Lab 5 – Iterative Heap Sort

Page 28: Engineering  of Software II

Lab 6 – Binary Tree

To introduce a binary tree implementationTo introduce tree operations (insert, search, delete, and list) To introduce basic Audit Trail techniques To introduce the Visitor Pattern (Behavioral)

Page 29: Engineering  of Software II

Example Sample Code

The Factory Method Patternpublic AbstractIterator getIterator(){

return new SomeIterator(listADT);}

The Singleton Patternprivate static HistoryList list;private HistoryList(){ }public static HistoryList getHistoryList(){

return list;}

Page 30: Engineering  of Software II

Introduction by Lecture

Flyweight Pattern - Use sharing to support large numbers of fine-grained objects efficiently Immutable - To create an object whose state does not change after creation Bridge Pattern - Decouple an abstraction of an interface from its implementation so that the two can vary independently

Page 31: Engineering  of Software II

Introduction by Lecture

Example Lecture (Immutable and Flyweight):

String a = “hello”;String b = “hello”;String c = new String(“hello”);If(a==b)

System.out.println(“1 equal?”);

If(a==c) System.out.println(“2 equal?”);

If(a.equals(b)) System.out.println(“3 equal?”);

hello

hello

Output:1 equal3 equal

Page 32: Engineering  of Software II

Design Pattern Solutions

Consider using data structure/design pattern proven implementationsFind problems matching patterns and mold an exercise around the problemIntroduce design patterns as an integral part of course, but emphasize their involvement as complementary, rather than supplementaryUse the tests to evaluate whether students are gaining the design pattern knowledge

Page 33: Engineering  of Software II

Benefits to Learning Design Patterns

Design patterns make it easier to reuse successful designsDesign patterns provide a common design vocabularyDesign patterns provide a foundation for the documentation of software artifactsDesign patterns provide students with a stronger understanding of APIsDesign patterns provide an abstraction transcending the analysis, design, and implementation phases

Page 34: Engineering  of Software II

ReferencesIS 2002, Model Curriculum and Guidelines for Undergraduate Degree Programs in Information Systems, Gorgone, Davis, Valacich, Topi, Feinstein and Longenecker, http://www.acm.org/education/is2002.pdf.Java BluePrints, Model-View-Controller, http://java.sun.com/blueprints/patterns/MVC-detailed.html.Preiss, B. R., “Design Patterns for the Data Structures and Algorithms Course,” 1999, pages 95-99.Nguyen, D., “Design Patterns for Data Structures,” 1998, pages 336-340.Proulx, V. K., “Programming Patterns and Design Patterns in the Introductory Computer Science Course,” 2000, pages 80-84. Astrachan, O., G. Berry, L. Cox, and G. Mitchener, “Design Patterns: An Essentail Component of Computer Science Curricula,” 1998, pages 153-160.Ghafarian, A., “Teaching Design Effectively in the Introductory Programming Courses,” 2001, pages 203-210. Gelfand, N., M. T. Goddrich, and R. Tamassia, “Teaching Data Structure Design Patterns,” 1998, pages 331-335. Clancy, M. J., M. C. Linn, “Patterns and Pedagogy,” 1999, pages 37-42.Stelting, S., O. Maassen, “Applied Java Patterns,” Sun Microsystems Press: A Pretence Hall Title, Palto Alto, CA, 2002.http://gee.cs.oswego.edu/dl/ca/ca/ca.html [Alexander93] Christopher Alexander:An Introduction for Object-Oriented Designers Doug LeaSUNY Oswego / NY CASE Center.