design dan fleck cs 421 george mason university. what is the design phase? analysis phase describes...

33
Design Dan Fleck CS 421 George Mason University

Upload: colin-green

Post on 11-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Design

Dan FleckCS 421

George Mason University

Page 2: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

What is the design phase?

• Analysis phase describes what the system should do

• Analysis has provided a collection of classes and descriptions of the scenarios that the objects will be involved in. These functions are clustered in groups with related behavior.

• The design phase is to work out how the system should do these things. This is the goal of the design phase.

Page 3: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Analysis --> Design

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 4: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Analysis --> Design

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 5: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Analysis --> Design

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 6: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Analysis --> Design

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 7: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Oversimplification

Analysis

ClassesAttributesOperationsRelationshipsBehavior

Design

ObjectsData

StructuresAlgorithmsMessagingControl

Page 8: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

The Design SpecArchitecture Design -

• Layers of the software (e.g.model, view, controller (MVC))• Categories of classes (e.g. UI, Business logic, interfaces)

Component design - • Description of classes/methods/algorithms• State machines for classes

UI design• sample screens• UI guidelines/standards we’re using • detailed description of how UI components work

Data design - • database design • data structures we’re using.

Page 9: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

The Design Spec

But really, how do I do it?

Find examples and use what you think is helpful from them!

http://www.mhhe.com/engcs/compsci/pressman/graphics/Pressman5sepa/common/cs2/design.pdf

http://www.cmcrossroads.com/bradapp/docs/sdd.html

Page 10: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

The goal of design is to think with your brain, not your hands! - Dan Fleck

Page 11: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Applied Design

We know what to do now, but that is just a set of documents..

How do we create a GOOD design?

Page 12: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Good Design

• Design Principles– What should you try to do?

• Design Patterns– How have people done it before you?

• Design Metrics– How do you know you have done it

well?

Page 13: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Single Responsibility Principle

• Each class should have a single overriding responsibility (high cohesion)

• Each class has only one reason for why it should change

Page 14: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Principle of Least Knowledge (aka Law of

Demeter)• “Only talk to your immediate friends”• Object O has a method M.

– M may call other methods in O– M may call methods of any parameter

passed into the M method– M may call methods of any object it

creates– Any object contained in O

Purpose: Reduce Coupling

Page 15: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Principle of Least Knowledge (aka Law of

Demeter)Simplified:• I can play by myself• I can play with toys given to me• I can play toys I made myself • I can play with my own toys (but

not take them apart)

Purpose: Reduce Coupling

Page 16: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Dependency Inversion Principle

• Depend on abstractions, not concretions• Program to interfaces not

implementations• Program to most abstract class possible

• Why? Concrete classes may change a lot. Abstract classes/Interfaces generally change very little.– How can we ensure interfaces change very

little? See next slide!

Page 17: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Interface Segregation Principle

• Don’t make large multipurpose interfaces – instead use several small focused ones.

• Don’t make clients depend on interfaces they don’t use.

• Class should depend on each other through the smallest possible interface.

• Why? When I change something I want to minimize changes for everyone else.

Page 18: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Remove Cyclic Dependencies

• Do not have cyclic dependencies in your packages

• Decomposition into independent modules

• Why?GUI

Logic

UserLogic BusinessLogic

ErrorHandling

Page 19: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Design Patterns • Proven solutions to common problems• Capture design expertise• Aid in meeting quality metrics

• Core patterns are from the “Gang of Four (GoF)”OOPSLA - 1994

Page 20: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Singleton Pattern

• Problem: I want to limit the application to only one instance of a particular class, but need global access to that class.

• Normally used to control access to key resources.

• Solution?

override new, make static accessor method.

Page 21: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Singleton Pattern (in Java)

public class MySingleton {

private static MySingleton instance;

private MySingleton() { // do anything you need to do }

public static MySingleton getInstance() { if (instance == null) instance = new MySingleton(); return instance; }}

Page 22: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Factory (GoF95)• Define an interface for a group of objects• Create a Factory to decide which specific object needs to be

instantiated

• Think of a multi-document application framework. An application object may know when an object needs to be created, but not which object. How do we create the correct object when needed?

• Can also be used when a complex initialization of objects is necessary, for instance when aggregation is heavily used.

• Can also be used to take advantage of memory-optimization like object pools, cached objects, etc.

Page 23: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Factory (GoF95)Encryption

encryptOutdecryptIn

DESEncryption RSAEncryption

Socket

EncryptedSocket

instance:IEncryptFactorycipher: Encryption

<<interface>>IEncryptFactory

CreateEncryption(Key): Encryption

RequestsCreation

EncryptionFactory

CreateEncryption(Key): EncryptionCreates

Encrypts/Decrypts with

Page 24: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Concrete Command

Command (GoF95)• Encapsulate commands in objects, so

we can queue them, undo them or make macros.

Abstract Command

+doIt():bool+undoIt():bool MacroCommand

+doIt():bool+undoIt():bool +doIt():bool

+undoIt():bool

- data

*+ manager:CmdMgr

Page 25: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Flyweight (GoF95)• I have a bunch of classes, but I need to

minimize the number of objects I am using.

• Instances of the objects contain the same information and can be used interchangeably

• Avoid the expense of multiple instances.

• Example: DocChar class used to hold characters in a line of text

Page 26: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Visitor (GoF95)

• If you need to perform an operation in multiple objects in a complex structure you could create the logic in each class.

• OR…the visitor pattern creates a single class that implements the logic and knows how to “visit” each object in your complex structure

Page 27: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Visitor (GoF95)

• I need to apply different operations to a collection of objects.

• I want to centralize these operations • I want to reduce coupling • For example in a word processor,

grammar check, spell check, table of contents builder, outliner all need to traverse the document.

Page 28: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Visitor Diagram

Object withStructure

Individual ElementsVisitor

navigates

concretevisitor

concretevisitor

Page 29: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Design Patterns Summary

• Many design patterns exist• Implementations are usually

available in every language• Use them as guides where

appropriate and make sure you understand the tradeoffs for each one. They aren’t always good for YOUR situation

Page 30: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Design Metrics

• Class Size• Methods per class• Lack of Cohesion (count of methods

with dissimilar purpose)• Coupling Between Classes (count of

other classes that this class refers to)• Depth of Inheritance Tree• Method Complexity - tools can do this

Page 31: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

Design Summary

• The design phase is when you plan HOW you implement your analysis

• Use – Design Principles– Design Patterns– Design Metrics

Page 32: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

What should you know

• Analysis = what the system should do• Design = how it should do it• Meaning of the parts of the design spec• Design Principles:

– Single Responsibility Principle - write it– Law of Demeter. Describe it and state why it is good.– 3 rules of Dependency Inversion Principle– Why you need to remove cyclic dependencies

• Metrics– Definition of cohesion and coupling - not how to calculate

it, but what it means!• Be able to describe patterns - singleton, factory, command

Page 33: Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a

References• Luc Berthouze, University of Sussex,

http://www.informatics.sussex.ac.uk/users/lb203/se/SE08.pdf

• Robert Martin, Principles and Patterns, http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf

• Bob Waters, Georgia Tech, CS2340 Slides, http://www.cc.gatech.edu/classes/AY2007/cs2340_summer/