SDSU CS 535 Object-Oriented Programming & Design
Spring Semester, 1999
Heuristics 3
Previous    Lecture Notes Index    Next    
© 1999, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 21-Mar-99

Contents of Doc 11, Heuristics 3


References


Design Patterns: Elements of Reusable Object-Oriented Software , Gamma, Helm, Johnson, Vlissides, Addison Wesley, 1995, Singleton Pattern pp. 127-134

Object-Oriented Design Heuristics , Riel, Addison-Wesley, 1996, Heuristics 2.3, 2.5, 2.6, 3.7, 3.8, 3.9, 4.1, 4.2, 4.3, 4.4.


Listen Here!S-mar4 12mins Doc 11, Heuristics 3 Slide # 2

Keep it Small

2.3 Minimize the number of messages in the protocol of a class

2.5 Do not put implementation details such as common-code private functions into the public interface of a class.

2.6 Do not clutter the public interface of a class with things that users of the class are not able to use or are not interested in using.

3.7 Eliminate irrelevant classes from your design.

3.8 Eliminate classes that are outside the system.

Listen Here!S-mar4 9mins Doc 11, Heuristics 3 Slide # 3

Common Minimal Public Interface

2.4 Implement a minimal public interface that all classes understand

This can be applied a global Object class, but also applies to classes in a project or domain

Java’s Object
clone()
Creates and returns a copy of this object.
equals(Object obj)
Indicates if another object is "equal to" this one.
finalize()
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
getClass()
Returns the runtime class of an object.
hashCode()
Returns a hash code value for the object.
toString()
Returns a string representation of the object.

Thread Related Methods
notify()
wait(long timeout)
notifyAll()
wait(long timeout, int nanos)
wait()


Squeak 2.3 (Smalltalk from Disney)
The Object class has 176 instance methods

Listen Here!S-mar4 12mins Doc 11, Heuristics 3 Slide # 4
Operations, Classes, Methods

3.9 Do not turn an operation into a class

Does it make sense to have:

DisplayText Class
ParseNameValuePairs Class


Listen Here!S-mar4 3mins Doc 11, Heuristics 3 Slide # 5

Relationships between Objects


Type of Relations:

Type
Relation between
Uses
(Object)
Containment
(Object)
Inheritance
(Class)
Association
(Object)

Uses

Object A uses object B if A sends a message to B
Assume that A and B objects of different classes
A is the sender, B is the receiver

Containment

Class A contains class B when A has a field of type B

That is an object of type A will have an object of type B inside it


Listen Here!S-mar4 2mins Doc 11, Heuristics 3 Slide # 6

Six different Ways to Implement Uses


How does the sender access the receiver ?

1. Containment

The receiver is a field in the sender

class Sender {
   Receiver here;
   
   public void method() {
      here.sendAMessage();
   }
}

2. Argument of a method

The receiver is an argument in one of the senders methods

class Sender {
   public void method(Receiver here) {
      here.sendAMessage();
   }
}

Listen Here!S-mar4 5mins Doc 11, Heuristics 3 Slide # 7
3. Ask someone else

The sender asks someone else to give them the receiver
How does receiver access the someone else?

class Sender {
   public void method() {
      Receiver here = someoneElse.getReceiver();
      here.sendAMessage();
   }
}

4. Creation

The receiver is created by the sender
class Sender {
   public void method() {
      Receiver here = new Receiver();
      here.sendAMessage();
   }
}

5 Referential Attribute (Covered later)

6. Global

The receiver is global to the sender


Listen Here!S-mar4 3mins, S-mar4 to end Doc 11, Heuristics 3 Slide # 8

Singleton

Intent

Insure a class only has one instance, and provide a global point of access to it

Motivation

There are times when a class can only have one instance

Applicability

Use the Singleton pattern when




Listen Here!S-mar4 4mins Doc 11, Heuristics 3 Slide # 9
Examples of Singleton Usage

Java Security manager
All parts of a program must access the same security manager
Once set a security manager cannot be changed in a program

Logging the activity of a server
All parts of the server should use the same instance of the logging system
The server should not be able to change the instance of the logging system was it has been set


The singleton is useful in many situations. It can be abused by creating a singleton containing just public fields. These fields then can be used like a common block in Fortran.

Listen Here!S-mar4 8mins Doc 11, Heuristics 3 Slide # 10

Implementation

Java


// Only one object of this class can be created
class Singleton {
   private static Singleton _instance = new Singleton();
   private Singleton()   { fill in the blank }
   public static Singleton getInstance() {
      return _instance;
   }
   public void otherOperations() { blank; }
}
class Program {
   public void aMethod() {
      X = Singleton.getInstance();
   }
}

Note: Java does garbage collection of classes. It is possible that the class Singleton can be garbage collected. The class will be garbage collected only if there are no references to the class or any references to any instances of the object in the JVM. If the singleton class is garbage collected, the next time Singleton is referenced, a new instance of the Singleton class will be created. If the Singleton object changes state over time, this can be a problem. It is possible to turn garbage collection off. (This is done using the -Xnoclassgc flag to java.). If this is not possible, you will need to insure there is always a reference to the class or an instance of the class.

Listen Here!S-mar4 55secs Doc 11, Heuristics 3 Slide # 11

Implementation

C++

// Only one object of this class can be created
class Singleton {
   private:
      static Singleton* _instance;
      void otherOperations();
   protected:
      Singleton();
   public:
      static Singleton* getInstance();
}
Implementation File

Singleton*  Singleton::_instance = 0;
Singleton* Singleton::getInstance() {
   if (  _instance == 0 )
       _instance = new Singleton;
   return _instance;
}

Doc 11, Heuristics 3 Slide # 12

Heuristics for the Uses Relationship


4.1 Minimize the number of classes with another class collaborates





Doc 11, Heuristics 3 Slide # 13
4.2 Minimize the number of messages sends between a class and its collaborator

4.3 Minimize the number of different messages a class sends to another class.

4.4 Minimize the product of the number of methods in a class and the number of different messages they send.

Which is more complex?





Doc 11, Heuristics 3 Slide # 14

Containment Relationship


4.5 If class A contains objects of class B, then A should be sending messages to its fields of type B.

This heuristic prohibits:
Orphaned fields (ones that are never used)
Fields that are only accessed in get/set methods
The one exception to 4.5 is container classes

class Foo {
   Bar data;
   public Bar getData()  {
      return data;
   }
   public void setData( Bar newData) {
      data = newData;
   }
}

4.6 Most of the methods defined on a class should be using most of the fields in the class most of the time

4.7 Classes should not contain more objects than a developer can fit in his or her short-term memory. A common value for this number is 6

Doc 11, Heuristics 3 Slide # 15

Narrow and Deep Containment Hierarchies


Combining fields into new classes can reduce the number of fields in a class


A broad and shallow Containment Hierarchy


A narrow and deep Containment Hierarchy

4.8 Distribute system intelligence vertically down narrow and deep containment hierarchies.

Doc 11, Heuristics 3 Slide # 16

No Talking between Fields


4.14 Objects that are contained in the same containing class should not have a uses relationships between them.



Contained Objects with uses relationships



The containing class should send messages to the contained objects


Doc 11, Heuristics 3 Slide # 17
4.13 A class must know what it contains, but it should not know who contains it.

If a number of classes depend on each other in a complex way, you can violate 4.13 to reduce the number of classes they interact with.

Wrap the classes in a containing class.

Each contained class sends a message to the containing class, which broadcasts the message to the proper contained objects


Complex interactions


Replacing complex interaction with containing class


Copyright © 1999 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.

Previous    visitors since 04-Mar-99    Next