Importance of the null (ID) Interface

Source: Internet
Author: User

This was a discussion I had with Tian Yi in a chat yesterday. Is the tag interface so important? For example, many beginners think that interfaces such as Java. Io. serializable can be used in many cases, and Object serialization functions are not required.
For this reason, we use a common and interesting example! In this example, a hunter is designed to hold a smart shotgun, which means that the shotgun will automatically identify humans and will not open fire if the target is humans, all other things can be killed.
To this end, we use the following three interfaces:
  A something used to represent everything
    
Public interface something {}

Human Interface:
Public interface humans extends something {}

  Animal interface:
Public interface animals extends something {}

Then there are a series of implementations:
Chinese:
Public class Chinese implements humans {}

Japanese:
Public class Japanese {}

Dog:
Public class dog implements animals {}

Monster (he is very smart and posts his tags to him ):
Public class monster implements humans {}

The core part of the program below,Hunters and client programs:
Public class hunter {
Public void fire (object target)
{
If (target instanceof humans)
{
System. Out. println ("this is over. I hit a person and should go to jail! ");
}
Else
{
System. Out. println ("Congratulations, you hit an animal! ");
}
}
// Smart gun
Public void intelligentfire (object target)
{
If (target instanceof humans)
{
Return;
}
System. Out. println ("a shot! "+ Target. getclass ());
// Perform the second kill and other related operations below
// Destroy him
Target = NULL;
}
Public static void main (string [] ARGs ){
Hunter hunter = new hunter ();
Object [] objects = new object [] {new dog (), new Japan (), new Japan (), new Chinese (), new monster (), new something () {}};
For (INT I = 0; I <objects. length; I ++)
Hunter. intelligentfire (objects [I]);
}
}

Run the program and you will find that the output is similar to the following:

Shot it! Class springroad. Demo. taginterface. Dog
Shot it! Class springroad. Demo. taginterface. Japanese
Shot it! Class springroad. Demo. taginterface. Japanese
Shot it! Class springroad. Demo. taginterface. Hunter $1

It can be seen that the smart shotgun targeted six targets and fired four. Only Chinese and monster instances are not shot. This is because the label interface is discussed here. Although humans does not have any way, from the perspective of smart shotgun, he uses this label to determine whether it can be fired.He can neither manage nor manage the hierarchical relationship of the target (for example, the Japanese certainly have a very clear hierarchical structure), that is, the inheritance relationship. He cannot determine where the target comes from. For example, you can use the new operator to create a file, retrieve the file from the container, or load the file from somewhere on the network.
  Hunter just developed a simple rule. To prevent my gun from opening fire on you, you must post a humans tag on yourself. That is to say, you must follow this rule.
Now let's look back, because monster should be a snake or any other animal, but he understands the rules set by hunter and cleverly posts a humans label to himself, this way, we were tricked into the smart shotgun.
While the Japanese think they are amazing and do not follow the rules, I will ignore the rules you set by Hunter, what humans label, I just don't need. So we put it in our program, of course, there is only the part of the kill.

  It can be seen that the importance of the null interface (TAG Interface) is a critical issue in this example. In fact, in the OO world, empty interfaces can be regarded as the highest layer image.
  
Related Articles: Interface Programming

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.