190 likes | 306 Views
Systems Analysis & Design Methods. VI OO first rules What is Object Orientation ?. Books:. Object Oriented Software Construction, Bertrand Meyer. Why rules ?. Consistency If everyone uses the same rules, different parts will work together well.
E N D
Systems Analysis & Design Methods VI OO first rules What is Object Orientation ?
Books: • Object Oriented Software Construction, Bertrand Meyer VI OO first rules What is Object Orientation ?
Why rules ? • Consistency • If everyone uses the same rules, • different parts will work together well. • different team members will understand each other designs better, less chance of misunderstandings • Less ways of solving the same problem • Waste less time discussing what the best solution is. VI OO first rules What is Object Orientation ?
Why rules ? (continued) • Better code: • = simpler, cheaper systems, that can be adapted more quickly Remarks about this: If a system is constructed with other rules or in a non OO way and it is working well, it deserves your respect anyway. VI OO first rules What is Object Orientation ?
Difference object-class • An object is an instance of a class. • A class describes some facts about its objects. • Example: If Antwerp is an object, then City could be its class. E.g. The Whitten book mixes up objects and classes. The writers also seem to be confused about the difference between objects and subclasses. VI OO first rules What is Object Orientation ?
Difference object-class • Exercise: Fill in the blancs: VI OO first rules What is Object Orientation ?
Complete class • A class at conception/analysis level should contain its attributes and its non- trivial methods. • Why ? : attributes and methods of a class change together and so they belong together. • Example: Account -balance: double +withDraw(amount: double) +transfer(amount: double, to: BankAccount) +getBalance(): double VI OO first rules What is Object Orientation ?
Complete class • Exercise: What’s weird about this design ?: Transactions Account -balance: double +withDraw(from: Account, amount: double) +getBalance(): double +setBalance(amount: double) +transfer(amount: double, from: Account, to:Account) VI OO first rules What is Object Orientation ?
Encapsulation/Data abstraction • If a class X is a client of class Y, the attributes of Y can be changed by X only by using methods explicitly created for this purpose. ( The attributes are generally private, the methods intended for clients have public or package visibility. Other methods are not public. ) VI OO first rules What is Object Orientation ?
Modifiable properties • A mutable (modifiable) property is represented by an attribute. • An immutable property can give rise to subclasses if the number of values the property can take is limited. • Why? • attributes are modifiable but an object cannot change its class. • Example: Person Person -maritalState: MaritalState +getMaritalState(): MaritalState … MariedPerson Bachelor VI OO first rules What is Object Orientation ?
Modifiable properties • Exercise 1 Suppose we want to model that some birds belong to a species that can swim, and others don’t. Is this okay ? Bird -canSwim: bool … VI OO first rules What is Object Orientation ?
Modifiable properties • Exercise 2 Is this okay ? Person -birthDate: Calendar … +getAge() : double +marry(Person partner) … VI OO first rules What is Object Orientation ?
Common code • Common code within a class should be put into one method which is then called at several places. • Why ? • The same modifications is preferably done only in one place -> otherwise: bug risk by forgetting one place • Example: see next slide VI OO first rules What is Object Orientation ?
Common code • Example VI OO first rules What is Object Orientation ?
Common code • Exercise: Enhance the code on the left: VI OO first rules What is Object Orientation ?
Command-Query separation • 1. A method that returns a result, shouldn’t change any attribute. (query) • 2. A method that changes an attribute, should NOT return a result. (command) • Why ? • In general: A method should have one single responsibility. • You don’t expect a query to change an attribute (side effect). Sometimes you just want to query. If you do, and the method has a side effect, you have to undo the undesired side effect, which is awkward. • Example. Next slide. VI OO first rules What is Object Orientation ?
Command-Query separation VI OO first rules What is Object Orientation ?
Command-Query separation:(continued) When mixing commands and queries, you sometimes can save a line of code or two… BUT… VI OO first rules What is Object Orientation ?
Command-Query separation:(continued) … what happens if you don’t need the side effect: VI OO first rules What is Object Orientation ?