You are previewing How to Use Objects: Code and Concepts.
O'Reilly logo
How to Use Objects: Code and Concepts

Book Description

While most developers today use object-oriented languages, the full power of objects is available only to those with a deep understanding of the object paradigm. How to Use Objects will help you gain that understanding, so you can write code that works exceptionally well in the real world.

Author Holger Gast focuses on the concepts that have repeatedly proven most valuable and shows how to render those concepts in concrete code. Rather than settling for minimal examples, he explores crucial intricacies, clarifies easily misunderstood ideas, and helps you avoid subtle errors that could have disastrous consequences.

Gast addresses the technical aspects of working with languages, libraries, and frameworks, as well as the strategic decisions associated with patterns, contracts, design, and system architecture. He explains the roles of individual objects in a complete application, how they react to events and fulfill service requests, and how to transform excellent designs into excellent code. Using practical examples based on Eclipse, he also shows how tools can help you work more efficiently, save you time, and sometimes even write high-quality code for you.

Gast writes for developers who have at least basic experience: those who’ve finished an introductory programming course, a university computer science curriculum, or a first or second job assignment.

Coverage includes

• Understanding what a professionally designed object really looks like

• Writing code that reflects your true intentions—and testing to make sure it does

• Applying language idioms and connotations to write more readable and maintainable code

• Using design-by-contract to write code that consistently does what it’s supposed to do

• Coding and architecting effective event-driven software

• Separating model and view, and avoiding common mistakes

• Mastering strategies and patterns for efficient, flexible design

• Ensuring predictable object collaboration via responsibility-driven design

Register your product at informit.com/register for convenient access to downloads, updates, and corrections as they become available.

