Design pattern Learning: the Richter Replacement principle

Source: Internet
Author: User
Tags definition inheritance

In this section we'll talk about the Richter replacement principle, and before we talk about it, let's look at the definition.

Definition: If the object O1 for each type is T1, there is an object O2 of type T2, so that all the program P defined in T1 is substituted for O1 when all the object O2 is replaced, and the behavior of the program P is not changed, then the type T2 is the subtype of the type T1. (Excerpt from Java and schema book)

If you think the definition is vague, it's not clear, it's OK, let's get it over with. Another short definition of the Richter substitution principle is that "all references to base classes must be transparent to the objects of their subclasses." This may be clearer. If you are familiar with an object-oriented language, you should all be aware of object-oriented inheritance, and subclasses inherit from the parent class, and naturally inherit all the methods of the parent class (assuming, of course, that the parent class does not declare the method private).

In object-oriented, inheritance has many advantages:

1 code sharing, reducing the effort to create classes, each with a parent class's methods and attributes;

2 Improve the reusability of the Code;

3 subclasses can resemble the parent class, but also different from the father, "Dragon born, chicken born chicken, mice born to make holes" is said to have the father of the "species", "the world does not have two identical leaves" is to indicate the difference between the son and the father;

4 Improve the scalability of the Code, the implementation of the method of the parent can "do whatever you want", you do not see a lot of open source framework extension interfaces are inherited through the parent class to complete;

5 improve the openness of the product or project.

And the Richter replacement principle is that when you write a class that inherits from the original class, try not to change the original method. The best way to say so much is to give an example.

If you want to do a shootout game, we omit some other details, we focus on the characters in the shooting of the action, we should play CF or CS, or at least heard of shooting games. Every character has a gun, so, here, we let the player own the gun, which is a property of the player that the gun is called. Take a look at the following code:

Example code:

* * Virtual Rob, behind the gun no matter how to achieve, must ensure that shoot this interface, the realization is the gun shooting, different gun firing mode is not the same, but the gun shooting theory has a common step * is the bullet out, fired at each other, specific how we don't care, in short, When the shoot implements the interface of the gun to this firing method, the sub class can not become loaded bullets, this is in violation of the Richter replacement principle * Here is the key, is the shooting method or shooting, although the process details are not the same, but still shooting * if you do not understand why the violation, then please think again carefully, or do not understand,
 Leave a message.
 
* * */interface gun{public void shoot ();}
       The pistol implements the Gun interface class handgun implements gun{public void shoot () {System.out.println ("Pistol shot"); //AK47 implements Gun interface class AK47 implements gun{public void shoot () {System.out.println (
       "AK47 shooting"); }///Machine gun Implementation Gun interface class machinegun implements gun{public void shoot () {System.out.println ("machine
       Gun in a Cartridge ")//This is a violation of the Richter replacement principle} class hero{String name;
       Gun Gun;
       Public Hero (String name) {this.name=name;
       } public void Setgun (Gun Gun) {this.gun=gun;
 Public Gun Getgun (Gun Gun) {return Gun;      The public void shoot () {if (gun==null) {System
                     Out.println ("Don't Rob, go get a Gun");
              return;
       } gun.shoot (); 
             
              } public class main{public static void Main (string[] args) {Hero hero=new Hero ("sharpshooter");
              Match the gunman with AK47 Hero.setgun (new AK47 ());
             
              Hero.shoot ();
              Match the Gunners with a machine gun hero.setgun (new machinegun ()); Hero.shoot ();//originally wanted to design, but turned into a loaded bullets//This is a more obvious problem Hero.setgun
              (New Handgun ());
       Hero.shoot (); }
}

URL Address: http://www.bianceng.cn/Programming/project/201602/49629.htm

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.