subsections:


Development process -- cognitive factors

  • model -> realize -> refine

Design criteria -- natural, flexible, reusable

  • abstraction -- types
  • modularity -- strong cohesion (class)
  • structure -- subtyping
  • information hiding -- narrow interfaces
  • complexity -- weak coupling

slide: Criteria for design


Class design -- guidelines


slide: Individual class design


Invariant properties -- algebraic laws


  class employee { 
employee
public: employee( int n = 0 ) : sal(n) { } employee* salary(int n) { sal = n; return this; } virtual long salary() { return sal; } protected: int sal; };

Invariant


     k == (e->salary(k))->salary() 
  

slide: Invariant properties as algebraic laws


Problem -- hidden bonus


  class manager : public employee { 
manager
public: long salary() { return sal + 1000; } };

Invariant


      k =?= (m->salary(k))->salary() 
  

slide: Violating the invariant


Solution -- explicit bonus


  class manager : public employee { 
manager'
public: manager* bonus(int n) { sal += n; return this; } };

Invariant -- restored


       k + n == ((m->salary(k))->bonus(n))->salary() 
  

slide: Restoring the invariant


Good Object-Oriented Design

  • organize and reduce dependencies between classes
  • Client

    -- A method m is a client of C if m calls a method of C

    Supplier

    -- If m is a client of C then C is a supplier of m

    Acquaintance

    -- C is an acquaintance of m if C is a supplier of m but not (the type of) an argument of m or (of) an instance variable of the object of m

    • C is a preferred acquaintance of m if an object of C is created in m or C is the type of a global variable
    • C is a preferred supplier of m if C is a supplier and C is (the type of) an instance variable, an argument or a preferred acquaintance

    slide: Clients, suppliers and acquaintances


Law of Demeter

ignorance is bliss


Do not refer to a class C in a method m unless C is (the type of)


   1. an instance variable
   2. an argument of m
   3. an object created in m
   4. a global variable
  

  • Minimize the number of acquaintances!

Class transformations

  • lifting -- make structure of the class invisible
  • pushing -- push down responsibility

slide: The Law of Demeter