C H A P T E R 10 Object-Oriented Design Principles Devotion to the facts will always give the pleasures of recognition; adherence to the rules of design, the pleasures of order and certainty. —Kenneth Clark How can I qualify my faith in the inviolability of the design principles? Their virtue is demonstrated. They work. —Edgar Whitney Now that we’ve spent some time looking at object-oriented analysis and design, let's recapitulate some of what we’ve already seen and add some more pithy prose. First, let's talk about some common design characteristics. First, designs have a purpose. They describe how something will work in a context, using the requirements (lists of features, user stories, and use cases) to define the context. Second, designs must have enough information in them so that someone can implement them. You need enough details in the design so that someone can come after you and implement the program correctly. Next, there are different styles of design, just like there are different types of house architectures. The type of design you want depends on what it is you’re being required to build. It depends on the context (see, we’re back to context); if you’re an architect, you’ll design a different kind of house at the sea shore than you will in the mountains. Finally, designs can be expressed at different levels of detail. When building a house, the framing carpenter needs one level of detail, the electrician and plumber another, and the finish carpenter yet another. There are a number of rules of thumb about object-oriented design that have evolved over the last few decades. These design principles act as guidelines for you the designer to abide by so that your design ends up being a good one, easy to implement, easy to maintain, and one that does just what your customer wants. We’ve looked at several of them already in previous chapters, and here I’ve pulled out ten fundamental design principles of object-oriented design that are likely to be the most useful to you as you become that designer extraordinaire. I’ll list them here and then explain them and give examples in the rest of the chapter. J. Dooley, Software Development and Professional Practice © John Dooley 2011 115 CHAPTER 10 OBJECT-ORIENTED DESIGN PRINCIPLES Our List of Fundamental Object-Oriented Design Principles Here are the ten fundamental principles: 1. Encapsulate things in your design that are likely to change. 2. Code to an interface rather than to an implementation. 3. The Open-Closed Principle (OCP): Classes should be open for extension and closed for modification. 4. The Don’t Repeat Yourself Principle (DRY): Avoid duplicate code. Whenever you find common behavior in two or more places, look to abstract that behavior into a class and then reuse that behavior in the common concrete classes. Satisfy one requirement in one place in your code. 5. The Single Responsibility Principle (SRP): Every object in your system should have a single responsibility, and all the objects services should be focused on carrying out that responsibility. Another way of saying this is that a cohesive class does one thing well and doesn’t try to do anything else. This implies that higher cohesion is better. It also means that each class in your program should have only one reason to change. 6. The Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types. (in other words, inheritance should be well designed and well behaved.) 7. The Dependency Inversion Principle (DIP): Don’t depend on concrete classes; depend on abstractions. 8. The Interface Segregation Principle (ISP): Clients shouldn’t have to depend on interfaces they don’t use. 9. The Principle of Least Knowledge (PLK) (also known as the Law of Demeter ): Talk only to your immediate friends. 10. The Principle of Loose Coupling: Objects that interact should be loosely coupled with well-defined interfaces. As you probably notice, there’s some overlap here, and one or more of the design principles may depend on others. That’s okay. It’s the fundamentals that count. Let’s go through these one at a time. 116