Polymorphic Properties:Mentioning object-oriented, it is natural to think of three major features:encapsulation, inheritance, polymorphism。 Their directories are: 1. Encapsulation, which enables the code to modularize the internal structure and state of the package. 2. Inheritance, used to extend the original code.3. polymorphic, convenient interface reuse, through the same interface and incoming objects invoke the implementation of different objects. Polymorphism at run-time binding functions, but not polymorphism at compile time has determined the function call address.
What are the benefits of polymorphism from an architectural design perspective? Reverse the source and run dependencies. Usually one function in the system calls another function, whether it is aRun-timeStill isin the source codeare dependent on the same direction, that is, the calling module relies on the called module. Polymorphism makes the source-dependent direction reverse, and the calling module no longer relies on the called module, which relies on only one polymorphic interface. This is how the plug-in works by injecting the dependent called module into the calling module through control inversion and dependency injection. In engineering practice, the plug-in architecture is robust, which makesstable, high-value business rules no longer rely on low-value, easily changing modules。
Conclusion: The robust business system architecture needs to use polymorphism.
C + + on the engineering practice of polymorphism: in the C + + language, two kinds of polymorphism are supported, that is, compile-time polymorphism (implemented by overloaded functions) and run-time polymorphism (implemented by virtual functions). This paper mainly discusses the practice of polymorphism at runtime.The most common use is to declare a base-class pointer, and then use that pointer to point to any subclass object and invoke the virtual function implementation of the corresponding subclass object. There are two different ways to do this:
1. Virtual Functions A virtual function is a member function that is declared as virtual in a base class and redefined in a derived class to implement a dynamic overwrite (override) of a member function.
2. Abstract classclasses that contain pure virtual functions (function declaration shapes such as:virtual void funtion () =0 ) are called abstract classes. Pure virtual function is a virtual function declared in the base class, because it is not defined in the base class, it is required that any derived class should define its own implementation method, and thus achieve the purpose of realizing polymorphism, and often use abstract class as interface in engineering practice.
On object-oriented polymorphism and C + + practice