Learn how to hide java-1.3 from the ground up
In this section, let's talk about why we need to hide the specific implementation.
In general, programming is divided into the class creator and the class user. The Creator hides some fragile code and only exposes some necessary methods to the user. In this way, the creator can modify the hidden part as needed without affecting users' calls.
Why do we need to hide the specific implementation.
1) Prevent users from accessing 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;}}
In the Bird class above, we have hidden the name attribute. Users of the class can only use the getName method or the setName method to perform operations, but cannot directly perform any operations on the name, because the name attribute is relatively fragile, you can change it to another one. At the end of the change, the name may not be the result you need. In particular, some malicious users assign null values, which is troublesome.
Negative 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 sets the object name to null, but the actual service is impossible. Therefore, exposing the name attribute poses unknown risks for subsequent operations. The correct method 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 blank);} public static void main (String [] args) {Bird bird1 = new Bird (); bird1.setName (null ); bird bird2 = new Bird (); bird2.setName (jack );}}
Running output:
Name cannot be blank
The above code avoids the risk of empty names, hides vulnerable code, and only exposes necessary code methods.
2) allows the Creator to modify hidden code at will.
In addition to limiting users and facilitating creators, we only need to return the same things and how to implement them in the middle.
For example:
We adapted the above Code.
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 blank);} public static void main (String [] args) {Bird bird1 = new Bird (); bird1.setName (null ); bird bird2 = new Bird (); bird2.setName (jack );}}
We also added setId at the same time of setName. Although a lot of code is added, it is not known to the user. It can be used and known just like the above Code.
Finally, we should talk about how to hide the specific implementation. However, this topic is too broad and generally needs to be done as needed. In a word, requirements determine what to hide.
Summary: This section describes why specific implementations need to be hidden.
This chapter is here. Thank you.