Learn from the java-1.3 hide concrete implementation

Source: Internet
Author: User

In this chapter, let's talk about why we need to hide specific implementations.

In general, programming is divided between the creator of the class and the consumer of the class, and the creator hides part of the fragile code and exposes only the necessary methods to the consumer, so that the creator can modify the hidden parts as needed without affecting the user's invocation.

Why you need to hide a specific implementation.

1) The user is not allowed to access all the code at will.

For example:

Package Com.ray.ch01;public class Bird {private string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;}}

The above bird class, we hide the name of this property, the user of the class can only be getname this method to get or setname this method operation, and can not do anything directly to name, because the name of the property is relatively fragile, you change me to change, change to the last, Name may not be the desired result, especially some malicious user, assigned null, which is troublesome.

Reverse Example:

Package Com.ray.ch01;public class Bird {public string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;} public static void Main (string[] args) {Bird Bird = new Bird (); bird.name=null;}}

The above code directly assigns the name of the object to null, but the actual business is impossible to exist, so exposing the name of this property is fraught with unknown risks to the operations behind us. The correct approach is as follows:

Package Com.ray.ch01;public class Bird {private string Name;public string GetName () {return name;} public void SetName (String name) {if (name!=null) {this.name = name;} else {System.out.println ("name cannot be empty");}} public static void Main (string[] args) {Bird bird1 = new Bird (); Bird1.setname (null); Bird bird2 = new Bird (); Bird2.setname ("Jack");}}

Run output:

The name cannot be empty


The above code avoids the risk of a null name, hides the fragile code, and exposes only the necessary code methods.


2) Let the creator be free to modify the hidden code.

In addition to restricting the user, but also to facilitate the creator, we just return the same thing, how the middle implementation, with us.

For example:

We adapted it according to the code above.

Package Com.ray.ch01;public class Bird {private string name;private string id;private void SetId () {id = "123456";} Public String GetName () {return name;} public void SetName (String name) {setId (), if (name! = null) {this.name = name;} else {System.out.println ("name cannot be empty");}} public static void Main (string[] args) {Bird bird1 = new Bird (); Bird1.setname (null); Bird bird2 = new Bird (); Bird2.setname ("Jack");}}


We also setid at the same time in the SetName, although the code adds a lot, but for the user, he is not aware of, he can use and know the same as the above code.


Finally, it should still need to talk about how to hide the specific implementation, but this topic is too broad, and generally need to do according to needs, so do not expand. In a word: the need to decide what to hide concrete implementation.


Summary: This section describes why you need to hide a specific implementation.


This chapter is here, thank you.


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

Learn from the java-1.3 hide concrete implementation

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.