What Are Classes?


There might be five electromagnetic doors in the building where the security system is deployed, all located in different areas. Each door is thus a separate object with a different physical address. However, all doors share the common behavior of needing to be secured, so they all can be classified similarly. A class provides a way of defining commonality for a related grouping of objects. It is a template, or blueprint, for constructing new objects.

The OO security system might define a Door class. The Door class specifies that all Door objects must provide secure and release (unlock) behavior. Furthermore, each Door object should retain its own location. A picture of this Door class is shown in Figure 2.

Figure 2. The Door Class


The class box shows the name of the class in the first (topmost) compartment. The second compartment lists the attributesthe information that each Door object will store. The third compartment lists the behaviors that each Door object supportsthe messages that each door will respond to.

Figure 3. Door Objects in Memory


In an object-oriented programming language, you use the Door class as a basis for creating, or instantiating, new objects. Each object that you instantiate is allocated a unique area in memory. Any changes you make to an object are exclusive of all other objects in memory.

A class diagram is used to represent the structure of an object-oriented system. It shows how classes relate to, or associate with, each other. A class diagram can provide a quick visual understanding of a system. You can also use it as one of the primary indicators of the quality of a system's design.

The basis of all relationships in a class diagram is the association. An association between two classes is formed when one class is dependent on the other class or when both classes are dependent on each other. Class A is dependent on class B if class A works only when class B is present.

A main reason class A becomes dependent upon class B is that class A sends one or more messages to class B. Since the Security object sends a message to the DoorController object, the Security class is dependent upon the DoorController class.

You show a one-way dependency between two classes with a navigable association (see Figure 4).

Figure 4. Class Dependency




Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net