Table of Contents

  1. About This E-Book
  2. Title Page
  3. Copyright Page
  4. Dedication Page
  5. Contents
  6. Preface
  7. Acknowledgments
  8. About the Author
  9. Introduction
    1. The Central Theme: Code and Concepts
    2. The Structure of the Book
    3. How to Read the Book
    4. Hints for Teaching with the Book
  10. Part I: Language Usage
    1. Chapter 1. Basic Usage of Objects
      1. 1.1 The Core: Objects as Small and Active Entities
      2. 1.2 Developing with Objects
        1. 1.2.1 Effective Code Editing in Eclipse
        2. 1.2.2 Refactoring: Incremental Design Improvements
        3. 1.2.3 The Crucial Role of Naming
      3. 1.3 Fields
        1. 1.3.1 Data Structures
        2. 1.3.2 Collaborators
        3. 1.3.3 Properties
        4. 1.3.4 Flags and Configuration
        5. 1.3.5 Abstract State
        6. 1.3.6 Caches
        7. 1.3.7 Data Shared Between Methods
        8. 1.3.8 Static Fields
      4. 1.4 Methods
        1. 1.4.1 An Object-Oriented View on Methods
        2. 1.4.2 Method Overloading
        3. 1.4.3 Service Provider
        4. 1.4.4 Convenience Method
        5. 1.4.5 Processing Step
        6. 1.4.6 Explanatory Methods
        7. 1.4.7 Events and Callbacks
        8. 1.4.8 Reused Functionality
        9. 1.4.9 Template Method
        10. 1.4.10 Delegated to Subclass
        11. 1.4.11 Extending Inherited Behavior
        12. 1.4.12 Factory Methods
        13. 1.4.13 Identity, Equality, and Hashing
        14. 1.4.14 Refused Bequest
      5. 1.5 Exceptions
        1. 1.5.1 Basic Usage
        2. 1.5.2 Exceptions and the System Boundary
        3. 1.5.3 Exceptions to Clarify the Program Logic
        4. 1.5.4 Exceptions for Checking Expectations
        5. 1.5.5 Exceptions to Signal Incompleteness
        6. 1.5.6 Exception Safety
        7. 1.5.7 Checked Versus Unchecked Exceptions
      6. 1.6 Constructors
        1. 1.6.1 Initializing Objects
        2. 1.6.2 Initialization by Life-Cycle Methods
        3. 1.6.3 Constructors and Inheritance
        4. 1.6.4 Copying Objects
        5. 1.6.5 Static Constructor Methods
      7. 1.7 Packages
        1. 1.7.1 Packages as Components
        2. 1.7.2 The Facade Pattern
      8. 1.8 Basics of Using Classes and Objects
        1. 1.8.1 General Facets of Objects
        2. 1.8.2 Service Provider
        3. 1.8.3 Information Holder
        4. 1.8.4 Value Object
        5. 1.8.5 Reusable Functionality
        6. 1.8.6 Algorithms and Temporary Data
        7. 1.8.7 Boundary Objects
        8. 1.8.8 Nested Classes
        9. 1.8.9 The null Object
    2. Chapter 2. Fundamental Object Structures
      1. 2.1 Propagating State Changes: Observer
        1. 2.1.1 Example: Observing Background Jobs
        2. 2.1.2 Crucial Design and Implementation Constraints
        3. 2.1.3 Implementation Details and Decisions
        4. 2.1.4 Judging the Need for Observers
      2. 2.2 Compound Objects
        1. 2.2.1 Ownership
        2. 2.2.2 Structure Sharing
        3. 2.2.3 Compound Objects and Encapsulation
        4. 2.2.4 Compound Objects and Observers
      3. 2.3 Hierarchical Structures
        1. 2.3.1 The Composite Pattern
        2. 2.3.2 The Visitor Pattern
        3. 2.3.3 Objects as Languages: Interpreter
        4. 2.3.4 Excursion: Composites and Stack Machines
      4. 2.4 Wrappers: Adapters, Proxies, and Decorators
        1. 2.4.1 The Adapter Pattern
        2. 2.4.2 The Decorator Pattern
        3. 2.4.3 The Proxy Pattern
        4. 2.4.4 Encapsulation Wrappers
    3. Chapter 3. Abstraction and Hierarchy
      1. 3.1 Inheritance
        1. 3.1.1 The Liskov Substitution Principle
        2. 3.1.2 Interface Between the Base Class and Subclasses
        3. 3.1.3 Factoring Out Common Behavior
        4. 3.1.4 Base Classes for Reusable Infrastructure
        5. 3.1.5 Base Classes for Abstraction
        6. 3.1.6 Reifying Case Distinctions
        7. 3.1.7 Adapting Behavior
        8. 3.1.8 Inheritance Versus Delegation
        9. 3.1.9 Downcasts and instanceof
        10. 3.1.10 Implementation Inheritance
        11. 3.1.11 The Fragile Base Class Problem
      2. 3.2 Interfaces
        1. 3.2.1 Behavioral Abstraction
        2. 3.2.2 Client-Specific Classification and Abstraction
        3. 3.2.3 Abstraction Hierarchies
        4. 3.2.4 Multiple Classification
        5. 3.2.5 Extension Interface
        6. 3.2.6 Specifying Callbacks
        7. 3.2.7 Decoupling Subsystems
        8. 3.2.8 Tagging Interfaces
        9. 3.2.9 Management of Constants
        10. 3.2.10 Inheritance Versus Interfaces
  11. Part II: Contracts
    1. Chapter 4. Contracts for Objects
      1. 4.1 The Core: Assertions Plus Encapsulation
      2. 4.2 Elaborating the Concepts by Example
        1. 4.2.1 Invariants and Model Fields
        2. 4.2.2 Contracts in Terms of Model Fields
        3. 4.2.3 Contracts, Invariants, and Processing Steps
        4. 4.2.4 The Role of Constructors
        5. 4.2.5 Pure Methods for Specification
        6. 4.2.6 Frame Conditions: Taming Side Effects
      3. 4.3 Motivating Contracts with Hindsight
      4. 4.4 Invariants and Callbacks
      5. 4.5 Checking Assertions at Runtime
      6. 4.6 The System Boundary
      7. 4.7 Arguing About the Correctness of Programs
        1. 4.7.1 Assignment
        2. 4.7.2 Loops: Summing over an Array
        3. 4.7.3 Conditionals and Loops: Binary Search
        4. 4.7.4 Outlook
    2. Chapter 5. Testing
      1. 5.1 The Core: Unit Testing
      2. 5.2 The Test First Principle
      3. 5.3 Writing and Running Unit Tests
        1. 5.3.1 Basic Testing Guidelines
        2. 5.3.2 Creating Fixtures
        3. 5.3.3 Dependency Injection
        4. 5.3.4 Testing OSGi Bundles
        5. 5.3.5 Testing the User Interface
      4. 5.4 Applications and Motivations for Testing
        1. 5.4.1 Testing to Fix Bugs
        2. 5.4.2 Testing to Capture the Contracts
        3. 5.4.3 Testing to Design the Interface
        4. 5.4.4 Testing to Find and Document the Requirements
        5. 5.4.5 Testing to Drive the Design
        6. 5.4.6 Testing to Document Progress
        7. 5.4.7 Testing for Safety
        8. 5.4.8 Testing to Enable Change
        9. 5.4.9 Testing to Understand an API
        10. 5.4.10 Testing for a Better Work–Life Balance
    3. Chapter 6. Fine Print in Contracts
      1. 6.1 Design-by-Contract
        1. 6.1.1 Contracts First
        2. 6.1.2 Weaker and Stronger Assertions
        3. 6.1.3 Tolerant and Demanding Style
        4. 6.1.4 Practical Examples of Demanding Style
        5. 6.1.5 Stronger and Weaker Class Invariants
      2. 6.2 Contracts and Compound Objects
        1. 6.2.1 Basics
        2. 6.2.2 Ownership and Invariants
        3. 6.2.3 Invariants on Shared Objects
      3. 6.3 Exceptions and Contracts
      4. 6.4 Inheritance and Subtyping
        1. 6.4.1 Contracts of Overridden Methods
        2. 6.4.2 Invariants and Inheritance
  12. Part III: Events
    1. Chapter 7. Introduction to the Standard Widget Toolkit
      1. 7.1 The Core: Widgets, Layouts, and Events
      2. 7.2 The WindowBuilder: A Graphical Editor for UIs
        1. 7.2.1 Overview
        2. 7.2.2 Creating and Launching SWT Applications
      3. 7.3 Developing with Frameworks
        1. 7.3.1 The Goals of Frameworks
        2. 7.3.2 Inversion of Control
        3. 7.3.3 Adaptation Points in Frameworks
        4. 7.3.4 Liabilities of Frameworks
      4. 7.4 SWT and the Native Interface
        1. 7.4.1 Influence on the API
        2. 7.4.2 Influence on Launching Applications
      5. 7.5 Compound Widgets
      6. 7.6 Dialogs
      7. 7.7 Mediator Pattern
      8. 7.8 Custom Painting for Widgets
      9. 7.9 Timers
        1. 7.9.1 Timeouts and Delays
        2. 7.9.2 Animations
      10. 7.10 Background Jobs
        1. 7.10.1 Threads and the User Interface
        2. 7.10.2 Long-Running Tasks
        3. 7.10.3 Periodic Jobs
      11. 7.11 Review: Events and Contracts
    2. Chapter 8. A Brief Introduction to Threads
      1. 8.1 The Core: Parallel Code Execution
      2. 8.2 Correctness in the Presence of Threads
      3. 8.3 Notifications Between Threads
      4. 8.4 Asynchronous Messages
      5. 8.5 Open Calls for Notification
      6. 8.6 Deadlocks
    3. Chapter 9. Structuring Applications with Graphical Interfaces
      1. 9.1 The Core: Model-View Separation
      2. 9.2 The Model-View-Controller Pattern
        1. 9.2.1 The Basic Pattern
        2. 9.2.2 Benefits of the Model-View-Controller Pattern
        3. 9.2.3 Crucial Design and Implementation Constraints
        4. 9.2.4 Common Misconceptions
        5. 9.2.5 Behavior at the User Interface Level
        6. 9.2.6 Controllers Observing the Model
        7. 9.2.7 Pluggable Controllers
        8. 9.2.8 The Document-View Variant
      3. 9.3 The JFace Layer
        1. 9.3.1 Viewers
        2. 9.3.2 Finishing Model-View-Controller with JFace
        3. 9.3.3 Data Binding
        4. 9.3.4 Menus and Actions
      4. 9.4 The MVC Pattern at the Application Level
        1. 9.4.1 Setting up the Application
        2. 9.4.2 Defining the Model
        3. 9.4.3 Incremental Screen Updates
        4. 9.4.4 View-Level Logic
      5. 9.5 Undo/Redo
        1. 9.5.1 The Command Pattern
        2. 9.5.2 The Command Processor Pattern
        3. 9.5.3 The Effort of Undo/Redo
        4. 9.5.4 Undo/Redo in the Real World
      6. 9.6 Wrapping Up
    4. Chapter 10. State Machines
      1. 10.1 The Core: An Object’s State and Reactions
      2. 10.2 State Machines in Real-World Scenarios
        1. 10.2.1 Additional Fundamental Elements
        2. 10.2.2 Ongoing Activities
        3. 10.2.3 Nested State Machines
      3. 10.3 Implementing Finite State Machines
        1. 10.3.1 Running Example: Line Editor
        2. 10.3.2 States-as-Assertions
        3. 10.3.3 Explicit States
        4. 10.3.4 State Pattern
  13. Part IV: Responsibility-Driven Design
    1. Chapter 11. Responsibility-Driven Design
      1. 11.1 The Core: Networks of Collaborating Objects
      2. 11.2 The Single Responsibility Principle
        1. 11.2.1 The Idea
        2. 11.2.2 The SRP and Abstraction
        3. 11.2.3 The SRP and Changeability
      3. 11.3 Exploring Objects and Responsibilities
        1. 11.3.1 Example: A Function Plotter
        2. 11.3.2 CRC Cards
        3. 11.3.3 Identifying Objects and Their Responsibilities
        4. 11.3.4 Summing Up
      4. 11.4 Responsibilities and Hierarchy
      5. 11.5 Fundamental Goals and Strategies
        1. 11.5.1 Information Hiding and Encapsulation
        2. 11.5.2 Separation of Concerns
        3. 11.5.3 Compositionality
        4. 11.5.4 Design-Code Traceability
        5. 11.5.5 DRY
        6. 11.5.6 The SOLID Principles
    2. Chapter 12. Design Strategies
      1. 12.1 Coupling and Cohesion
        1. 12.1.1 Coupling and Change
        2. 12.1.2 Coupling and Shared Assumptions
        3. 12.1.3 Cohesion
        4. 12.1.4 The Law of Demeter
      2. 12.2 Designing for Flexibility
        1. 12.2.1 Techniques for Decoupling
        2. 12.2.2 The Layers Pattern
      3. 12.3 Extensibility
        1. 12.3.1 Basic Techniques and Considerations
        2. 12.3.2 The Interceptor Pattern
        3. 12.3.3 The Eclipse Extension Mechanism
        4. 12.3.4 Pipes and Filters
      4. 12.4 Reusability
        1. 12.4.1 The Challenge of Reusability
        2. 12.4.2 The Case of the Wizard Dialogs in JFace
        3. 12.4.3 Building a Reusable Parser
        4. 12.4.4 Strategies for Reuse
  14. Part V: Appendix
    1. Appendix A. Working with Eclipse Plugins
      1. A.1 OSGi: A Module System for Java
        1. A.1.1 The Structure of OSGi Bundles
        2. A.1.2 Working with Bundles in Eclipse
        3. A.1.3 OSGi as an Application Platform
        4. A.1.4 Defining Target Platforms
      2. A.2 Launching Plugins
        1. A.2.1 JUnit Plugin Tests
        2. A.2.2 Contributions to the Eclipse IDE
        3. A.2.3 Eclipse Applications
        4. A.2.4 Installing Plugins in the Workspace
        5. A.2.5 Java Programs
      3. A.3 Where to Go from Here
  15. Bibliography
  16. Index
  17. Code Snippets