13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

12.6 Essential and N<strong>on</strong>essential285• Does this attribute or method bel<strong>on</strong>g here?If you find yourself specifying nearly identical methods in more than<strong>on</strong>e class, this should make you ask if the classes should have a comm<strong>on</strong>base class from which they should inherit, or if there should be a newunrelated class that they all share by compositi<strong>on</strong>.If the functi<strong>on</strong>ality is the same for a set of classes, and the classes arespecific instances of a more general type, the method should be <strong>on</strong> thegeneral class. For example, a changeName() method should probably be<strong>on</strong> Pers<strong>on</strong>, not <strong>on</strong> Employee or Customer, because the functi<strong>on</strong>ality is thesame for all three classes. By c<strong>on</strong>trast, a changeEmployeeNumber()method should be <strong>on</strong>ly <strong>on</strong> Employee. It should not be <strong>on</strong> Pers<strong>on</strong>, becausenot all Pers<strong>on</strong>s are Employees. There may also be methods that are comm<strong>on</strong>to both Employee and Customer types, but are radically different inimplementati<strong>on</strong>. For example, a changePassword() method mightchange a password in a system-wide LDAP server for an Employee, butmight just change a record in a Web site database for a Customer. This iseasily d<strong>on</strong>e by writing separate methods in each class.But should you add a changePassword() method <strong>on</strong> Pers<strong>on</strong>? If youwant to be able to call the method when treating either a Customer or anEmployee as a Pers<strong>on</strong>, then you should. But you d<strong>on</strong>’t have to implementthe method <strong>on</strong> Pers<strong>on</strong>. You can declare Pers<strong>on</strong>.changePassword as abstract,and then, if you call the method <strong>on</strong> a Pers<strong>on</strong>, it will call the correctmethod based <strong>on</strong> what type of Pers<strong>on</strong> (Employee or Customer) the Pers<strong>on</strong>is. Note that if a class c<strong>on</strong>tains any abstract methods, the class itself mustbe declared abstract and it cannot then be instantiated. Also note thatthis is often best accomplished not through abstract classes, but throughinterfaces (see Eckel, pp. 321–322).These are by no means the <strong>on</strong>ly c<strong>on</strong>siderati<strong>on</strong>s that come to bear <strong>on</strong> whatclasses to create and how to arrange and implement them, but they do representa good start. They are a foundati<strong>on</strong> <strong>on</strong> which you can build best practices outof your own experience and envir<strong>on</strong>ment.Whole books have been written <strong>on</strong> the topics of object-oriented analysisand object-oriented design. CRC cards are <strong>on</strong>ly <strong>on</strong>e part of an array of techniquesthat can be applied to OOA/OOD. The Unified Modeling Language(UML) is popular in many MIS circles. UML c<strong>on</strong>sists of a variety of differentdiagrams which are used to model parts of an object-oriented design. They are:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!