Software Architecture and Design CS 406 Software Engineering

  • Slides: 38
Download presentation
Software Architecture and Design CS 406 Software Engineering I Fall 2001 Aditya P. Mathur

Software Architecture and Design CS 406 Software Engineering I Fall 2001 Aditya P. Mathur Purdue University Last update: September 25, 2001 September 20, 2001 Software Architecture and Design

Organization Part I: III: IV: September 20, 2001 Design principles Design Patterns-I OO Design

Organization Part I: III: IV: September 20, 2001 Design principles Design Patterns-I OO Design Patterns-II Software Architecture and Design 2

Learning Objectives z To understand the basic principles of and techniques for software architecture

Learning Objectives z To understand the basic principles of and techniques for software architecture and design. September 20, 2001 Software Architecture and Design 3

What is Architecture? z Organization of a software system y Components within the system.

What is Architecture? z Organization of a software system y Components within the system. y Interfaces of the components and their interconnection. y Collaborations amongst components. z Involves the identification of those functional and nonfunctional requirements that have a significant impact on the system design. Examples: y y Performance Cost Maintainability Points of evolution September 20, 2001 Software Architecture and Design 4

What is Architecture? z Involves the identification of those functional and non-functional requirements that

What is Architecture? z Involves the identification of those functional and non-functional requirements that have a significant impact on the system design. Examples: y y Performance Cost Maintainability Points of evolution September 20, 2001 Software Architecture and Design 5

Architecture (2) z. Analogy: Explain the architecture of: y. Your favorite Concerto y. Your

Architecture (2) z. Analogy: Explain the architecture of: y. Your favorite Concerto y. Your favorite play y. Your favorite home September 20, 2001 Software Architecture and Design 6

Architecture (3) z. Software components-OO View: y. Application x. A subsystem • An object

Architecture (3) z. Software components-OO View: y. Application x. A subsystem • An object – A method – A data item September 20, 2001 Software Architecture and Design 7

Architecture (4) z. Software components-Functional View: y. Application x. A Subsystem • A function

Architecture (4) z. Software components-Functional View: y. Application x. A Subsystem • A function • A data item z. Do you understand the differences between a class, an object, and a function? Between a function and a method? September 20, 2001 Software Architecture and Design 8

What is Design? z. As a process: The process by which the architecture of

What is Design? z. As a process: The process by which the architecture of a software system is established. z. As an artifact: The architecture of a software system describing system components and their interactions. September 20, 2001 Software Architecture and Design 9

Analysis and Design z. Analysis: Focus on…. y. Understanding, refining, and specifying requirements. y“What”

Analysis and Design z. Analysis: Focus on…. y. Understanding, refining, and specifying requirements. y“What” are domain processes, concepts, terms, operations? z. Design: Focus on…. y“How” can the requirements be met? September 20, 2001 Software Architecture and Design 10

Design Phase Refine Plan Sync Analyze Design Construct Artifacts September 20, 2001 Software Architecture

Design Phase Refine Plan Sync Analyze Design Construct Artifacts September 20, 2001 Software Architecture and Design Test 11

Design Principles [1] z. Design for change Change of algorithms Change of data representation

Design Principles [1] z. Design for change Change of algorithms Change of data representation Change of peripheral devices Change of underlying abstract machine Change of social environment September 20, 2001 Software Architecture and Design 12

Design Principles [2] z Separation of concerns Separation over time. Separation of quality attributes:

Design Principles [2] z Separation of concerns Separation over time. Separation of quality attributes: e. g. performance and correctness Separation by views: e. g. data and control flows. Separation by parts of the system. September 20, 2001 Software Architecture and Design 13

Coupling (1) y. Measure of interconnectedness. y. Class C 1 is coupled to class

Coupling (1) y. Measure of interconnectedness. y. Class C 1 is coupled to class C 2 if C 1 requires C 2 directly or indirectly. y. Coupling measures the strength of dependence between classes and packages. September 20, 2001 Software Architecture and Design 14

Coupling (2) High coupling September 20, 2001 Low coupling Software Architecture and Design 15

Coupling (2) High coupling September 20, 2001 Low coupling Software Architecture and Design 15

Coupling (3) z. Design with low coupling y. A class that depends on 2

