Software Design

128 downloads 161 Views 294KB Size Report
“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 y y p p , please read ...
Software Design “There 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) CSE 403, Spring 2008, Alverson

Readings g y

“Code Complete (2nd ed.)”, chapter 5, pg. 87-105 by Steve McConnell (link on Calendar) Summaryy is onlyy on a p portion of the chapter, p , but please read the whole chapter. It’s good!

CSE 403, Spring 2008, Alverson

Where does design g fit? Requirements q

Architecture – high level design Low level design

Code CSE 403, Spring 2008, Alverson

Architecture is design g y

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

y

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

y

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

CSE 403, Spring 2008, Alverson

Good design g quotes q 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 cases. Pragmatic Programmer

CSE 403, Spring 2008, Alverson

What do we want in a design? g Top 7 desirable characteristics: 1. 2 2. 3. 4. 5. 6. 7.

Minimal complexity Loose coupling Strong cohesion Extensibility Reusability Maintainability L Leanness

1.

CSE 403, Spring 2008, Alverson

Code Complete

Loose coupling p g y

coupling p g assesses the kind and q quantity y of interconnections among modules

y

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

y

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

CSE 403, Spring 2008, Alverson

Pragmatic g Programmer g agrees g

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

CSE 403, Spring 2008, Alverson

Tightly g y or loosely y coupled? p -End21 *

-End1 End1 -End2 End2

-End3

-End6 * *

* *

* * -End11

* *

-End16 *

-End25 *

-End26

*

-End23 -End24 End24

*

-End19

-End4 -End5

*

*

-End13 *

-End9

*

-End7

*

-End12 *

-End14

*

-End8 *

-End10

*

-End15

*

* -End18 -End17 * *

CSE 403, Spring 2008, Alverson

-End20

-End22

Tightly g y or loosely y coupled? p

CSE 403, Spring 2008, Alverson

Tightly g y or loosely y coupled? p

CSE 403, Spring 2008, Alverson

Strong g cohesion y

cohesion refers to how closely the operations in a module are related

y

Tight relationships improve clarity and understanding

y

Classes with good abstraction usually have strong cohension

y

No schizophrenic classes!

CSE 403, Spring 2008, Alverson

Strong g or weak cohesion? class Employee { public: … FullName GetName() const; Address GetAddress() const; Ph PhoneNumber N b G GetWorkPhone() tW kPh () const; t … bool IsJobClassificationValid(JobClassification jobClass); p ((Address address); ); bool IsZipCodeValid bool IsPhoneNumberValid (PhoneNumber phoneNumber); … SqlQuery GetQueryToCreateNewEmployee() const; SqlQuery GetQueryToModifyEmployee() const; SqlQuery GetQueryToRetrieveEmployee() const; … } CSE 403, Spring 2008, Alverson

How do we attack the design process? Treat design as a wicked wicked, sloppy 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

CSE 403, Spring 2008, Alverson

Heuristics help p provide p ideas y y

There is no “right answer” with design Applying effective heuristics can provide insights and lead to a good design

y

C d C Code Complete l t Ch Chapter t 5 llooks k att 9 h heuristics: i ti

1. Identify objects 2. Form consistent abstractions 3. Encapsulate implementation details 4. Favor composition over inheritance 5. Hide information 6. Keep coupling loose and cohesion strong 7. Identify areas likely to change * in your project 8. Use design g p patterns 9. Consider testability CSE 403, Spring 2008, Alverson 10. Other common principles

Inheritance inheritance promotes abstraction and reuse but breaks encapsulation l ti

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

Person

Kid CSE 403, Spring 2008, Alverson

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

Kid PickyEatingPattern

CSE 403, Spring 2008, Alverson

Class Kid { Cl private: EatingPattern *ep; } Kid::Kid() { ep = new PickyEatingPattern; }

Favor a o co composition pos t o over o e inheritance e ta ce Kid KidEatingPattern Kid Kid::Kid() { ep = new PickyEatingPattern() } PickyEatingPatternKid kid = new PickyEatingPatternKid; CSE 403, Spring 2008, Alverson

Composition p y y y y y

Fosters reuse Keeps class libraries shallow (easier to understand) Keeps encapsulation strong Decouples concepts Enables runtime binding Dog PickyEatingPattern

CSE 403, Spring 2008, Alverson

Design g patterns p A 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 collection.

o

Example: Factory pattern Defines an interface that declares methods for letting a class defer instantiation to its subclass

CSE 403, Spring 2008, Alverson

Recent history y of p patterns y

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

y

1995 book Design Patterns: El Elements t off Reusable R bl Object-Oriented Obj t O i t d Software is a classic of the field

CSE 403, Spring 2008, Alverson

More about patterns p y

A pattern describes a recurring software structure: o o o o

y

is abstract from concrete design elements identifies classes, their collaborations and responsibilities li t iimplementation lists l t ti ttrade-offs d ff are not code or designs; are “templates” to be instantiated

The software engineer is required to: o o o

evaluate trade-offs of using a pattern make design and implementation decision how best to apply the pattern, perhaps modify it slightly implement the pattern in code

CSE 403, Spring 2008, Alverson

Can y you think of benefits? y

Benefits of using patterns (from Sp08): o o o o o

common vocabulary allows new developers to come up to speed faster use the results of experience in the field provides a starting point for coding capture good design principles, allowing extensibility, and all that g good stuff

CSE 403, Spring 2008, Alverson

Great website on patterns p

http://sourcemaking.com/design_patterns • complete with code samples

CSE 403, Spring 2008, Alverson

Gang g of Four (GoF) ( ) patterns p y

Creational Patterns (abstract object instantiation) Ab t t Factory, Abstract F t F t Factory, B ild P Builder, Prototype t t Singleton

y

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

y

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

CSE 403, Spring 2008, Alverson

403 Mid-Quarter Q Assessment y

Lead by Jim Borgford-Parnell, Ph.D. Assistant Director Center for Engineering Learning and Teaching

CSE 403, Spring 2008, Alverson