Mastering Generalization in OOP: Techniques and Examples
mohamed Tayel
Posted on November 15, 2024
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
andManager
both have anEmail
attribute, moveEmail
to the parent classPerson
:
public class Person
{
public string Email { get; set; }
}
public class Employee : Person { ... }
public class Manager : Person { ... }
2. Pull Up Method
- Scenario: Two child classes have the same method implementation.
- Solution: Move the method to the parent class.
-
Example:
If
Rectangle
andCircle
both have aCalculateArea()
method, move it to the parent classShape
:
public abstract class Shape
{
public abstract double CalculateArea();
}
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 theEngineType
field in the parent classVehicle
, moveEngineType
toCar
:
public class Vehicle { ... }
public class Car : Vehicle
{
public string EngineType { get; set; }
}
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 theSendPushNotification()
method inSoftware
, moveSendPushNotification()
toMobileApp
:
public class Software { ... }
public class MobileApp : Software
{
public void SendPushNotification() { ... }
}
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
andScanner
, create anIMachine
interface:
public interface IMachine
{
void Start();
void Stop();
}
public class Printer : IMachine { ... }
public class Scanner : IMachine { ... }
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 aGenerateDigitalSignature()
method used only forLegalDocument
, move the method to aLegalDocument
subclass:
public class Document { ... }
public class LegalDocument : Document
{
public void GenerateDigitalSignature() { ... }
}
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
, andLaptop
all have common fields (ModelName
,Price
,Brand
) and methods, create aGadget
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 { ... }
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.
\
💖 💪 🙅 🚩
mohamed Tayel
Posted on November 15, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.