OO design Classes, Responsibilities, Collaborations (CRC) 1
bank accounts the system to be modelled: bank accounts with differing fee structures purpose: evaluate different account types with respect to cost easy example to start with what's being 'simulated' is a calculation 2
what classes will we need? BankAccount the class is used to make different bank accounts what are the responsibilities of an account? accept individual transactions keep in mind: different banks have different categories of transactions 3
more responsibilities instance variables -- define state of the account have to remember what will be needed to calculate a fee all accounts have a balance BankAccount can take responsibility for deposit, but not debits -- why? deposits don't usally involve a fee 4
subclassing differing responsiblities imply different classes similarities imply sub-classing make each type of account a separate sub-class of BankAccount 5
transactions responsibilities for Transactions class? operations such as "next transaction" methods related to its internal structure -- suggests this is a datatype based on some built-in datatype such as OrderedCollection 6
collaborations what classes of object does a BankAccount need in order to function? Transactions BankAccounts "collaborate" with Transactions (collaboration can be asymmetric) 7
an overview UML class diagram BankAccount balance minimum balance debit: deposit: fee Transactions BofM planfee waiverbalance : 8
OO Programming in Smalltalk 9
object-oriented programming objects = data structures with local state variables and encapsulated code (methods) objects have limited vision and limited effect on other objects; this increases comprehensibility reduces the amount of context needed to understand what happens objects only receive and respond to messages. They don't usually know about other objects' state variables or code. 10
Smalltalk example Example: suppose we have an object called 'square'. inside the 'square' object, define a variable 'side': side := 10. From outside the square, to find out its size, send it a message, e. g. size size ^side value of square size is 10 11
notational variants Smalltalk-80 assignment operator printed: input: _ Visualworks assignment operator printed & input: := 12
talking in circles Example: an object called circle'. inside the circle' object, a variable called radius : radius := 10. From outside the circle, to find out its size, send it a message, size size ^radius Value of circle size is 10 13
what s happening? How does anything happen in a Smalltalk program? Objects respond to messages What s the object and message in 3 +2? 3 is an object which (like all numbers) reacts to the message + 2. 14
classification Objects are classified into classes with similar properties. This avoids redundant specification. You don't have to describe the structure and behaviour of each individual object in complete detail. 15
instance creation Classes are like types or kinds better metaphor: classes are factories for making instances instance = object the object s properties are defined in its class 16
system image A Smalltalk system development and application environment combined Each user can construct their own system by starting with a default system and modifying it = user s (system) image you need an "engine" (virtual machine) + an image to run Smalltalk code 17
image size about 1000 to 2000 classes in a default image 20,000 to 200,000 objects in an image in active use 18
inheritance & reflexivity An object inherits the properties of the class of which it is an instance as well as the superclasses of its class. Problem: in order to program in Smalltalk,you have to know the major classes of objects. Tools: System browser and debugger which lets you inspect everything, including the internals of the Smalltalk system! 19
classes (structure) & methods (behaviour) 20
class hierarchies what is the function of a class hierarchy (inheritance)? factor out commonalities in state and behavior common variables move up in the hierarchy shared values become values of class variables common method selectors move up the hierarchy 21
selectors simple message (no argument) size selector with argument + 2 initialbalance: amount size: 10 border: red width: 2 this is a message what's the selector? 22
selectors, messages, methods selector = pattern of invocation message = selector + arguments method selected depends on the class to which the message is sent selector can be default method, defined in superclass specialization: override default in a subclass selector can be abstract, only implemented in subclass 23
expressions review handout on Smalltalk expressions 24