DEV Community

mohamed Tayel
mohamed Tayel

Posted on

Mastering Generalization in OOP: Techniques and Examples

meta description: Learn key techniques for mastering generalization in object-oriented programming, including pull-up, push-down, extract interface, and superclass strategies, with clear examples and practical applications.

1. Pull Up Field

  • Scenario: Two child classes share the same attribute.
  • Solution: Move the attribute to the parent class to reduce redundancy.
  • Example: If Employee and Manager both have an Email attribute, move Email to the parent class Person:
  public class Person
  {
      public string Email { get; set; }
  }

  public class Employee : Person { ... }
  public class Manager : Person { ... }
Enter fullscreen mode Exit fullscreen mode

2. Pull Up Method

  • Scenario: Two child classes have the same method implementation.
  • Solution: Move the method to the parent class.
  • Example: If Rectangle and Circle both have a CalculateArea() method, move it to the parent class Shape:
  public abstract class Shape
  {
      public abstract double CalculateArea();
  }
Enter fullscreen mode Exit fullscreen mode

3. Push Down Field

  • Scenario: Only one child uses an attribute in the parent class.
  • Solution: Move the attribute to the child class to avoid unused fields or null values.
  • Example: If only Car uses the EngineType field in the parent class Vehicle, move EngineType to Car:
  public class Vehicle { ... }

  public class Car : Vehicle
  {
      public string EngineType { get; set; }
  }
Enter fullscreen mode Exit fullscreen mode

4. Push Down Method

  • Scenario: Only one child implements a specific method in the parent class.
  • Solution: Move the method to the child class to avoid throwing NotImplementedException.
  • Example: If only MobileApp implements the SendPushNotification() method in Software, move SendPushNotification() to MobileApp:
  public class Software { ... }

  public class MobileApp : Software
  {
      public void SendPushNotification() { ... }
  }
Enter fullscreen mode Exit fullscreen mode

5. Extract Interface

  • Scenario: Two or more classes have similar methods or properties.
  • Solution: Create an interface and move the shared declarations to it.
  • Example: For Printer and Scanner, create an IMachine interface:
  public interface IMachine
  {
      void Start();
      void Stop();
  }

  public class Printer : IMachine { ... }
  public class Scanner : IMachine { ... }
Enter fullscreen mode Exit fullscreen mode

6. Extract Subclass

  • Scenario: A class has features used only in specific cases.
  • Solution: Create a subclass and move these features to it.
  • Example: If Document has a GenerateDigitalSignature() method used only for LegalDocument, move the method to a LegalDocument subclass:
  public class Document { ... }

  public class LegalDocument : Document
  {
      public void GenerateDigitalSignature() { ... }
  }
Enter fullscreen mode Exit fullscreen mode

7. Extract Superclass

  • Scenario: Multiple classes share fields or methods.
  • Solution: Create a shared parent class and move the shared functionality to it.
  • Example: If Mobile, Tablet, and Laptop all have common fields (ModelName, Price, Brand) and methods, create a Gadget superclass:
  public class Gadget
  {
      public string ModelName { get; set; }
      public decimal Price { get; set; }
      public string Brand { get; set; }
  }

  public class Mobile : Gadget { ... }
  public class Tablet : Gadget { ... }
  public class Laptop : Gadget { ... }
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • These generalization techniques are effective for refactoring and improving code structure.
  • Updated examples ensure applicability across various domains, such as business, software, and devices.
  • Refactoring tools like those in Visual Studio can assist in implementing these techniques efficiently.

\

Top comments (0)