Object Oriented Design 1 Design Process n n
Object Oriented Design 1
Design Process n n The process leading to solving a problem with a program involves more that just “writing Java” Basic steps: q q n establish requirements design implementation testing Often people jump to “implementation” too fast 2
Requirements n Indicate what the final program should do q q n Often initial requirements are provided q n what to do – not how to do it e. g. user input, corresponding output, etc by a client, employer, instructor, … These requirements are often incomplete q q must be filled out and clarified careful specification of requirements can save a lot of time and effort later 3
Design n Deciding how a program will meet the requirements q n what programming language? functions needed? algorithms? That is, a software design determines: q q how the solution can be broken down into manageable pieces what each piece will do 4
Design (Object-Oriented) n n n What classes are needed? How will the classes interact? What methods will each class use? q n n How will the methods perform their jobs? UML diagrams can help Again… time spent on a good design can save a lot of time later… 5
Implementation n n Involves translating the design into code Important decisions should have all been made in the design q n Implementation is the least creative step – the details are largely technical Focus on coding details, including style and documentation 6
Testing n n n Mistakes are inevitable at all 3 previous steps Must do black-box testing to see if program meets the requirements Also should test smaller parts of code during implementation q n much easier to debug small bits of code Some testing can be automated 7
Object Design n We focus on object-oriented design q q n not the only way to design… … but it’s widely used The main step in object oriented design is specifying the classes that will be used to build the program q If the classes are well-designed, it should be easy to put them together into a complete program 8
Identifying Classes and Objects n The classes may be part of a class library, reused from a previous project, or newly written n One way to identify potential classes is to identify the objects discussed in the requirements n Objects are generally nouns, and the services that an object provides are generally verbs 9
Example n A partial description of requirements: The user selects television programs to record. A recording will have a start and end time specified. The quality of recording can vary, depending on the user’s needs. Each recording will be stored on disk and can be accessed from a menu of programs. n With noun phrases highlighted 10
Example (cont’d) n In this program, we might create classes for q q q q user TV program recording time quality user’s needs disk (file? ) menu 11
But… n Unclear from the specification we have q q user: do we have multiple users? (which would require a User class)… or is it just referring to a user interface recording: does that need to be a separate object… or just a Program with a flag saying “recorded” user’s needs == preferences? ? if so, what kind? … 12
Problems Details n We need a lot of clarification in the example problem statement q n not surprising… one paragraph The level of detail in a problem will vary q q often requires intelligent interpretation or requires discussion with the “client” 13
Identifying Classes and Objects n Remember that a class represents a group (classification) of objects with the same behaviors n Generally, classes that represent objects should be given names that are singular nouns n Examples: Coin, Student, Message n A class represents the concept of one such object n We are free to instantiate as many of each object as needed 14
Identifying Classes and Objects n Sometimes it is challenging to decide whether something should be represented as a class n For example, should an employee's address be represented as a set of instance variables or as an Address object n The more you examine the problem and its details the more clear these issues become n When a class becomes too complex, it often should be decomposed into multiple smaller classes to distribute the responsibilities 15
Identifying Classes and Objects n We want to define classes with the proper amount of detail n For example, it may be unnecessary to create separate classes for each type of appliance in a house n It may be sufficient to define a more general Appliance class with appropriate instance data n It all depends on the details of the problem being solved 16
Identifying Classes and Objects n Part of identifying the classes we need is the process of assigning responsibilities to each class n Every activity that a program must accomplish must be represented by one or more methods in one or more classes n We generally use verbs for the names of methods n In early stages it is not necessary to determine every method of every class – begin with primary responsibilities and evolve the design 17
Designing Classes n n There is always a balance in designing classes Don’t create a new class for everything you imagine q q q some things can be represented by built-in types some things just don’t make sense as a class some nouns can be combined in a single class 18
Example (cont’d) n n “user” can refer to the possible input… no class needed “quality” might just be a number 1 -100. q n … but maybe we need methods for a quality that ints don’t have… then we would need a class There are classes in the standard library for “times”… will they do? 19
Assigning Responsibilities n Once you have decided what the classes will be, what will each one do? q q q n What methods will they have? What data members will they have? Don’t need to specify every single detail in design What parts of the functionality will each class be “responsible” for? 20
Methods and Responsibilities n The methods will have to implement the individual behaviours of the program q q n correspond to verbs need to decide which class gets each function e. g. to initiate a recording, what class do we access? q q q program. record()? channel. record()? user. set. Recording()? 21
Static Class Members n Recall that a static method is one that can be invoked through its class name n For example, the methods of the Math class are static: result = Math. sqrt(25) n Variables can be static as well n Determining if a method or variable should be static is an important design decision 22
The static Modifier n We declare static methods and variables using the static modifier n It associates the method or variable with the class rather than with an object of that class n Static methods are sometimes called class methods and static variables are sometimes called class variables 23
Static Variables n Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists private static float price; n Memory space for a static variable is created when the class is first referenced n All objects instantiated from the class share its static variables n Changing the value of a static variable in one object changes it for all others 24
Static Methods in Design n n A static method/variable is associated with a class not an object Consider a Dog object q q object data: colour of fur, height, weight, … static data: number of legs, life span, … object methods: eat, run, bite, … static method: list. All. Breeds(), get. Dog. Years(int n) 25
- Slides: 25