Graduate Program KB

Chapter 6 - Objects and Data-Structures

  • Law of Demeter

    • "A module should not know about the inards of the objects it manipulates"
    • Guideline to reduce coupling
      • Coupling: Dependency of one class on another
      • Example: Multiple function calls from class A to class B tightly couples class A and B
    • Law of Demeter Explanation / Example:
      • When we have an object that accesses another objects inards using the object accessor pattern it breaks the law of demeter.
      • This tightly couples classes together as the objects and methods of many other classes are all being called in one line of code to return some data.
      • Class A, B, C Example (Train Wreck): (var data = new A().GetObjectB().GetObjectC().GetData();) => This breaks the law of demeter
  • Data Abstraction:

    • Commonly to get data/variables from objects, developers attach getter and setter methods
    • We don't want to blindly expose our objects or change indivudal class variables
    • Rather we want to expose abstract interfaces that allow manipulation of the way variables/data changes
  • Abstraction Definition:

    • Process of hiding implementation details from the user so that only the functionality is shown
    • User is intended only to see what the object does rather than how it does it (explaining implementation)
  • Objects vs Data Structures (The Shape Example)

    • Procedural Coding (Data Structures)
      • Shape Classes are Data Structures(Circle, Square, Rectangle)
      • Geometry Class handles implementation of the functions
    • Coding using abstractions (Inheritance) (Object Oriented)
      • Interface Shape holds all the possible methods
      • implementation is in all the classes that implement the Shape inteface (Circle, Square, Rectangle)
  • Object Oriented Approach

    • Pros
      • Easy to add new classes (implementation in the class)
    • Con
      • Hard to define new behaviour (Adding a new function) as each class needs to change
  • Procedural Approach

    • Pros
      • Easy to add new behaviour (Adding a new function) as only one large function needs to be added
    • Con
      • Hard to add a new data structure (class) as every function needs to be updated to accommodate the change
  • Conclusion: What to choose when?

    • Procedural Approach: When you are adding behaviour without changing the data structures
    • Object Oriented Approach: When you are adding many types of objects but the behaviour is unchanging