Cohesion and Coupling

Coupling - A measure of how much a module (package, class, method) relies on other modules. It is desirable to reduce coupling, or reduce the amount that a given module relies on the other modules of a system.

Cohesion - A measure of how closely related the members (classes, methods, functionality within a method) of a module are to the other members of the same module. It is desirable to increase cohesion as that indicates that a module has a very specific task and does only that task.

Coupling.

Definition:  Coupling is the extent to which the parts of an OOD are interconnected.

 

Cohesion.

Definition:  Cohesion is the extent to which the parts of an OOD are interrelated.  Cohesion is “the degree to which the elements of a portion of a design contribute to the carrying out of a single, well-defined purpose.”

 

 

What makes a design better? 

 

By participating in the process of evaluating designs, you have likely devised some criteria (perhaps only implicit and/or subjective) that you use to determine why one design is better than another.  If these criteria are good, of course, you should try to apply them to your own design choices. 

 

Coad and Yourdon offer some guidance in devising more objective criteria.  These criteria are based on coupling and cohesion. One goal of good design is to minimize the lifetime cost of a solution to a problem.  By evaluating a design in terms of coupling and cohesion, Coad and Yourdon aim to find a means of comparing lifetime costs for competing solution designs.

 

The following (brief) notes are based on Chapter 8 of our textbook.  You are encouraged to read that chapter.  However, always keep in mind that these ideals might not always point to the best design solution for an actual, real problem. 

 

Coupling.

Definition:  Coupling is the extent to which the parts of an OOD are interconnected.

 

2 Types of coupling:

Interaction coupling.

            Definition: The extent to which the parts of an OOD are interconnected through message passing.

 

            Desired design characteristic: Low interaction coupling.

 

            Determining interaction coupling: Look at the number of messages and the number of parameters included in the messages sent/received by objects.

 

            Bottom line (what to look for in a good design): Few message “arrows” and message “arrows” having 3 or fewer parameters. A good design also avoids “pass through” messages as described on page 131-132 of the text.

 

            Why is low interaction coupling a good thing? Over the lifetime of a design with a low degree of interaction coupling, when one class is modified, few other classes are likely to be affected.  This decreases the complexity and cost of maintenance. 

 

Inheritance coupling.

            Definition: The extent to which the parts of an OOD are interconnected through inheritance.  In particular, the extent to which a specialization class inherits from its more general parent class.

 

Desired design characteristic: High inheritance coupling.

 

            Determining inheritance coupling: Look at the attributes that belong to the parent class and the child class. The child should inherit and use a large portion of the attributes provided by the parent.

 

            Bottom line (what to look for in a good design):  Few instances in which the child class re-defines an attribute that is provided by the parent; few instances where a child inherits, but makes no use of, an attribute provided by the parent.

 

            Why is high inheritance coupling a good thing? Consider a design with low inheritance coupling  (a bad thing). Over the lifetime of a design with a low degree of inheritance coupling, when one class is modified, the modification does not affect many other classes.  In effect, an equivalent modification might have to be made several times. This increases the complexity and cost of maintenance. 

 

Cohesion.

Definition:  Cohesion is the extent to which the parts of an OOD are interrelated.  Cohesion is “the degree to which the elements of a portion of a design contribute to the carrying out of a single, well-defined purpose.”

 

3 Types of cohesion:

Service cohesion.

            Definition: The extent to which a design’s provided services and performed functions are in a one-to-one relationship.

 

            Desired design characteristic: High service cohesion.

 

            Determining service cohesion:  A good design does not require more than one service to carry out a function; a good design does not allow a service to carry out more than one function.

 

·        Look at the expected complexity of the service.  If the service is too complex, it is probably fulfilling more than one function. 

·        Look for the number of services required to complete one function.  If more than one service is required to fulfill one function, then the design lacks service cohesion.

 

            Bottom line (what to look for in a good design): Ask the designer to describe each service using an English sentence.  If all services are described each by a single, simple sentence with one verb and one direct object, the design is likely to have high service cohesion.

 

            Why is high service cohesion a good thing? Over the lifetime of a design with a high degree of service cohesion, when it becomes necessary to change a function performed by the design, a change to a single service will be required.  This decreases the complexity and cost of maintenance. 

 

 Class cohesion.

            Definition: The extent to which the attributes and services of a class are used.

 

            Desired design characteristic: High class cohesion.

 

            Determining class cohesion/ Bottom line (what to look for in a good design) :  A good design for a class will have no extra or unused attributes or services.

 

            Why is high class cohesion a good thing? Over the lifetime of a design the more simple design decreases the cost of maintenance. 

 

 

Generalization/Specialization cohesion.

            Definition: The extent to which the design’s generalization/specialization relationships are appropriate.

 

            Desired design characteristic: High generalization/specialization cohesion.

 

Determining generalization/specialization cohesion:  In a good design, each specialization of a more general class is appropriate.  The more often this is not the case, the worse the design is. 

 

            Bottom line (what to look for in a good design): Ask this question:  is the child class truly a specialization of the parent class?  Or is the generalization/specialization relationship misplaced.  For example, suppose a design has the parent class “printer” as the generalization for the child classes “laser_printer,” “dot_matrix_printer” and “telephone.”  The third child class shows a lack of generalization/specialization cohesion in the design.    

 

            Why is high generalization/specialization cohesion a good thing? It should result in lower maintenance costs over the lifetime of the design.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值