The principle of object-oriented design principle in the Richter scale substitution

Source: Internet
Author: User

The Richter substitution principle was presented in 1994 by the 2008 Turing Award winner, Professor Barbara Liskov , the first woman in computer science in the United States and Professor Jeannette Wing of Carnegie Mellon University. It is strictly stated as follows: If for each object of type S O1, there is an object of type T O2, so that all program P defined in T O1 the substitution O2 of all objects, the behavior of the program P does not change, then the type S is a subtype of type T. This definition is rather awkward and difficult to understand, so we generally use another popular version of it:

The Richter substitution principle (Liskov Substitution Principle, LSP): All references to the base class (parent class) must be able to transparently use objects of its subclasses.

The Richter substitution principle tells us that in the software a base class object is replaced with its subclass object, the program will not produce any errors and exceptions, and the reverse is not true, if a software entity uses a subclass object, then it is not necessarily able to use the base class object. For example: I like animals, I must like dogs, because dogs are the children of animals, but I like dogs, can not be concluded that I like animals, because I do not like mice, although it is also an animal.

For example, there are two classes, one class is BaseClass, the other is the subclass class, and the subclass class is a subclass of the BaseClass class, so if a method can accept a base class object base of BaseClass type, such as: Method1 ( Base), then it is bound to accept a subclass object of type BaseClass sub,method1 (sub) to function properly. Conversely, substitution is not tenable, such as a method Method2 accepts a subclass object of type BaseClass as a parameter: Method2 (sub), then generally there is no method2 (base), unless it is an overloaded method.

The principle of substitution of the Richter scale is one of the important ways to realize the open and closed principle, because the subclass object can be used wherever the base class object is used, so as far as possible the base class type is used in the program to define the object, while at run time the subclass type is determined, and the child class object is substituted for the parent class object.

There are several issues to be aware of when using the Richter substitution principle:

(1) All methods of a subclass must be declared in the parent class, or the child class must implement all methods declared in the parent class. According to the principle of the substitution of the Richter scale, in order to ensure the extensibility of the system, the parent class is usually used in the program to define, if a method exists only in the subclass, does not provide the corresponding declaration in the parent class, the method cannot be used in the object defined in the parent class.

(2) When we use the Richter substitution principle, we try to design the parent class as an abstract class or interface, let the subclass inherit the parent class or implement the parent interface, and implement the method declared in the parent class, the runtime, the subclass instance replaces the parent class instance, we can easily extend the function of the system without modifying the code of the original subclass. Adding new functionality can be achieved by adding a new subclass. The principle of substitution on the Richter scale is one of the concrete means of realization.

(3) In the Java language, during the compilation phase, the Java compiler checks whether a program conforms to the Richter substitution principle, which is an implementation-independent, purely syntactic check, but the Java compiler's check is limited.

In the CRM system developed by Sunny software company, customers can be divided into VIP customers (Vipcustomer) and ordinary Customers (Commoncustomer), the system needs to provide a function of sending email, as shown in the original design scheme 1:

Figure 1 Original structure diagram

After further analysis of the system, it was found that the process of sending mail was the same for both ordinary customers and VIP customers, meaning that the code in the two Send () methods was duplicated, and that new types of customers would be added to the system. To make the system more scalable, and to reduce code duplication, it is reconstructed using the Richter substitution principle.

In this example, you can consider adding a new abstract customer class customer, and the Commoncustomer and Vipcustomer classes as their subclasses, the message sending class Emailsender class for the abstract customer class customer programming, according to the principle of the Richter substitution, The place where the base class object can be accepted is bound to accept the subclass object, so the parameter type of the Send () method in Emailsender is changed to customer, and if you need to add a new type of customer, just use it as a subclass of the customer class. The reconstructed structure is shown in 2:

Fig. 2 Structure diagram after reconstruction

the principle of substitution on the Richter scale is one of the important ways to realize open closed principle. In this example, you use the base class object when passing parameters, and in addition, you can use the Richter substitution principle when defining member variables, defining local variables, and determining method return types. For the base class programming, determine the specific subclass when the program runs.

Extended

The principle of substitution of the Richter scale is named after Professor Barbara Liskov (Barbara Liskov). Barbara Liskov: American computer scientist, 2008 Turing Award winner, 2004 John Von Neumann Award winner, American Academy of Engineering Academician, American Institute of Arts and Sciences, American Computer Association, MIT professor of Electrical and computer science, and the first woman in computer science in the United States.

The principle of object-oriented design principle in the Richter scale substitution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.