Coupling (3) z. Design with low coupling y. A class that depends on 2 other classes has a lower coupling than a class that depends on 8 other classes. y. Why low coupling? . . . Leads to easier changes, easier understanding, and easier re-use. September 20, 2001 Software Architecture and Design 16

Factors effecting coupling September 20, 2001 Software Architecture and Design 17

Factors effecting coupling September 20, 2001 Software Architecture and Design 17

Cohesion (1) z. Cohesion measures the strength of the relationship amongst elements of a

Cohesion (1) z. Cohesion measures the strength of the relationship amongst elements of a class. z. When methods within a class use a “similar” set of instance variables, the class is considered highly cohesive. September 20, 2001 Software Architecture and Design 18

Cohesion (2) z. High cohesion and low coupling is preferred. (Example: Electric subsystem of

Cohesion (2) z. High cohesion and low coupling is preferred. (Example: Electric subsystem of a home. ) z. All operations and data within a class should naturally “belong” to the concept the class models. z. Classes with high cohesion can often be described by a simple sentence. September 20, 2001 Software Architecture and Design 19

Types of cohesion z Coincidental : No meaningful relationship amongst elements of a class.

Types of cohesion z Coincidental : No meaningful relationship amongst elements of a class. May occur when a program is “modularized” by chopping it into pieces and making each piece a module. z Logical cohesion : Elements of a class perform one kind of a logical function, e. g. interfacing with the Point of Sale terminal hardware. z Temporal cohesion : All elements of a class are executed “together”. y. Was NATO cohesive during its operations in the 90’s? September 20, 2001 Software Architecture and Design 20

Design Patterns [1] z A solution to a problem that occurs repeatedly in a

Design Patterns [1] z A solution to a problem that occurs repeatedly in a variety of contexts. z Each pattern has a name. z Use of each pattern has consequences. September 20, 2001 Software Architecture and Design 21

Design Patterns [2] z Generally at a “higher level” of abstraction. z Not about

Design Patterns [2] z Generally at a “higher level” of abstraction. z Not about designs such as linked lists or hash tables. z Generally descriptions of communicating objects and classes. September 20, 2001 Software Architecture and Design 22

Observer Pattern [1] z Need to separate presentational aspects with the data, i. e.

Observer Pattern [1] z Need to separate presentational aspects with the data, i. e. separate views and data. z Classes defining application data and presentation can be reused. z Change in one view automatically reflected in other views. Also, change in the application data is reflected in all views. z Defines one-to-many dependency amongst objects so that when one object changes its state, all its dependents are notified. September 20, 2001 Software Architecture and Design 23

Observer Pattern [2] Relative Percentages A B C D X 15 35 35 15

Observer Pattern [2] Relative Percentages A B C D X 15 35 35 15 Y 10 40 30 20 Z 10 40 30 20 Change notification Requests, modifications September 20, 2001 A D B C A B C D A=10% B=40% C=30% D=20% Software Architecture and Design Application data 24

Observer Pattern [3] observers Subject Observer Update() attach (Observer) detach (Observer) For all x

Observer Pattern [3] observers Subject Observer Update() attach (Observer) detach (Observer) For all x in observers{ x Update(); } Notify () Concrete Observer Concrete Subject subject observer. State Get. State() Set. State() observer. State= subject get. State(); subject. State September 20, 2001 Update() Software Architecture and Design 25

Class collaboration in Observer : Concrete. Subject : Concrete. Observer-1 : Concrete. Observer-2 Set.

Class collaboration in Observer : Concrete. Subject : Concrete. Observer-1 : Concrete. Observer-2 Set. State() Notify() Update() Get. State() September 20, 2001 Software Architecture and Design 26

Observer Pattern: Observer code class Subject; class observer { public: Abstract class defining the

Observer Pattern: Observer code class Subject; class observer { public: Abstract class defining the Observer interface. virtual ~observer; virtual void Update (Subject* the. Changed. Subject)=0; protected: observer (); }; Note the support for multiple subjects. September 20, 2001 Software Architecture and Design 27

Observer Pattern: Subject Code [1] class Subject { public: Abstract class defining the Subject

Observer Pattern: Subject Code [1] class Subject { public: Abstract class defining the Subject interface. virtual ~Subject; virtual void Attach (observer*); virtual void Detach (observer*) ; virtual void Notify(); protected: Subject (); private: List <Observer*> *_observers; }; September 20, 2001 Software Architecture and Design 28

Observer Pattern: Subject Code [2] void Subject : : Attach (Observer* o){ } _observers

Observer Pattern: Subject Code [2] void Subject : : Attach (Observer* o){ } _observers -> Append(o); void Subject : : Detach (Observer* o){ _observers -> Remove(o); } void Subject : : Notify (){ List. Iterator<Observer*> iter(_observers); for ( iter. First(); !iter. Is. Done(); iter. Next()) { iter. Current. Item() -> Update(this); } } September 20, 2001 Software Architecture and Design 29

Observer Pattern: A Concrete Subject [1] class Clock. Timer : public Subject { public:

Observer Pattern: A Concrete Subject [1] class Clock. Timer : public Subject { public: Clock. Timer(); virtual int Get. Hour(); virtual int Get. Minutes(); virtual int Get. Second(); void Tick (); } September 20, 2001 Software Architecture and Design 30

Observer Pattern: A Concrete Subject [2] Clock. Timer : : Tick { // Update

Observer Pattern: A Concrete Subject [2] Clock. Timer : : Tick { // Update internal time keeping state. // gets called on regular intervals by an internal timer. Notify(); } September 20, 2001 Software Architecture and Design 31

Observer Pattern: A Concrete Observer [1] class Digital. Clock: public Widget, public Observer {

Observer Pattern: A Concrete Observer [1] class Digital. Clock: public Widget, public Observer { public: Digital. Clock(Clock. Timer*); virtual ~Digital. Clock(); virtual void Update(Subject*); virtual void Draw(); Override Observer operation. Override Widget operation. private: Clock. Timer* _subject; } September 20, 2001 Software Architecture and Design 32

Observer Pattern: A Concrete Observer [2] Digital. Clock : : Digital. Clock (Clock. Timer*

Observer Pattern: A Concrete Observer [2] Digital. Clock : : Digital. Clock (Clock. Timer* s) { _subject = s; _subject Attach(this); } Digital. Clock : : ~Digital. Clock() { _subject->Detach(this); } September 20, 2001 Software Architecture and Design 33

Observer Pattern: A Concrete Observer [3] void Digital. Clock : : Update (subject* the.

Observer Pattern: A Concrete Observer [3] void Digital. Clock : : Update (subject* the. Changed. Subject ) { If (the. Changed. Subject == _subject) { Draw(); } } Check if this is the clock’s subject. void Digital. Clock : : Draw () { int hour = _subject->Get. Hour(); int minute = _subject->Ge. Minute(); // etc. // Code for drawing the digital clock. } September 20, 2001 Software Architecture and Design 34

Observer Pattern: Main (skeleton) Clock. Timer* timer = new Clock. Timer; Digital. Clock* digital.

Observer Pattern: Main (skeleton) Clock. Timer* timer = new Clock. Timer; Digital. Clock* digital. Clock = new Digital. Clock (timer); September 20, 2001 Software Architecture and Design 35

When to use the Observer Pattern? z When an abstraction has two aspects: one

When to use the Observer Pattern? z When an abstraction has two aspects: one dependent on the other. Encapsulating these aspects in separate objects allows one to vary and reuse them independently. z When a change to one object requires changing others and the number of objects to be changed is not known. z When an object should be able to notify others without knowing who they are. Avoid tight coupling between objects. September 20, 2001 Software Architecture and Design 36

Observer Pattern: Consequences z Abstract coupling between subject and observer. Subject has no knowledge

Observer Pattern: Consequences z Abstract coupling between subject and observer. Subject has no knowledge of concrete observer classes. (What design principle is used? ) z Support for broadcast communication. A subject need not specify the receivers; all interested objects receive the notification. z Unexpected updates: Observers need not be concerned about when then updates are to occur. They are not concerned about each other’s presence. In some cases this may lead to unwanted updates. September 20, 2001 Software Architecture and Design 37

Summary 1. What is design? 2. What is architecture? 3. Where does design fit

Summary 1. What is design? 2. What is architecture? 3. Where does design fit in a development process? 4. What are the general principles of design ? Reference for the Observer Pattern: Design patterns by Gamma et al. , 1995, Chapter 5, Or read pp. 372 -380 from Larman’s book or section 3. 2. 2. 1 of Braude’s book. September 20, 2001 Software Architecture and Design 38