Java Quick Reference
  Application Design
  GUI Design
  Database Processing
  Networking
  Threads
  Errors and Exceptions
  Security
  Documentation

Sidebar - Skyscraper and Firefox

 

SCJD Study Notes - Application Design - OOD

Note

A class is a programming construct; a template used to create objects. Try to think in terms of the object vs the class when you start a design. The design process involves building a model of an object using abstraction.

An interface describes the services the client wants accomplished ie the object's capabilities or functionality. A public interface describes the objects contract with users.
"Always start by designing a minimal public interface."

The implementation is how the object goes about providing the services

In Procedural programming design is based on the implementation; it is task oriented. Object-Oriented programming design is based on the interface; it is service oriented. You need to be concerned, initially, with what an object can do, not how it does it.

Encapsulation hides the non-essentials ie it hides the implementation details. This is not about setting every field to private and writing public gettors and settors. You need to make sure your public interface does not rely on how the objects behaviour is implemented. Think what would happen if every time you upgraded your PC you had to learn a new keyboard layout! Sales would plummet and programmers would become extinct.

When you begin to design an object, you need to act like an investigative reporter and discover the:

  • WHO
  • WHERE, and
  • WHAT

of an object's existance.

  • Who is going to use the object? What clients(actors) are going to use the object you're designing
  • Where is your object going to exist? What hardware and software is involved? Will it exist in a framework ie inside other objects? What operating system will it run on?
  • What functions should it have from the user's point of view? What services can it be reasonably expected to provide?

As a first step, describe, in a single paragraph, exactly what the object you're building should do (requirements). This paragraph is informal and written from a user's perspective ie "I want an object that can display the current date and the time in an analog or digital format." not "This object uses the Java Date class and JPanel to display the date and time. The analog display blah, blah, blah ...."

State and Behaviour

State

An objects attributes define its state (condition). The attributes can be defined as:

  1. Instance fields. An instance is one object created from a class. The instance attributes are unique to each object. For example, a Name class might have two attributes: firstName and lastName. Every object created from the Name class would have a different value for each attribute.
  2. Class fields. A state that holds true for every object in the class. For example, an Employee class may include an id attribute that holds the last id number and is incremented every time a new Employee is created. The value in the id field would be common to all Employee objects.
  3. Class constants. Pre-defined conditions that can be applied to all objects in the class. For example, a class that defines buffer objects may have a MAX_BUFFER value.

Behaviour

Design Traps

It might be easier to describe well-designed code in terms of what it is not vs what it is. The following is a summary of such information gleaned from various sources:

Source: Object-Oriented Design in Java by Stephen Gilbert and Bill McCarty

Data Warehouse Trap

An object is not a repository for data that the rest of your program will use! An object should manipulate it's own data; not pass it to other parts of the program which then manipulate it.

Spectral Object Trap

An object is not a collection of methods you pass data to. Objects with no data are ghosts.

Multiple Personality Trap

An object should model only one object. Every data element and every method should contribute to that object.

OOD OOP       Resources