OO Modeling Heuristics


  • 0
    M
    1. Eliminate irrelevant classes.
    • That have no meaningful behaviour. (demote them to attributes)
    1. Eliminate classes that are outside the system.
    • Imagine you have a banking system. And a Customer(class) sends messages to your ATM(class),
      just sending messages to the ATM is not meaningful behaviour. Your ATM must be be able to receive messages, be invoked.
    • How many object-oriented developers are needed to screw a light bulb?
      0 because and object-oriented developer will teach the light bulb to screw itself.
    1. Do not turn an operation into a class.
      Egg: Do not create a class InvertImage that contains an invertImage method. Create a class
      Image that multiple operations related to images.
    2. Be sure that the abstractions that you model are classes and not simply the roles objects play.
    • If the abstractions have different behaviour then they go into different classes.
    1. Distribute the intelligence uniformly.
      Egg: If you model a room temperature regulator, let's suppose the Room package has different classes PresenceSensor, TemperatureSensor, HumiditySensor, there is a Regulator class outside the room package and a Heater class.
      The Regulator class should not call on the Room package: getTemperature(), isPresent(), getHumidity() then start to compute if the room needs heat or not.
      The Regulator should only ask the Room if it needs heat, and the room class should do all the computations internally. Eventually if the room needs heat, (or not) the regulator may start/stop the Heater.

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.