CMSC 202 Design Basics Coupling Version 910 Coupling
CMSC 202 Design Basics: Coupling Version 9/10
Coupling � The degree to which software modules rely upon one another. � If module A is coupled to module B, module A has a dependency on module B. � For example, if class A below has an instance variable of class type B (composition), public class A { private B b; . . . �A is coupled to B, �A depends on B, but } �B is not coupled to A and �B does not depend on A. Class A 2 Class B Version 9/10
Coupling (con’t) �Coupling strength is based on the �quantity of module coupling points, and �the complexity of the coupling points. A A A B B C D E Low coupling 3 B C F D E C F Higher coupling D E F Very high coupling For each example above, what are the concerns when the module C must be modified or replaced? Version 9/10
The Low Coupling Situation A B C D E F Low coupling C must be aware of: • Which of its methods are used by class A • What it needs from classes D, E, and F (instance variables and methods) 4 This is a “has a” public class A { dependency (i. e. . composition) private B b. Var = new B( ); private C c. Var = new C( ); // instance variable of type C. . . private void a. Method 1( ) {. . . int x = c. Var. c. Method 1( ); // call to a method in class C. . . } // other methods which may call methods in class C } public class C {. . . private D d. Var = new D( ); // instance variable of type D private E e. Var = new E( ); // instance variable of type E. . . public int c. Method 1( ) { // called from class A This is a “uses a” F f. Var = new F( ); dependency. . . // code that uses f. Var } // other methods that are called by methods in class A // other methods that call methods in classes D and E } Version 9/10
The Very High Coupling Situation It’s now much more complex to make modifications to class C. A B C D 5 E C must be aware of: • Which of its methods are used by classes A, B, and D F • What it needs from classes B, D, E, and F (instance variables and methods) Version 9/10
Coupling (con’t) �Want weak (loose) coupling �Cannot have zero coupling, so our goals are to �minimize coupling, �weaken (loosen) coupling, and �most importantly, to control coupling. �Every coupling point is intentional. �Every coupling point has a well-defined interface. 6 Version 9/10
So, What’s a Module? �Any “chunk” of a software system or program �function �method �class �cluster of functions �cluster of classes �other … �Coupling must be controlled at every level of a software system or program. 7 Version 9/10
Project 1 Design Program Stub Project 1 Class Cash. Register Class Test Script (Driver) Code that tests Project 1 class functionality and error conditions 8 Driver Stub: All code necessary to emulate the Cash. Register class interface Driver: All code necessary to fully exercise the Cash. Register class (can be a separate class or Cash. Register’s main( ) Version 9/10
Sample Cash. Register Driver (Partial) public static void main( ) { // Testing constructor and to. String() System. out. println("***Testing constructor() and to. String()***"); Cash. Register cr 1 = new Cash. Register(); System. out. println(cr 1. to. String()); System. out. println(); // Testing unlock() and lock() 9 System. out. println("***Testing lock() and unlock()***"); Cash. Register cr 2 = new Cash. Register(); System. out. println(cr 2. to. String()); // register is locked System. out. println(); cr 2. unlock(); System. out. println(cr 2. to. String()); // register is now unlocked System. out. println(); cr 2. lock(); System. out. println(cr 2. to. String()); // register is locked again System. out. println(); Version 9/10
Sample Cash. Register Driver (Partial, con’t) // Testing remove. Money() System. out. println("***Testing remove. Money()***"); Cash. Register cr 5 = new Cash. Register(); System. out. println(cr 5. to. String()); // register is locked cr 5. unlock(); // unlock register System. out. println(cr 5. to. String()); // register is unlocked System. out. println("Success = " + cr 5. add. Money(5, 6, 7, 8)); // add money System. out. println(cr 5. to. String()); // success System. out. println("Success = " + cr 5. remove. Money(-1, 1, 1, 1)); // negative $ System. out. println(cr 5. to. String()); // fail System. out. println("Success = " + cr 5. remove. Money(4, 3, 2, 1)); // good $ System. out. println(cr 5. to. String()); // success System. out. println("Success = " + cr 5. remove. Money(10, 1, 1, 1)); // too much $ System. out. println(cr 5. to. String()); // fail cr 5. lock(); // lock register System. out. println("Success = " + cr 5. remove. Money(1, 1, 1, 1)); // locked System. out. println(cr 5. to. String()); // fail System. out. println(); } // end main( ) 10 Version 9/10
Sample Project 1 Stub (Partial) package proj 1; public class Cash. Register {. . . public boolean add. Money(int twenties, int tens, int fives, int ones) { System. out. println(“add. Money called”); System. out. println(“twenties = “ + twenties); System. out. println(“tens = “ + tens); return (true); } public boolean remove. Money(int twenties, int tens, int fives, int ones) { System. out. println(“remove. Money called”); System. out. println(“twenties = “ + twenties); System. out. println(“tens = “ + tens); return (true); } // other stub methods } 11 Version 9/10
A More Complex System Design System Stub Application UI A B C One or more classes D E F One or more classes Driver 12 Stub: all code necessary to emulate the application interface Driver: all code necessary to fully exercise the application Version 9/10
An Example from Later This Semester … Game You write this Application UI Controller Class (e. g. Poker) Model Classes Deck Card We write this other classes? Hand 13 So, in this situation, who writes the. Version 9/10 application interface?
Example 1: Evaluating Coupling A Method public void some. Method(int flag) {. . . if (flag == 1). . . else if (flag == 2). . . else if (flag == 3). . . else. . . } 14 • This code is very strongly coupled to any other internal or external code that calls it. • Yes, there is only one coupling point between the caller and some. Method( ). But it is very strong (complex). • The calling code must be aware of the meaning of all flag values. • some. Method must be careful if it adds flag values, deletes flag values, or changes the meaning of any flag values. Relative to coupling, what’s a better way to implement this code? Version 9/10
Example 1: A Better Solution Replacement Methods public void some. Method 1( ) { // method to call if (flag == 1) } public void some. Method 2( ) { // method to call if (flag == 2) }. . . public void some. Method. N( ) { // method to call if (flag == “N”) • Each of these methods is more cohesive than the method on the previous slide. They each perform one specific task. • Yes, the caller needs to know which method to call. But a change to one of these methods will not affect any of the other methods. • If new functionality is needed by the caller, a new corresponding method is written. The calling code then simply tests for the newly added flag value and calls the new method. } • The number of coupling points has increased, but each is weak (loose). 15 Version 9/10
Example 2: Evaluating Coupling • Once again, this code is very A Method public int another. Method( ) {. . . if (condition 1) {. . . return (1); } else if (condition 2) {. . . return (2); } else if (condition 3) {. . . return (3); } else. . . strongly coupled to any other internal or external code that calls it. • Again, the coupling point between the caller and another. Method( ) is very strong (complex). • The calling code must be aware of the meaning of all return values. • another. Method( ) must be careful if it adds flag conditions, deletes flag conditions, or changes the meaning of any flag conditions. . } 16 Relative to coupling, what’s a better way to implement this Version 9/10 code?
Example 2: A Better Solution �This one is not as easy to get around. �It’s common for predefined library functions/methods to return flags that may have many values. �If it’s possible to compute the condition to be tested outside of another. Method( ), then break up another. Method( ) into separate methods. �If the condition to be tested must be computed inside of another. Method( ), you just need to deal carefully with the coupling. 17 Version 9/10
Example 3: Evaluating Coupling Problem: Create a program to track students registered for all sections of a course. UI (main) Course Better: course. get. Student. GPA(student. Id) X Section X Student But… more coupling! X X Transcipt section. get. Student. GPA(student. Id) student. get. Transcript() transcript. get. GPA() Date Suppose we want the GPA for a given student. From the UI class, we could: X X X course. get. Section(section. Id). get. Student(student. Id). get. Transcript(). get. GPA() 18 Version 9/10
- Slides: 18