Object Interconnections
Transcript of Object Interconnections
Introduction Connections Coupling Cohesion Visibility Summary References
OBJECT INTERCONNECTIONS
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 23, 2015
Introduction Connections Coupling Cohesion Visibility Summary References
OUTLINE I
1 INTRODUCTION
2 CONNECTIONS
3 COUPLING
4 COHESION
5 VISIBILITY
6 SUMMARY
7 REFERENCES
Introduction Connections Coupling Cohesion Visibility Summary References
INTRODUCTION
In this chapter we move up a level of abstraction, andconsider collections of objects working together.Our focus will be on how objects are connected to eachother, and how one can make those connections as looseas possible.Our primary tool for analyzing connectednesss will be theconcepts of visibility and dependency.
Introduction Connections Coupling Cohesion Visibility Summary References
CONNECTIONS – THE BANE OF LARGE SCALE
PROGRAMMING
Difficulties in developing large scale programs are oftennot so much a matter of algorithmic complexity as they areof communication complexity.If several programmers are working together on a project,need to control the amount of information one programmermust have about the code being developed by a secondprogrammer.
Introduction Connections Coupling Cohesion Visibility Summary References
VISIBILITY
Visibility is an attribute of names.Names of variables, functions, fields, whatever.If you can’t name something, you can’t manipulate it.Languages already have a variety of mechanisms for thecontrol of name visability.OOP languages introduce a few new twists.
Introduction Connections Coupling Cohesion Visibility Summary References
DEPENDENCY
Dependency describes the degree to which one softwarecomponent relies on another component to perform itsresponsibilities.A high degree of dependency obviously limits code reuse -moving one component to a new project.
Introduction Connections Coupling Cohesion Visibility Summary References
COUPLING AND COHESION
Ideas from the Software Engineering Community,pre-dating OOP.Coupling refers to the extent to which one componentuses another to perform actions.Generally a goal is to reduce coupling between software.Cohesion refers to the extent to which the actions of acomponent seem to be tied together in purpose.Generally a goal is to increase cohesion within a softwarecomponent.
Introduction Connections Coupling Cohesion Visibility Summary References
VARIETIES OF COUPLING
Arranged from Bad to Better.Internal data coupling.Global data coupling.Control (or sequence) coupling.Component coupling.Parameter coupling.Subclass coupling.
Introduction Connections Coupling Cohesion Visibility Summary References
INTERNAL DATA COUPLING
EXAMPLE
class SneekyModif ier {public :
void sneeky ( ) {/ / change my f r i e n d s namemyFriend−>name = " Lucy " ;
}Person ∗ myFriend ;
} ;
class Person {public :
Person ( ) {name = " Lar ry " ;
}s t r i n g name ;
} ;
This is bad because it makes it difficult to understand asingle class in isolation.Can be mitigated by always making your data areas privateor protected, and not exposing pointers to these areas.
Introduction Connections Coupling Cohesion Visibility Summary References
GLOBAL DATA COUPLING
DOUBLE TODAYSDOW;
class One {public :
void setDow ( ) {todaysDow = 9473;
}} ;
class Two {public :
void printDow ( ) {cout << " Today the Dow h i t "
<< todaysDow ;}
} ;
Two or more classes that interact through a common globalvariable.Again, makes it difficult to understand a single class ininsolation.Can be mitigated by making a class that “manages” theglobal area, thereby reducing global coupling tocomponent coupling.
Introduction Connections Coupling Cohesion Visibility Summary References
CONTROL, OR SEQUENCE COUPLING
This occurs when objects are linked by the fact that onemust be manipulated before the other, but otherwise theyhave no connection.Again, makes it difficult to understand a class in isolation.Can be mitagated by making a controller class, that clearlyindicates the sequence of operations.
EXAMPLE
class MyClass {public :
doS tu f f ( ) {d o F i r s t ( ) ;doSecond ( ) ;doThird ( ) ;
}
protected :d o F i r s t ( ) { . . . }doSecond ( ) { . . . }doThird ( ) { . . . }
}
Introduction Connections Coupling Cohesion Visibility Summary References
COMPONENT COUPLING
Occurs when one class holds an instance of another class.
EXAMPLE
class Set {. . .
private :L i s t data ;
}
Ideally, connection is one way. Held component has noknowledge or holder.This is a very weak and benign connection. (Weak is good,remember).
Introduction Connections Coupling Cohesion Visibility Summary References
PARAMETER COUPLING
Parameter coupling occurs when one object knows ofanother only through being passed as a parameter or areturn value.Another very weak (and therefore good) type of coupling.
EXAMPLE
class MyClass {public :
void doSomething ( Set aSet ) {. . .
}}
Introduction Connections Coupling Cohesion Visibility Summary References
SUBCLASS COUPLING
Subclass coupling describes the relationship between aparent class and a child class.Ideally the parent has no strong connection to the child, sothe connection is one way.Can understand the parent in isolation from the child.
EXAMPLE
class Parent {. . .
}
class Chi ld extends Parent {. . .
}
A very weak form of coupling. Which makes it a good designchoice.
Introduction Connections Coupling Cohesion Visibility Summary References
FORMS OF COHESION
Arranged from bad to better.Coincidental cohesion.Logical cohesion.Temporal cohesion.Communication cohesion.Sequential cohesion.Functional cohesion.Data cohesion.
Introduction Connections Coupling Cohesion Visibility Summary References
LIMITING COUPLING – THE LAW OF DEMETER
The law of demeter is an attempt to limit the way in whichone component can interact with another component.Law of Demter: In any Method M attached to a class C,only methods defined by the following classes may beused:
1 The instance variable classes of C.2 The argument classes of method M (including C); note that
global objects or objects created inside the method M areconsidered arguments to M.
Named for the Demeter project at Northeaster University.
Introduction Connections Coupling Cohesion Visibility Summary References
REWRITTEN IN TERMS OF MESSAGES
Law of Demeter (weak form). Inside a method, it is onlypermitted to access or send messages to the followingobjects:
1 The arguments associated with the method being executed(including the self object).
2 Instance variables for the receiver of the method.3 Global variables.4 Temporary variables created inside the method.
The strong form eliminates global variables and inheriteddata fields.
Introduction Connections Coupling Cohesion Visibility Summary References
WHAT IS RULED OUT
Basically, what is ruled out by the law of demeter is oneobject going in and directly manipulating the internal datavalues of another object.Instead, all access to data values in another componentshould be made through procedures – thereby reducingdata coupling to the weaker parameter coupling.
Introduction Connections Coupling Cohesion Visibility Summary References
CLASS LEVEL VERSUS OBJECT LEVEL VISIBILITY
Here is another interesting way that object-orientedlanguages have chosen to differ from each other.Question: Are sisters and brothers allowed to look at eachothers private data fields?An answer of YES is class-level visibility (C++ and Java)and answer of NO is object-level visibiity.
Introduction Connections Coupling Cohesion Visibility Summary References
ACTIVE VALUES
The creation of active values is a good illustration of whyparameter coupling is better than direct manipulation.Suppose we have an existing program and we just want toobserve a data value – see when it gets set and changed.Solution – create a new subclass that just changes thosemethods that set or read the data value.
EXAMPLE
@inter face Reactor : Object{ . . .
double heat ; . . .}− ( void ) setHeat : ( double ) newValue ;− ( double ) getHeat ;@end
@implementation GraphicalReactor : Reactor− ( void ) setHeat : ( double ) newValue
{/∗ code necessary to ∗ //∗ update gauge ∗ /[ super setHeat : newValue ] ;
}@end
Can add new functionality simply by replacing an object with aninstance of a subclass; making no change to the original class.
Introduction Connections Coupling Cohesion Visibility Summary References
PRIVATE, SUBCLASS AND PRIVATE FACES
We have several times noted that object have a public andprivate face - inheritance introduces a third alternative, thesubclass face.Public features are those aspects that users of thesoftware component must have access to.Private features are those aspects that the implementor ofthe software component must have access to.Protected features are those aspects that implementors ofchild classes can have access to.There are two types of clients for the class developer.These are user clients (those who use an instance of theclass), and subclass clients (those who will subclass fromthe class).
Introduction Connections Coupling Cohesion Visibility Summary References
CONTROL OF VISIBILITY
The public/protected/private modifiers are the primary wayto control. visibility in most OO languages, the there areother mechanisms as well.Friends in C++.Inner classes in Java and C++.Private Inheritance in C++.Name Spaces, Units or Packages.
Introduction Connections Coupling Cohesion Visibility Summary References
FRIENDS IN C++
In C++ a friend (class or method) is allowed access to all partsof a class.
EXAMPLE
class Complex {public :
Complex ( double , double ) ;f r iend double abs ( Complex &) ;
private :double rp ;double i p ;
} ;
double abs ( Complex& x ){ return s q r t ( x . rp ∗ x . rp + x . i p ∗ x . i p ) ; }
Friendship is something that is given away, not something thatis taken.
Introduction Connections Coupling Cohesion Visibility Summary References
INNER CLASSES
Inner classes in Java and, to a lesser extent, nestedclasses in C++ and C# are allowed to access the dataareas in the surrounding class.
EXAMPLE
class Aconta inerClass {. . .
/ / r e t u r n an enumeratorpublic Enumeration elements ( )
{ return new MyEnumeration ( ) ; }. . .
/ / i nne r c lass i s al lowed to see/ / a l l aspects o f surrounding c lass
private class MyEnumeration implements Enumeration {. . .public boolean hasMoreElements ( ) { . . . }public Object nextElement ( ) { . . . }
}}
Uses a lot for event listeners in Java, among other things.
Introduction Connections Coupling Cohesion Visibility Summary References
PRIVATE INHERITANCE IN C++
In a public inheritance the public features of a parentbecome public features of a child.In a private inheritance, the public and protected featuresof the parent do not filter through the child; and only thepublic features of the child are visible.
Introduction Connections Coupling Cohesion Visibility Summary References
NAMESPACES, PACKAGES AND UNITS
Names Spaces (in C++), Packages (Java) or Units (Delphiand Object Pascal) give the programmer another way toencapsulate names, and release only those names thatare necessary.
EXAMPLE
package foo ;public class bar { / / w i l l be v i s i b l e
. . .}
class baz { / / w i l l not be v i s i b l e ou ts ide package. . .
}
Introduction Connections Coupling Cohesion Visibility Summary References
INTENTIONAL DEPENDENCY
Sometimes want code to depend upon another class.Sometimes want this even if the dependee doesn’t knowthe dependant. (example, a model and its display).Can be managed by having a separate “dependancymanager”.When an object changes, it tells the manager “notify mydepedants”.
Introduction Connections Coupling Cohesion Visibility Summary References
SUMMARY
In this chapter we have examined a variety of topicsrelated to dependency.How classes and objects can depend upon each other.How methods within a class can depend upon each other.How to control visibility, as a means of controllingdependency.How strong dependency can be weaked by usingalternative designs.
Introduction Connections Coupling Cohesion Visibility Summary References
REFERENCES
Images and content for developing these slides have beentaken from the follwoing book with the permission of theauthor.An Introduction to Object Oriented Programming, TimothyA. Budd.This presentation is developed with Beamer:
Darmstadt, lily.