Two basic principles of object-oriented

Source: Internet
Author: User
Liskov substitution principle (LSP ):
A class instance should complete all functions of its superclass instance.
For example, in mathematics, a circle is a special case of an elliptic, but some behavior of an elliptic is also not possessed by a circle in object-oriented systems. For example, the height of an elliptic can be twice the width, but the circle does not.
Demeter rules (SLS ):
In informal terms, this rule assumes that each method should only send messages to a limited number of objects, including: the object as a parameter, indicating the pseudo variable (that is, this) of the current object ), and the direct components of the current object.
For example, we now have a class A that is used to process data from a remote computer. There is a Class B that manages remote computers. A has a method that accepts the parameter B object, object B has a property C for storing the IP address of the remote computer. C can return the IP address D of the remote computer. Object D represents the IP address of object B. Suppose we want to process the data, to determine whether remote computer D is available, we may write
If (B. C. isactive ())
{
//...
}
In this way, it violates the level of detail (PAD), because B. C is neither a parameter nor an attribute of the current object (this. We assume that the IP address of the machine represented by location C is variable, so that once the IP address changes slightly, any subtle changes in Location C will cause errors in the program.

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.