Docsity

Software Design Principles: Coupling, Cohesion, and Interfaces in Software Engineering, Slides of Software Engineering

Partial preview of the text

Download Software Design Principles: Coupling, Cohesion, and Interfaces in Software Engineering and more Slides Software Engineering in PDF only on Docsity!

Software Design

๏‚— In this lecture we shall again be looking at how to

derive a software solution which satisfies the

software requirements.

๏‚— We shall be examining ways in which architecture

designs and individual components can be said to

be โ€œgoodโ€ in practice.

What makes a Good System? ๏‚— We may remark that a good system has the following properties: ๏‚— Useful and usable; ๏‚— Reliable; ๏‚— Flexible; ๏‚— Affordable; ๏‚— Available. ๏‚— During this lecture we will be considering design properties of modules. ๏‚— Question : How do modules allow us to meet some or all of the above goals of a good system?

Interfaces โ€“ an Example

  • COMP201 - Software Engineering โ€ข 4 // A C++ Vector class to perform mathematical calculations // All Vector arguments should be normalized class Vector { public: Vector(float x, float y, float z); ~Vector(); float dotProduct(Vector inVector); Vector crossProduct(Vector inVector); void setVector(float x, float y, float z); private: void normalize(); float x_value; float y_value; float z_value; }; Private interface Public interface docsity.com

Interfaces โ€“ an Example

  • COMP201 - Software Engineering โ€ข 5
  • On the previous slide, do you know how to compute the cross product of two vectors?
  • The point is that due to abstraction, you do not need to know, that information is hidden in the module!
  • We may choose to replace the code for computing the cross product to be faster or more numerically stable for example, but users of the module (or component) should not need to change their code since the interface has not changed. docsity.com

Linguistic Modular Units ๏‚— A programming language (or design language) should support the principle of linguistic modular units: ๏‚— Modules must correspond to linguistic units in the language used ๏‚— EXAMPLE: Java methods and classes ๏‚— COUNTER EXAMPLE: Subroutines in BASIC are called by giving a line number where execution is to proceed from; there is no way of telling, just by looking at a section of code, that it is a subroutine.

Few Interfaces ๏‚— This principle states that the overall number of communication channels between modules should be as small as possible: ๏‚— Every module should communicate with as few others as possible. ๏‚— So, in the system with n modules, there may be a minimum of n- 1 and a maximum of links; your system should stay closer to the minimum. 2 n ( n ๏€ญ 1 )

Small Interfaces (Loose Coupling) ๏‚— Loose coupling implies that : ๏‚— If any two modules communicate, they should exchange as little information as possible.

๏‚— An interface to a module defines the features on which

a client may rely and the rest of the system can only use

the module as permitted by the interface.

๏‚— COUNTER EXAMPLE. Declaring all instance variables as public!

Coupling ๏‚— Coupling is a measure of the strength of the inter- connections between system components. ๏‚— Loose coupling means component changes are unlikely to affect other components. ๏‚— Shared variables or control information exchange lead to tight coupling (usually bad). ๏‚— Loose coupling can be achieved by state decentralization (as in objects) and component communication via parameters or message passing.

Loose Coupling Module A Aโ€™s data Module B Bโ€™s data Module D Dโ€™s data Module C Cโ€™s data

  • Object-oriented systems are loosely coupled because there is no shared state and objects communicate using message passing.
  • However , an object class is coupled to its super- classes. Changes made to the attributes or operations in a super-class propagate to all sub-classes. Coupling and Inheritance

Stepwise Refinement ๏‚— The simplest realistic design method, widely used in practice. ๏‚— Not appropriate for large-scale, distributed systems: mainly applicable to the design of methods. ๏‚— Basic idea is: ๏‚— Start with a high-level spec of what a method is to achieve; ๏‚— Break this down into a small number of problems (usually no more than 10) ๏‚— For each of these problems do the same; ๏‚— Repeat until the sub-problems may be solved immediately.

Explicit Interfaces ๏‚— If two modules must communicate, they should do it so that we can see it: ๏‚— If modules A and B communicate, this must be obvious from the documentation of A or B or both. ๏‚— Why? If we change a module, we need to see what other modules may be affected by these changes. A traceability matrix can be used for this purpose.

Cohesion Cohesion - A measure of how well a component โ€œfits togetherโ€ ๏‚— A component should implement a single logical entity or function. ๏‚— Cohesion is a desirable design component attribute as when a change has to be made, it is localized in a single cohesive component. ๏‚— Various levels of cohesion have been identified.

Cohesion Levels ๏‚— Coincidental cohesion (weak) ๏‚— Parts of a component are simply bundled together. ๏‚— Logical association (weak) ๏‚— Components which perform similar functions are grouped. ๏‚— Temporal cohesion (weak) ๏‚— Components which are activated at the same time are grouped.