`
leonzhx
  • 浏览: 793443 次
  • 性别: 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 ...

    Effective C#

    **Item 19: Ensure That 0 Is a Valid State for Value Types** - **Purpose:** Ensuring that the default state of a value type is valid can prevent errors. - **Example:** Define a `PersonId` struct that ...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    This section covers how to define and use interfaces. - **Implicit Implementation**: Implicit implementation occurs when a class implements an interface method without specifying the interface name. ...

    C++_CLI标准.pdf

    - **Interfaces**: Section 8.10 covers interfaces, which define contracts for classes to implement. - **Enums**: Section 8.11 explains enums, which are a way to define named constants. - **Namespaces ...

    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 ...

    C & C++ Win32 API Programming

    The Win32 API is a set of programming interfaces that provides direct access to the underlying operating system functionalities, enabling developers to create robust and efficient applications for ...

    Wireshark- A Guide to Color My Packets

    - **Multiple Rules**: You can define multiple rules, each with different display filters and colors, to differentiate between various types of traffic visually. - **Conditional Rules**: Complex ...

    iOS 8 Swift Programming Cookbook Solutions Examples for iOS Apps.pdf

    They can also use `UIAttachmentBehavior` and `UIConstraintBehavior` to define object interactions. - **Sample Code**: The book includes detailed code examples for implementing dynamic animations and...

    Packt.Mastering.Csharp.and.NET.Programming

    - **The OS Multitasking Execution Model**: Modern operating systems use sophisticated mechanisms to manage multiple processes and threads. Understanding this model is essential for efficient resource ...

    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 ...

    NS by example

    Let's explore a few examples to illustrate how to use NS effectively: **Example 1: Setting Up a Simple Network** Suppose you want to simulate a simple network consisting of two hosts connected by a ...

    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 ...

    WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4

    The chapter provides examples of how to use these controls to create complex graphical interfaces. #### Properties (Chapter 9) Chapter 9 delves deeper into the concept of properties in WPF. It ...

    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-...

    PROFESSIONAL F# 2.0

    This chapter teaches how to define and use classes in F#, including properties, methods, and constructors. - **Inheritance (Chapter 9):** Inheritance is a core concept in OOP, allowing for the ...

    Java - A Beginner’s Guide - Sixth Edition - Herbert Schildt

    This chapter discusses how to define and use packages, and how they help in managing large-scale projects. It also introduces interfaces, which define contracts that classes can implement. ...

    C# - CSharp 12 in a Nutshell The Definitive Reference

    One of the core features of C# is its type safety, which ensures that variables can only hold values of the types they were declared to hold. This helps prevent runtime errors and makes the code more ...

Global site tag (gtag.js) - Google Analytics