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

Item 19: Use interfaces only to define types

阅读更多

1.  Interfaces should be used only to define types. They should not be used to export constants.

 

2.  The constant interface pattern is a poor use of interfaces. That a class uses some constants internally is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class’s exported API.

 

3.  The constant interface represents a commitment: if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility. If a non-final class implements a constant interface, all of its subclasses will have their namespaces polluted by the constants in the interface.

 

4.  If the constants are strongly tied to an existing class or interface, you should add them to the class or interface. (i.e. Integer.MIN_VALUE, Double.MAX_VALUE)

 

5.  If the constants are best viewed as members of an enumerated type, you should export them with an enum type.

 

6.  In other cases, you should export the constants with a non-instantiable utility class. (Make the constructor private to prevent instantiation)

 

7.  If you make heavy use of the constants exported by a utility class, you can avoid the need for qualifying the constants with the class name by making use of the static import facility, introduced in release 1.5.

分享到:
评论

相关推荐

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

    Item 22: Use interfaces only to define types Item 23: Prefer class hierarchies to tagged classes Item 24: Favor static member classes over nonstatic Item 25: Limit source files to a single top-level ...

    GObject Reference Manual

    How To define and implement Interfaces? How To define Interfaces? How To define implement an Interface? Interface definition prerequisites Interface Properties Howto create and use signals ...

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

    link ▶You may use file names with a -inl.h suffix to define complex inline functions when needed. The definition of an inline function needs to be in a header file, so that the compiler has the ...

    UEFI_Spec_2_8_A_Feb14.pdf

    The intent of this specification is to define a way for the OS and platform firmware to communicate only information necessary to support the OS boot process. This is accomplished through a formal ...

    UEFI 2.8协议规范(英文)---UEFI_Spec_2_8_final.pdf

    The intent of this specification is to define a way for the OS and platform firmware to communicate only information necessary to support the OS boot process. This is accomplished through a formal ...

    sip-4.15.5.zip

    the ability to define interfaces and to write adaptors that allow an object to appear to implement an interface without needing to change the object itself the ability to specify a user interface ...

    sip-4.15.5.tar.gz

    the ability to define interfaces and to write adaptors that allow an object to appear to implement an interface without needing to change the object itself the ability to specify a user interface ...

    C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition

    to override a type member, how to use polymorphism, how to create extension methods, and how to cast between classes in an inheritance hierarchy. Chapter 8, Working with Databases Using Entity ...

    Effective C++(第三版)

    make interfaces easy to use correctly and hard to use incorrectly. 条款19:设计class犹如设计type treat class design as type design. 条款20:宁以pass-by-reference-to-const替换pass-by-value prefer pass-...

    Pro WPF in C# 2010 Mar 2010

    Chapter 2: XAML describes the XAML standard that you use to define user interfaces. You’ll learn why it was created and how it works, and you’ll create a basic WPF window using different coding ...

    TypeScript 2.x for Angular Developers-Packt Publishing(2017).epub

    We will see how to create components with classes, implement lifecycle hooks with TypeScript interfaces, and define components, metadetails using TypeScript decorators. Chapter 6, Component ...

    WPTools.v6.29.1.Pro

    We recommend to use TMS components to create modern user interfaces, toolbars and ribbons. 19.2.2013 - WPTools 6.29.1 - fix in rtf writing code to solve problem with merged cells - fix possible range...

    LCTF软件备份VariSpec™ Liquid Crystal Tunable Filters

    Of course, to use the new functions one must link the application code with the new .lib file containing these functions. Previous release: 1.20 Release date December 3rd, 2003 Known bugs: a) there ...

    acpi控制笔记本风扇转速

    subfunctions in order to reduce CPU stack use and improve maintainability. (Mikhail Kouzmich) AcpiGetHandle: Fix for parameter validation to detect invalid combinations of prefix handle and pathname....

    An Introduction to Design Patterns in C++ with Qt Second Edition

    * Define inheritance relationships to share code and promote reuse. * Learn how code libraries are designed, built, and reused. * Work with QObject, the base class underlying much of Qt. * Build ...

    计算机网络第六版答案

    First, the content provider has more control over the user experience, since it has to use few intermediary ISPs. Second, it can save money by sending less traffic into provider networks. Third, if ...

    Professional C# 3rd Edition

    Chapter 19: Windows Forms 573 Creating a Windows Form Application 574 Control Class 579 Size and Location 580 Appearance 580 User Interaction 580 Windows Functionality 582 Miscellaneous Functionality ...

    Professional WPF Programming

    book also covers the new XAML syntax, which is a markup language used to define UI in your WPF applications, regardless of whether you are developing for a standalone Windows-based environment or ...

    Cisco Press - OSPF Network Design Solutions, 2nd Edition

    Chapter 2 Introduction to OSPF 47 Chapter 3 OSPF Communication 103 Part II OSPF Routing and Network Design 161 Chapter 4 Design Fundamentals 163 Chapter 5 Routing Concepts and Configuration 225 ...

    Senfore_DragDrop_v4.1

    4 doesn't), you will have to use the convert.exe utility supplied with Delphi to convert all the demo form files to binary format. A batch file, convert_forms_to delphi_4_format.bat, is supplied in...

Global site tag (gtag.js) - Google Analytics