Object Oriented Design
- From chapter 4.
- Read it, through page 149.
- He begins by discussing design and the importance of design.
- Design becomes more important as
- You work on larger projects.
- You work on projects with multiple programmers.
- As you work on projects where the team changes as the project progresses.
- If PennWest is not a warning, I don't know what is.
- He begins with a number of examples you should read.
- He moves on to discuss abstraction and reuse
- He claims that these two are fundamental design rules to follow.
- Abstraction - Preserving the information that is relevant in a given context and forgetting the information that is irrelevant in that context. John Guttag.
- He gives an example of a TV.
- You know how to use a TV.
- But you don't know how a TV is implemented.
- The same is true of an object or abstract data type (ADT)
- An abstract data type is a data type whose properties (domain, and operations) are specified independently of any implementation.
- Let's consider a bug in our game.
- Hopefully we have implemented a functional decomposition by now.
- In my opinion, a major problem is that you have to know how a bug works at all levels of the implementation.
- In main,
int player1[BODY_PART_COUNT]{0}
- In Win, we step through the array.
- In PrintBug we step through the array...
- And none of the information is collected
- The essence of a bug is spread throughout the code.
- Neither of these is that bad in this code because
- It is a tiny program (200 lines)
- It is a simple program.
- And only one person is working on it.
- The real problem is an abstraction problem.
- I need to think of the bug, game rules, players, ... at all times during the program.
- By introducing abstraction I can avoid this.
- Think about the Die class.
- The programmer level (user) I only need to know the interface.
- At the implementation level I need to know the details of implementation.
- Thus the Die class is an abstraction.
- The Die class has been separated into an interface and an implementation
- Reuse
- "You don't build your own oven in which to bake your cookies. Nor do you churn you own butter, mill your own flower or form y our own chocolate chips."
- This is tough, you need to
- Design with the proper functionality.
- Design with the proper interface.
- Implement only the "correct" features.
- Don't provide too much extra
- Header and implementation files are useful.
- Again, my die class.
- Note it doesn't do an I/O.
- That will be program specific.
- It does provide the required interface, perhaps
- It might be nice to have a
CurrentValue()
function.
- Would adding that alter the current interface?
- Would that break programs that use the current Die class?