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
EmployeeandManagerboth have anEmailattribute, moveEmailto 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
RectangleandCircleboth 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
nullvalues. -
Example:
If only
Caruses theEngineTypefield in the parent classVehicle, moveEngineTypetoCar:
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
MobileAppimplements 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
PrinterandScanner, create anIMachineinterface:
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
Documenthas aGenerateDigitalSignature()method used only forLegalDocument, move the method to aLegalDocumentsubclass:
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, andLaptopall have common fields (ModelName,Price,Brand) and methods, create aGadgetsuperclass:
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.
\
Top comments (0)