Section 8.7. Principles of Object-Oriented Design


[Page 396 (continued)]

8.7. Principles of Object-Oriented Design

To conclude this chapter, it will be helpful to focus briefly on how the examples we have seen address the various object-oriented design (OOD) principles we set out at the beginning of the book.

  • Divide-and-Conquer Principle. All of the problems tackled in this chapter have been solved by dividing them into several classes, with each of the classes divided into separate methods. The very idea of a class hierarchy is an application of this principle.

  • Encapsulation Principle. The superclasses in our designs, Cipher and TwoPlayerGame, encapsulate the features of the class hierarchy that are shared by all objects in the hierarchy. The subclasses CaesarCipher and OneRowNim encapsulate features that make them distinctive with the class hierarchy.

  • Interface Principle. The Java interfaces we have designed, IPlayer, CLUIPlayableGame, and UserInterface, specify clearly how various types of related objects will interact with each other through the methods contained in the interfaces. Clean interfaces make for clear communication among objects.

  • Information-Hiding Principle. We have continued to make consistent use of the private and public qualifiers, and have now introduced the protected qualifier to extend this concept. The inheritance mechanism gives subclasses access to protected and public elements of their superclasses.

  • Generality Principle. As you move down a well-designed class hierarchy, you go from the more general to the more specific features of the objects involved. The abstract encode() method specifies the general form that encoding will take, while the various implementations of this method in the subclasses provide the specializations necessary to distinguish, say, Caesar encoding from transpose encoding. Similarly, the abstract makeAMove() method in the IPlayer interface provides a general format for a move in a two-player game, while its various implementations provide the specializations that distinguish one game from another.

  • Extensibility Principle. Overriding inherited methods and implementing abstract methods from either an abstract superclass or a Java interface provide several well-designed ways to extend the functionality in an existing class hierarchy. Extending a class is a form of specialization of the features inherited from the superclass.

  • Abstraction Principle. Designing a class hierarchy is an exercise in abstraction, as the more general features of the objects involved are moved into the superclasses. Similarly, designing a Java interface or an abstract superclass method is a form of abstraction, whereby the signature of the method is distinguished from its various implementations.

These, then, are some of the ways that the several examples we have considered and this chapter's discussion have contributed to a deepening understanding of object-oriented design.




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

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