You are previewing Sams Teach Yourself Object Oriented Programming in 21 Days, Second Edition.
O'Reilly logo
Sams Teach Yourself Object Oriented Programming in 21 Days, Second Edition

Book Description

Sams Teach Yourself Object Oriented Programming in 21 Days differs from other OOP books in two main ways. Many classic OOP books are designed for software engineers and teach at an academic level. Sams Teach Yourself Object Oriented Programming in 21 Days presents accessible, user-friendly lessons designed with the beginning programmer in mind. Other OOP books work to present both OOP and to teach a programming language (for example: Object-Oriented Programming in C++). Although Sams Teach Yourself Object Oriented Programming in 21 Days uses Java to present the examples, the book is designed to present concepts that apply to any OOP environment.

Table of Contents

  1. Copyright
    1. Dedication
  2. Teach Yourself Object Oriented Programming in 21 Days
  3. About the Author
  4. Acknowledgments
  5. Tell Us What You Think!
  6. Introduction
    1. About the Examples
    2. What You Need to Know to Use This Book
  7. 1. Defining OO
    1. Week 1 At a Glance
    2. 1. Introduction to Object Oriented Programming
      1. Object Oriented Programming in a Historical Context
        1. Precursors to OOP
        2. Object Oriented Programming
          1. An OOP Approach to Software Using Objects
          2. What Is a Class?
          3. Putting It All Together: Classes and Objects
          4. Putting Objects to Work
          5. Object Relationships
        3. How Object Oriented Programming Builds Upon the Past
      2. Benefits and Goals of OO
        1. Natural
        2. Reliable
        3. Reusable
        4. Maintainable
        5. Extendable
        6. Timely
      3. Pitfalls
        1. Pitfall 1: Thinking of OOP as Simply a Language
        2. Pitfall 2: Fearing Reuse
        3. Pitfall 3: Thinking of OO as a Cure-All
        4. Pitfall 4: Selfish Programming
      4. The Week Ahead
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Exercises
    3. 2. Encapsulation: Learn to Keep the Details to Yourself
      1. The Three Pillars of Object Oriented Programming
      2. Encapsulation: The First Pillar
        1. An Example of Interface and Implementation
        2. Public, Private, and Protected
        3. Why Should You Encapsulate?
      3. Abstraction: Learning to Think and Program Abstractly
        1. What Is Abstraction?
        2. Two Examples of Abstraction
        3. Effective Abstraction
      4. Keeping Your Secrets Through Implementation Hiding
        1. Protecting Your Object Through the Abstract Data Type (ADT)
          1. What Is a Type?
          2. An Example ADT
        2. Protecting Others from Your Secrets Through Implementation Hiding
        3. A Real-World Example of Implementation Hiding
      5. Division of Responsibility: Minding Your Own Business
      6. Encapsulation Tips and Pitfalls
        1. Abstraction Tips and Pitfalls
        2. ADT Tips and Pitfalls
        3. Implementation Hiding Tips
        4. How Encapsulation Fulfills the Goals of OO Programming
      7. Caveats
      8. Summary
      9. Q&A
      10. Workshop
        1. Quiz
        2. Exercises
    4. 3. Encapsulation: Time to Write Some Code
      1. Lab 1: Setting Up the Java Environment
        1. Problem Statement
      2. Lab 2: Class Basics
        1. Problem Statement
        2. Solutions and Discussion
      3. Lab 3: Encapsulation Roundup
        1. Problem Statement
        2. Solutions and Discussion
      4. Lab 4: Case Study—The Java Primitive Wrappers (Optional)
        1. Problem Statement
        2. Solutions and Discussion
      5. Q&A
      6. Workshop
      7. Quiz
      8. Exercises
    5. 4. Inheritance: Getting Something for Nothing
      1. What Is Inheritance?
      2. Why Inheritance?
      3. “Is-A” Versus “Has-A”: Learning When to Use Inheritance
      4. Learning to Navigate Inheritance’s Tangled Web
        1. Inheritance Mechanics
          1. Overridden Methods and Attributes
          2. New Methods and Attributes
          3. Recursive Methods and Attributes
      5. Types of Inheritance
      6. Inheritance for Implementation
        1. Implementation Inheritance Problems
        2. Inheritance for Difference
          1. Specialization
        3. Inheritance for Type Substitution
      7. Tips to Effective Inheritance
      8. Summary
      9. How Inheritance Fulfills the Goals of OO
      10. Q&A
      11. Workshop
        1. Quiz
        2. Exercises
    6. 5. Inheritance: Time to Write Some Code
      1. Lab 1: Simple Inheritance
        1. Problem Statement
        2. Solutions and Discussion
      2. Lab 2: Using Abstract Classes for Planned Inheritance
        1. Problem Statement
        2. Solutions and Discussion
      3. Lab 3: Bank Account—Practicing Simple Inheritance
        1. A Generic Account
        2. The Savings Account
        3. The Timed Maturity Account
        4. Checking Account
        5. Overdraft Account
        6. Problem Statement
        7. Extended Problem Statement
        8. Solutions and Discussion
      4. Lab 4: Case Study—“Is-A,” “Has-A,” and the java.util.Stack
        1. Problem Statement
        2. Solutions and Discussion
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Exercises
    7. 6. Polymorphism: Learning to Predict the Future
      1. Polymorphism
      2. Inclusion Polymorphism
      3. Parametric Polymorphism
        1. Parametric Methods
        2. Parametric types
      4. Overriding
      5. Overloading
        1. Coercion
      6. Effective Polymorphism
      7. Polymorphic Pitfalls
        1. Pitfall 1: Moving Behaviors Up the Hierarchy
        2. Pitfall 2: Performance Overhead
        3. Pitfall 3: Blinders
      8. Caveats
      9. How Polymorphism Fulfills the Goals of OO
      10. Summary
      11. Q&A
      12. Workshop
        1. Quiz
        2. Exercises
    8. 7. Polymorphism: Time to Write Some Code
      1. Lab 1: Applying Polymorphism
        1. Problem Statement
        2. Solutions and Discussion
      2. Lab 2: Bank Account—Applying Polymorphism to a Familiar Example
        1. Problem Statement
        2. Solutions and Discussion
      3. Lab 3: Bank Account—Using Polymorphism to Write Future-Proof Code
        1. Problem Statement
        2. Solutions and Discussion
      4. Lab 4: Case Study—The Java Switch and Polymorphism
        1. Fixing a Conditional
        2. Problem Statement
        3. Solutions and Discussion
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Exercises
    9. Week 1 In Review
  8. 2. Learning to Apply OO
    1. Week 2 At a Glance
    2. 8. Introduction to the UML
      1. Introduction to the Unified Modeling Language
      2. Modeling Your Classes
        1. Basic Class Notation
        2. Advanced Class Notation
        3. Modeling Your Classes to Suit Your Purposes
      3. Modeling a Class Relationship
        1. Dependency
        2. Association
          1. Aggregation
          2. Composition
        3. Generalization
      4. Putting It All Together
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Exercises
    3. 9. Introduction to Object Oriented Analysis (OOA)
      1. The Software Development Process
        1. The Iterative Process
          1. An Iterative Approach
          2. An Incremental Approach
        2. A High-Level Methodology
      2. Object Oriented Analysis (OOA)
        1. Using Use Cases to Discover System Use
          1. Identify the Actors
          2. Create a Preliminary List of Use Cases
          3. Refine and Name the Use Cases
            1. Splitting Use Cases
            2. Combining Use Cases
            3. The Resulting Use Cases
          4. Define Each Use Case’s Sequence of Events
          5. Use-Case Diagrams
          6. Interaction Diagrams
            1. Sequence Diagrams
            2. Collaboration Diagrams
          7. Activity Diagrams
        2. Building the Domain Model
      3. So Now What?
      4. Summary
      5. Q&A
      6. Workshop
        1. Quiz
        2. Exercises
    4. 10. Introduction to Object Oriented Design (OOD)
      1. Object Oriented Design (OOD)
      2. How Do You Apply OOD?
        1. Step 1: Generate an Initial List of Objects
        2. Step 2: Refine Your Objects’ Responsibilities
          1. What Are CRC Cards?
          2. How Do You Apply CRC Cards?
          3. An Example CRC Card
          4. How Many Responsibilities Per Class?
          5. CRC Card Limitations
        3. Step 3: Develop the Points of Interaction
          1. Interfaces
          2. Agents
          3. Future Considerations
          4. Data Transformation
        4. Step 4: Detail the Relationships Between Objects
        5. Step 5: Build Your Model
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Exercises
    5. 11. Reusing Designs Through Design Patterns
      1. Design Reuse
      2. Design Patterns
        1. The Pattern Name
        2. The Problem
        3. The Solution
        4. The Consequences
      3. Pattern Realities
      4. Patterns by Example
        1. The Adapter Pattern
          1. Implementing an Adapter
          2. When to Use the Adapter Pattern
        2. The Proxy Pattern
          1. Publish/Subscribe and the Proxy Pattern
          2. The General Proxy Pattern
          3. When to Use the Proxy Pattern
        3. The Iterator Pattern
          1. Implementing an Iterator
          2. When to Use the Iterator Pattern
      5. Making a Pattern Yours
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Exercises
      9. Answers to Quiz
      10. Answers to Exercises
    6. 12. Advanced Design Patterns
      1. More Patterns by Example
        1. The Abstract Factory Pattern
          1. Implementing an Abstract Factory
          2. When to Use the Abstract Factory Pattern
        2. The Singleton Pattern
          1. Implementing a Singleton
          2. Inheritance and the Singleton Pattern
          3. When to Use the Singleton Pattern
        3. The Typesafe Enum Pattern
          1. Implementing the Typesafe Enum Pattern
          2. When to Use the Typesafe Enum Pattern
      2. Pattern Pitfalls
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Exercises
      6. Answers to Quiz
      7. Answers to Exercises
    7. 13. OO and User Interface Programming
      1. OOP and the User Interface
      2. The Importance of Decoupled UIs
      3. How to Decouple the UI Using the Model View Controller Pattern
        1. The Model
          1. Implementing The Model
        2. The View
          1. Implementing the View
        3. The Controller
          1. Implementing the Controller
          2. Putting the Model View and Controller Together
      4. Problems with the Model View Controller
        1. An Emphasis on Data
        2. Tight Coupling
        3. Inefficiency
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Exercises
    8. 14. Building Reliable Software Through Testing
      1. Testing OO Software
      2. Testing and the Iterative Software Development Process
      3. Forms of Testing
        1. Unit Testing
        2. Integration Testing
        3. System Testing
        4. Regression Testing
      4. A Guide to Writing Reliable Code
        1. Combining Development and Testing
          1. An Example Unit Test
          2. Why You Should Write Unit Tests
          3. Writing Unit Tests
          4. JUnit
          5. Writing Advanced Unit Tests
        2. Writing Exceptional Code
        3. Writing Effective Documentation
          1. Source Code as Documentation
            1. Coding Conventions
            2. Constants
            3. Comments
            4. Names
            5. Method and Class Headers
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Exercises
    9. Week 2 In Review
  9. 3. Putting It All Together: A Complete OO Project
    1. Week 3 At a Glance
    2. 15. Learning to Combine Theory and Process
      1. Blackjack
        1. Why Blackjack?
        2. Vision Statement
        3. Overriding Requirements
      2. Initial Blackjack Analysis
        1. The Blackjack Rules
          1. Betting
          2. Dealing
          3. Playing
          4. Settlement
          5. Miscellaneous
        2. Identifying the Actors
        3. Creating a Preliminary List of Use Cases
      3. Planning the Iterations
        1. Iteration 1: Basic Game Play
        2. Iteration 2: Rules
        3. Iteration 3: Betting
        4. Iteration 4: User Interface
      4. Iteration 1: Basic Game Play
        1. Blackjack Analysis
          1. Refining the Use Cases
          2. Modeling the Use Cases
          3. Modeling The Domain
        2. Blackjack Design
          1. CRC Cards
          2. The Command Line UI
          3. The Blackjack Model
        3. The Implementation
          1. The Card
          2. The Deck and Deckpile
          3. The Player and HumanPlayer
          4. The Dealer
          5. The BlackjackGame
          6. A Procedural Pitfall
        4. Testing
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Exercises
    3. 16. Blackjack Iteration 2: Adding Rules
      1. Blackjack Rules
        1. Rules Analysis
          1. Blackjack Rules Use Case Analysis
          2. Modeling the Use Cases
          3. Updating the Blackjack Domain Model
        2. Rules Design
          1. State Diagrams
          2. Modeling the Player States
          3. Modeling the Dealer States
          4. The Updated Blackjack Model
        3. Rules Implementation
          1. Changes to Hand
          2. Changes to Player
          3. Changes to Dealer and BlackjackDealer
        4. Testing
      2. Summary
      3. Q&A
      4. Workshop
        1. Quiz
        2. Exercises
    4. 17. Blackjack Iteration 3: Adding Betting
      1. Blackjack Betting
        1. Betting Analysis
          1. Blackjack Betting Use Case Analysis
          2. Updating the Blackjack Domain Model
        2. Betting Design
          1. Designing the Bank
          2. The Betting Design
          3. Refactoring the Player Hierarchy
          4. The Updated Blackjack Model
        3. Betting Implementation
          1. The Bank Implementation
          2. The BettingPlayer Implementation
          3. Changes to Dealer and HumanPlayer
          4. Changes to BlackjackDealer
          5. Miscellaneous Changes
        4. A Little Testing: A Mock Object
      2. Summary
      3. Q&A
      4. Workshop
        1. Quiz
        2. Exercises
    5. 18. Blackjack Iteration 4: Adding a GUI
      1. Blackjack Presentation
      2. Command Line Tweaks
      3. Blackjack GUI Analysis
        1. GUI Use Cases
        2. GUI Visual Mock Ups
        3. Blackjack GUI Design
        4. GUI CRC Cards
          1. PlayerView
          2. OptionView and OptionViewController
          3. CardView
          4. BlackjackGUI
          5. Miscellaneous
        5. GUI Structure
        6. Refactoring
        7. GUI Class Diagram
        8. Blackjack GUI Implementation
        9. Implementing the VCard, VDeck, and CardView
        10. Implementing the PlayerView
        11. Implementing the OptionView and the OptionViewController
        12. Implementing the GUIPlayer
        13. Putting It All Together with the BlackjackGUI
      4. Summary
      5. Q&A
      6. Workshop
        1. Quiz
        2. Exercises
    6. 19. Applying an Alternative to MVC
      1. An Alternative Blackjack GUI
        1. The PAC Layers
        2. The PAC Philosophy
        3. When to Use the PAC Design Pattern
      2. Analyzing the PAC Blackjack GUI
      3. Designing the PAC Blackjack GUI
        1. Identifying the Presentation Layer Components
        2. Designing the Abstraction Layer Components
        3. Designing the Control
        4. Using the Factory Pattern to Avoid Common Errors
      4. Implementing the PAC Blackjack GUI
        1. Implementing the VCard and VHand
        2. Implementing the VBettingPlayer
        3. Implementing the VBlackjackDealer
        4. Implementing the GUIPlayer
        5. Putting It All Together with the Control
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Exercises
    7. 20. Having Some Fun with Blackjack
      1. Having Fun with Polymorphism
        1. Creating a Player
        2. The Safe Player
        3. Adding the SafePlayer to the GUI
        4. Polish
      2. OOP and Simulations
        1. The Blackjack Players
          1. Implementing the FlipPlayer
          2. Implementing the OneHitPlayer
          3. Implementing the SmartPlayer
          4. Setting Up the Simulator
          5. The Results
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Exercises
    8. 21. The Final Mile
      1. Tying Up the Loose Ends
        1. Refactoring the Blackjack Design for Reuse in Other Systems
          1. The Design Problem
          2. Sewing a Solution with Threads
        2. Identifying the Benefits the OOP Brought to the Blackjack System
        3. Industry Realities and OOP
      2. Summary
      3. Q&A
      4. Workshop
        1. Quiz
        2. Exercises
    9. Week 3 In Review
  10. Appendices
    1. A. Answers
      1. Day 1 Quiz Answers
        1. Answers to Quiz
      2. Day 2 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      3. Day 3 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      4. Day 4 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      5. Day 5 Quiz Answers
        1. Answers to Quiz
      6. Day 6 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      7. Day 7 Quiz Answers
        1. Answers to Quiz
      8. Day 8 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      9. Day 9 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      10. Day 10 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      11. Day 11 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      12. Day 12 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      13. Day 13 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      14. Day 14 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      15. Day 15 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      16. Day 16 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      17. Day 17 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      18. Day 18 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      19. Day 19 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      20. Day 20 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
      21. Day 21 Quiz and Exercise Answers
        1. Answers to Quiz
        2. Answers to Exercises
    2. B. Java Primer
      1. Java Developer’s Kit: J2SE 1.3 SDK
        1. Development Environment Configuration
      2. SDK Tools Overview
        1. Java Compiler: javac
        2. Java Interpreter: java
        3. Java Archive Utility: jar
        4. Java Documentation and the Documentation Generator: javadoc
      3. Java Playpen: Your First Java Program
        1. Compiling and Running
        2. Creating a .jar File
        3. Generating javadoc
      4. Java Language Mechanics
        1. Simple Java Class
        2. Data Types
        3. Variables
        4. Constants
        5. Operators
        6. Conditional Statements
        7. Loops
      5. Classes and Interfaces—Building Blocks of Java
        1. Using Existing Classes
        2. Creating Your Own Classes
        3. Interfaces
        4. Inner Classes and Anonymous Inner Classes
      6. Summary
    3. C. UML Reference
      1. UML Reference
      2. Classes
        1. Object
        2. Visibility
        3. Abstract Classes and Methods
      3. Notes
      4. Stereotypes
      5. Relationships
        1. Dependency
        2. Association
          1. Roles
          2. Multiplicity
        3. Aggregation
        4. Composition
        5. Generalization
      6. Interaction Diagrams
        1. Collaboration Diagrams
        2. Sequence Diagrams
    4. D. Selected Bibliography
      1. Analysis, Design, and Methodologies
      2. C++ Programming
      3. Design Patterns
      4. General OO Principals and Theory
      5. “Hard Core” Theory (But Don’t Let That Scare You!)
      6. Java Programming
      7. Miscellaneous
      8. Smalltalk
      9. Testing
    5. E. Blackjack Code Listings
      1. blackjack.core
      2. blackjack.core.threaded
      3. blackjack.exe
      4. blackjack.players
      5. blackjack.ui
      6. blackjack.ui.mvc
      7. blackjack.ui.pac