Valid Item, valid tiveitem
Whether the module is well designed has an important factor: whether internal data and implementation details are hidden from external modules.
A well-designed module hides Implementation Details and isolates the API from its implementation.
Modules communicate with each other through APIS, and internal work conditions are invisible to each other.
Encapsulation is one of the basic principles of software design.
Why encapsulation?
Through encapsulation, the coupling relationships between modules can be effectively exposed, so that these modules can be independently developed, tested, optimized, used, understood, and modified.
That is:
- It can increase development efficiency, and modules can be developed in parallel.
- Encapsulation can reduce the maintenance burden and optimize the module more effectively without affecting the correctness of other modules.
- This improves the reusability of the Software. modules can also be used in other environments.
(PS: I recall that I did not have these concepts when I used php to write my website, nor did it affect my development work. I thought there was nothing at the time, because I wrote it by myself, and I did not extend it later, so I had no experience .)
You can use Ram to ensure encapsulation.
The accessibility of an object is determined by the location of the object Declaration and the access modifier.
SuggestionsTry to prevent each class or member from being accessed by the outside world.
There are only two access levels for top-level classes and interfaces:Package-level privateAndPublic.
Smaller accessibility means less compatibility. You can modify it in future releases with confidence.
If it is public, you must always consider the customer's behavior. (PS: If a top-level class is used only within a class, it can be declared as a private static internal class .)
For example, in a later version, the expression method cannot be changed and any constraints cannot be added:
class Point { public double x; public double y;}
As many people get used to, public fields are replaced by public access methods for public classes to ensure data flexibility within the class: class Point {private double x; private double y;
public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; }}
(PS: Of course, some common classes do not comply with this rule, such as java. awt. Point and java. awt. Dimension.
However, the original author also made it clear that these classes are not worth doing .)
If a field can only be public and can be declared as final, there will be less harm.
We can still access this field through public access methods.
However, public setter cannot be used, but constraints are added through the constructor:
public final class Time { private static final int HOURS_PER_DAY = 24; private static final int MINUTES_PER_HOUR = 60; public final int hour; public final int minute; public Time(int hour, int minute) { if (hour < 0 || hour >= HOURS_PER_DAY) throw new IllegalArgumentException("Hour: " + hour); if (minute < 0 || minute >= MINUTES_PER_HOUR) throw new IllegalArgumentException("Min: " + minute); this.hour = hour; this.minute = minute; } // Remainder omitted}
There are four access levels for members (field, method, nested class, and nested Interface:
- Private
- Package-private (default)
- Protected
- Public
After the public API of the class is designed, all the Members will be private.
Then it is found that another class in the same package also needs to access this member, so the access level is changed. If this happens frequently, check whether the design is reasonable.
Although both package and private-level members are part of class implementation, the exported API is not affected.
However, this class implements Serializable.
If protected is used for modification, pay attention to part of the API exported when this member is used.
In addition, do not set the instance field to public. If a field is not final or a final field pointing to a mutable object, the public access level will damage the non-variability of the field. It is no longer an internal data representation and is not thread-safe.
Of course, this is for the length! The array with = 0 is the same.
To declare an array as public, try the following methods:
private static final Thing[] PRIVATE_VALUES = {...};public static final List<Thing> VALUES = Collections.unmodifiableList(Arrays.asList(PRIVATE_VALUES));
Alternatively, you can use clone to copy an array each time:
private static final Thing[] PRIVATE_VALUES = {...};public static final Thing[] values(){ return PRIVATE_VALUES.clone();}