80 likes | 532 Views
Coupling Cohesion. Chandan R. Rupakheti Steve Chenoweth (Chapter 18). Outline. Coupling and Cohesion Command and Query Separation Principle. Types of Coupling. Tight to loose coupling Content : Module A has access to local data of Module B
E N D
Coupling Cohesion Chandan R. Rupakheti Steve Chenoweth (Chapter 18)
Outline • Coupling and Cohesion • Command and Query Separation Principle
Types of Coupling • Tight to loose coupling • Content: Module A has access to local data of Module B • Common: Global variable shared between two modules • External: Share an externally imposed data format or communication protocol • Control: Module A controls the control flow of module B through flags • Stamp: Modules share a composite data structure but uses only a part of it • Data: Modules share data through parameters • Message: Modules communicate via message passing (e.g. Observer) • No Coupling: Modules do not communicate at all
Types of Cohesion • Worst to best cohesion • Coincidental: When parts of a module is grouped arbitrarily, e.g. Utilities class • Logical: Grouped because they are logically categorized to do the same thing, e.g. MouseListener • Temporal: Grouped at runtime, e.g. exception processing function that does multiple task, e.g. close file, log error, and notify users • Procedural: Grouped because they follow a certain sequence of execution, e.g. function which checks file permission and opens it • Communicational: Grouped because they operate on the same data • Sequential: Grouped because the output from one part is the input to another part • Functional: Grouped because they all contribute to a single well-defined task of the module
Command-Query Separation Principle • Each method should be either a command or a query • Command Method • Performs an action, typically with side effects, but has no return value • Query Method • Returns data but has no side effects • Why is this principle important? • Principle of least surprise, side effects only happen in “void” methods • Provides for most flexible interface, e.g. a value can be queried multiple times without changing it
Things to Do Next • Look at the quiz for today, and the zipped program. • Answer the related questions about coupling! • Answer the last question about cohesion! • Turn in after class, as usual, but please also be ready to discuss your answers in class!