[OOD] A solution that violates the Richter scale replacement principle

Source: Internet
Author: User

About the Ood in the Richter replacement principle, we familiar with can Cheung, no longer open, you can refer to the design pattern of the six design principles of the Richter replacement principle. Here are some attempts to discuss two forms and solutions that are often violated.

The root cause of violation of the Richter scale substitution principle is the ambiguous relationship between the subclass and the parent. Our design succession relationships are often subject to some subjective understanding, such as the relationship between lines and line segments mentioned by Robert C. Martin, as well as the rotten squares and rectangles that you are talking about. From previous experience, we think that they conform to the inheritance relationship, such as the line segment is a short form of lines, the square is a special case of the rectangle. But in fact they are not completely tolerant and irreplaceable.

In the form of a collection, the left is the target of the Richter substitution, and the subclass can fully accommodate the collection of attributes of the parent class. On the right, there is an incompatible set of features:

The solution is to further abstract, to redefine their previous relationship from a language perspective, perhaps indeed is-a, perhaps has-a, maybe they are just brothers.
The basic ideas are as follows:

1. Find a higher level of abstraction


Taking Robert C. Martin's line and line segment as an example, the initial implementation is the base class for LineSegment:

//Line stands for a two point (P1,P2)Class line{ Public:Double Getslope()Const; Point GetP1 ()Const; Point GetP2 ()Const;Virtual BOOLIsOn (Constpoint&)Const;Private: Point itsP1; Point itsP2;}//LineSegment is a line segment connected by a two point (P1,P2). Class LineSegment: PublicLine { Public:Virtual BOOL IsOn(Constpoint&)Const;}

Where the ISON function is used to calculate that a point is not on a line or segment. For a straight line, a point is not on it just depends on the relationship of the point with respect to the two points of the line. And for a line segment, whether it is within the thread start and end boundary. The two have different judging conditions for this interface function, so linesegment cannot replace the parent class directly, violating the Richter scale substitution principle.

The solution is to exclude this inconsistent interface, leaving the public interface as the base class for lines and line segments, defining a linearobject as the base class for line and LineSegment:

public:   doubleGetSlopeconst;   const;   const;   // 纯虚函数的意义在于,确保使用基类的客户代码不会使用这个接口函数   virtualbool IsOn(constconst0private:  Point itsP1;  Point itsP2;}
2. Change to Has-a relationship

Another solution is to have a situation that is too far-fetched for inheritance, such as the so-called is-implemented-in-terms-of (by whom), rather than converting to a combination pattern, such as the following relationship:

Scott Meyers in effective C + + 3e, Item 38 mentions a case. For example, prepare to implement a set based on Std::list. The initial idea is to expect to maintain the same interface as the list, which is defined as:

T>class Set : public std::List<T> {...}

But a huge difference between set and list behavior is that set does not allow duplicate elements, so it violates the Richter scale substitution principle.
The solution is to use the std::list implementation, which is a has-a relationship that can be defined as:

template<typename T>classpublic:  void insert(const T& item);  void remove(const T& item);  private:  std::list<T> rep;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[OOD] A solution that violates the Richter scale replacement principle

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.