`
leonzhx
  • 浏览: 785933 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 13: Minimize the accessibility of classes and members

阅读更多

1.  A well-designed module hides all of its implementation details, cleanly separating its API from its implementation. Modules then communicate only through their APIs and are oblivious to each others’ inner workings. This concept, known as information hiding or encapsulation, is one of the fundamental tenets of software design.

 

2.  Information hiding decouples the modules that comprise a system, allowing them to be developed, tested, optimized, used, understood, and modified in isolation.

 

3.  Make each class or member as inaccessible as possible.

 

4.  For top-level (non-nested) classes and interfaces, there are only two possible access levels: package-private and public.

 

5.  If a package-private top-level class (or interface) is used by only one class, consider making the top-level class a private nested class of the sole class that uses it.


6.  For members (fields, methods, nested classes, and nested interfaces), there are four possible access levels, in order of increasing accessibility : private, package-private, protected, public.

 

7.  If a method overrides a superclass method, it is not permitted to have a lower access level in the subclass than it does in the superclass.

 

8.  It is acceptable to make a private member of a public class package-private in order to test it, but it is not acceptable to raise the accessibility any higher than that.

 

9.  Instance fields should never be public.

 

10.  Classes with public mutable fields are not thread-safe.

 

11.  You can expose constants via public static final fields, assuming the constants form an integral part of the abstraction provided by the class. By convention, such fields have names consisting of capital letters, with words separated by underscores. It is critical that these fields contain either primitive values or references to immutable objects.

 

12.    It is wrong for a class to have a public static final array field, or an accessor that returns such a field.

 

13.    You can make the array filed private and add a public immutable list:

private static final Thing[] PRIVATE_VALUES = { ... };
public static final List<Thing> VALUES =
Collections.unmodifiableList(Arrays.asList(PRIVATE_VALUES));

 

 

Alternatively, you can make the array field private and add a public method that returns a copy of a private array:

 

private static final Thing[] PRIVATE_VALUES = { ... };
public static final Thing[] values() {return PRIVATE_VALUES.clone();}

 

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 15: Minimize the accessibility of classes and members Item 16: In public classes, use accessor methods, not public fields Item 17: Minimize mutability Item 18: Favor composition over inheritance ...

    Effective C#

    **Item 13: Use Proper Initialization for Static Class Members** - **Importance:** Proper initialization ensures thread safety and avoids potential race conditions. - **Best Practice:** Initialize ...

    the effect of voltage dips oninduction motors

    of the different processes, and then choose the appropriate method to minimize the effects of voltage dips, within the constraints of the supply and budget. The worst case scenario is that of a three ...

    Microsoft® .NET: Architecting Applications for the Enterprise

    Description: Make the right architectural decisions up front and improve the quality and reliability of your results. Led by two enterprise programming experts, you ll learn how to apply the patterns ...

    A.Philosophy.of.Software.Design.2018.4 (1).pdf

    This book addresses the topic of software design: how to decompose complex software systems into modules (such as classes and methods) that can be implemented relatively independently. The book first ...

    Signal Integrity - Simplified(Eric Bogatin).pdf

    Inductance Principle #3: When the Number of Field Line Loops Around a Conductor Changes, There Will Be a Voltage Induced Across the Ends of the Conductor Section 6.6. Partial Inductance Section 6.7. ...

    Veracity of Big Data Machine Learning and Other 2018

    Develop the mathematical foundation needed to help minimize the impact of the problem using easy-to-understand language and examples Use diverse tools and techniques such as machine learning ...

    Effective C# 2ed (Covers C# 4.0)

    See how optional parameters can minimize the number of method overloads (see Item 10) You’re already a successful C# programmer–this book will help you become an outstanding one. About the Author ...

    Lean_manufacturing_Production_flow_and_activities_AX2012

    The white paper "Lean Manufacturing: Production Flows and Activities AX2012" is focused on the implementation and understanding of lean manufacturing concepts within the Microsoft Dynamics AX2012 ...

    财务管理第十六章课件.pptx

    Chapter 16 of the "Financial Management" course discusses the concept of financial leverage and capital structure policy, which are crucial elements in determining the financial health and overall ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Of course, .cc files typically do require the definitions of the classes they use, and usually have to include several header files. Note: If you use a symbol Foo in your source file, you should ...

    Kanban for the Shopfloor

    - **Operator Requirements**: Calculating the number of operators needed based on the takt time and workload. - **Line Balancing**: Ensuring that each workstation has an equal amount of work to prevent...

    essential skills of java

    Organizations should utilize this document as a reference guide for training and evaluating the skills of their development teams. Additionally, integrating secure development lifecycle practices ...

    HX8347-D_DS_T__preliminary_v01_080624_Foxlink.pdf

    The **HX8347-D(T)** is a TFT (Thin Film Transistor) mobile single chip driver designed for driving displays with a resolution of 240RGBx320 dots and 262K colors. This device incorporates an internal ...

    Microprocessor Design Principles and Practices With VHDL

    - **2.5.2 Duality Principle:** The principle that every algebraic expression derived by the laws of Boolean algebra remains valid if the operators and identity elements are interchanged. - **2.5.3 ...

    用友自动化测试面试题

    - **Define Scope and Objectives**: Clearly define the scope and objectives of testing. - **Select Tools and Resources**: Choose appropriate tools and allocate necessary resources. - **Create a ...

Global site tag (gtag.js) - Google Analytics