Combination/Aggregation Multiplication Principle (LSP)
Definition: Prioritize the use of combinations to make the system more flexible, and then consider inheritance to achieve the purpose of reuse.
Reused method:
Inheritance, combination, aggregation
Explanation:
- Inheritance: When we want to reuse code, we generally give priority to inheritance, but the two classes with inheritance relationships are the two with the highest coupling degree. (If the parent class changes the child class, it may be affected, and if the child class changes the parent class, it may be affected)
If the function of the parent class is relatively stable, it is recommended to use inheritance to implement code reuse, because inheritance is statically defined and cannot be called dynamically at runtime. - Combination: It is the relationship between the whole and the part. The whole cannot be separated from the part. It is meaningless to leave the whole, such as the relationship between the wings of the plane and the plane.
- Aggregation: It is also the relationship between the whole and the part, but the whole can separate the part, and the part can also leave the whole, such as the relationship between the train and the car.
Composition/aggregation: It is defined dynamically at runtime by obtaining references to other objects, that is, saving the properties of other objects in an object. This method requires the object to have a well-defined interface, and this interface does not change frequently, and the object can only be accessed through the interface. This way, we do not destroy the encapsulation, so as long as the types are consistent, the runtime can also replace another object with one object.
Example 1: Combination
using System; using ; using ; using ; using ; namespace Combination aggregation and multiplexing principle { /// <summary> /// Define an aircraft wing class /// </summary> public class Airfoil { /// <summary> /// Define the method /// </summary> public void AdjustAngle() { ("Adjust the angle"); } } public class Plane { // Define a private aircraft wing class variable private Airfoil airfoil; // Omit setters and getters public void Fly() { // Use combination to implement code reuse (); } } }
In the above code, it is not appropriate to use inheritance between the aircraft wings and the aircraft, and it is a combination relationship itself, which realizes the reuse of the code. What is most commonly used is to declare variables of another object in one object.
Example 2: Aggregation
using System; using ; using ; using ; using ; namespace Combination aggregation and multiplexing principle { /// <summary> /// Define a car class /// </summary> public class RailwayCarriage { public void Loading() { ("The carriage loads 118 people"); } } /// <summary> /// Define a train class /// </summary> public class Train { // Define a private car class variable (define a reference to another class in one class) private RailwayCarriage rc; // Omit setters and getters public void Carry() { // You can use the carriage or not if(rc!=null) { (); } ("The front of the car is loaded with 5 people"); } public void Run() { } } }
Code download link:Click here to download
This is what this article about the combination/aggregation and multiplexing principle of C# object-oriented design principles. I hope it will be helpful to everyone's learning and I hope everyone will support me more.