CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software...

27
CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies; the other is to make it so complicated that there are no obvious deficiencies.” -- C.A.R. Hoare (1985)

Transcript of CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software...

Page 1: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Software Design

“There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies; the other is to make it so complicated that there are no obvious deficiencies.” -- C.A.R. Hoare (1985)

Page 2: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Readings “Code Complete (2nd ed.)”, chapter 5, pg. 87-105

by Steve McConnell (link on Calendar)

Summary is only on a portion of the chapter, but please read the whole chapter. It’s good!

Page 3: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Where does design fit?

Requirements Requirements

Code Code

Architecture – high level design

Low level design

Page 4: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Architecture is design

Architecture, high level design, focuses on system components and their connections

Low level design is a step closer to code. It looks at objects, classes, algorithms, variables, …

The boundary between high and low level design is blurry. Many of the same principles apply!

Page 5: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Good design quotes

The driving force behind design is managing complexity

The goal of all software design techniques is to break a complicated problem into simple pieces

The key to performance is elegance, not battalions of special cases. Pragmatic Programmer

Page 6: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

What do we want in a design?Top 7 desirable characteristics:

1. Minimal complexity2. Loose coupling3. Strong cohesion4. Extensibility5. Reusability6. Maintainability7. Leanness

1. Code Complete

Page 7: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Loose coupling

coupling assesses the kind and quantity of interconnections among modules

Modules that are loosely coupled (or uncoupled) are better than those that are tightly coupled

The more tightly coupled are two modules, the harder it is to work with them separately, and thus the benefits become more limited

Page 8: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Pragmatic Programmer agrees

Eliminate Effects Between Unrelated Things – design components that are self-contained, independent, and have a single, well-defined purpose

Pragmatic Programmer

Page 9: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Tightly or loosely coupled?

User Interface Graphics

Data StorageApplication Level Classes

Business Rules Enterprise Level Tools

-End1

*

-End2

*-End3

*

-End4*

-End5

*

-End6

*

-End7*

-End8*

-End9*

-End10

*

-End11*

-End12*

-End13

*

-End14*

-End15

*

-End16

*

-End17

*

-End18

*

-End19*

-End20*

-End21

*

-End22

*

-End23*-End24*

-End25*

-End26*

Page 10: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Tightly or loosely coupled?User Interface Graphics

Data Storage Application Level Classes

Business Rules Enterprise Level Tools

-End1

*

-End2

*

-End3*

-End4

*

-End5*

-End6*

-End9

*

-End10

*

-End11

*

-End12

*

-End13*

-End14*

-End15*

-End16*

-End7*

-End8 *

Page 11: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Tightly or loosely coupled?

Page 12: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Strong cohesion cohesion refers to how closely the operations in

a module are related

Tight relationships improve clarity and understanding

Classes with good abstraction usually have strong cohension

No schizophrenic classes!

Page 13: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Strong or weak cohesion?class Employee {public: … FullName GetName() const; Address GetAddress() const; PhoneNumber GetWorkPhone() const; … bool IsJobClassificationValid(JobClassification jobClass); bool IsZipCodeValid (Address address); bool IsPhoneNumberValid (PhoneNumber phoneNumber); … SqlQuery GetQueryToCreateNewEmployee() const; SqlQuery GetQueryToModifyEmployee() const; SqlQuery GetQueryToRetrieveEmployee() const; …}

Page 14: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

How do we attack the design process?

Treat design as a wicked, sloppy, heuristic process. Don’t settle for the first design that occurs to you.

Collaborate. Strive for simplicity.

Prototype when you need to. Iterate, iterate, and iterate again.

-- Steve McConnell, Code Complete

Page 15: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Heuristics help provide ideas There is no “right answer” with design Applying effective heuristics can provide

insights and lead to a good design

Code Complete Chapter 5 looks at 9 heuristics:1. Identify objects2. Form consistent abstractions3. Encapsulate implementation details4. Favor composition over inheritance5. Hide information6. Keep coupling loose and cohesion strong7. Identify areas likely to change * in your project8. Use design patterns9. Consider testability10. Other common principles

Page 16: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Inheritance

inheritance promotes abstraction and reuse but breaks encapsulation

The main guiding philosophy is that inheritance should be used only when a subclass is-a superclass.

Kid

Person

Page 17: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Composition

Compose by making a class an instance of another class (instead of a subclass)

PickyEatingPattern

Kid

Class Kid { private: EatingPattern *ep; }

Kid::Kid(){ ep = new PickyEatingPattern;}

Page 18: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Favor composition over inheritance

KidEatingPattern

Kid

PickyEatingPatternKid

Kid

kid = new PickyEatingPatternKid;

Kid::Kid() { ep = new PickyEatingPattern()}

Page 19: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Composition Fosters reuse Keeps class libraries shallow (easier to

understand) Keeps encapsulation strong Decouples concepts Enables runtime binding

PickyEatingPattern

Dog

Page 20: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Design patternsA design pattern is a repeatable solution to a

common software design problem

o example: Iterator pattern

Defines an interface that declares methods for sequentially accessing the objects in a collection.

o Example: Factory pattern

Defines an interface that declares methods for letting a class defer instantiation to its subclass

Page 21: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Recent history of patterns

In 1990 a group called the Gang of Four (Gamma, Helm, Johnson, Vlissides) compiled a catalog of design patterns

1995 book Design Patterns: Elements of Reusable Object-Oriented Software is a classic of the field

Page 22: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

More about patterns

A pattern describes a recurring software structure:o is abstract from concrete design elements o identifies classes, their collaborations and responsibilitieso lists implementation trade-offso are not code or designs; are “templates” to be instantiated

The software engineer is required to:o evaluate trade-offs of using a patterno make design and implementation decision how best to

apply the pattern, perhaps modify it slightlyo implement the pattern in code

Page 23: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Can you think of benefits? Benefits of using patterns (from Sp08):

o common vocabularyo allows new developers to come up to speed fastero use the results of experience in the fieldo provides a starting point for codingo capture good design principles, allowing extensibility,

and all that good stuff

Page 24: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

Great website on patterns

CSE 403, Spring 2008, Alverson

http://sourcemaking.com/design_patterns

• complete with code samples

http://sourcemaking.com/design_patterns

• complete with code samples

Page 25: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

CSE 403, Spring 2008, Alverson

Gang of Four (GoF) patterns Creational Patterns (abstract object instantiation)

Abstract Factory, Factory, Builder, Prototype Singleton

Structural Patterns (combine objects)Adapter, Bridge, Composite, Decorator, Façade, Flyweight, Proxy

Behavioral Patterns (communication btwn objects)Chain of responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor

Page 26: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

403 Mid-Quarter Assessment Lead by

Jim Borgford-Parnell, Ph.D.Assistant DirectorCenter for Engineering Learning and Teaching

CSE 403, Spring 2008, Alverson

Page 27: CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.

Sp07 Final From a design perspective, would it be

appropriate to implement a class House via inheritance from class Building in order to reuse

the implementation of the many useful methodsalready defined in Building even if Building has other methods that have no meaning in the context of House?

CSE 403, Spring 2008, Alverson