Providing a default value. 1. S.Ducasse. â¤&.3. Provider Responsibility. This is the responsibility of the class to provide well-formed object. A client should not ...
Define an instance method that initializes the instance variables and override new to invoke it. (1&2)! ! ! (3)! ! (4)!
! !
Packet class>>new “Class Method” ! ^ super new initialize! !
! ! !
Packet>>initialize! “Instance Method” super initialize. contents := ‘default message’
Object class Object
Node name accept: aPacket send: aPacket
Node class new withName: aString instance of
Workstation class
! Packet new (1-2) => aPacket initialize (3-4) => returning aPacket but initialized!
Workstation originate: aPacket accept: aPacket name
Reminder: You cannot access instance variables from a class
S.Ducasse
instance of
instance of aWorkstation withName: 'BigMac'
S.Ducasse
13
Instance Initialization
14
Lazy Initialization
How to ensure that an instance is well initialized?
!"#$
When some instance variables are: - not used all the time - consuming space, difficult to initialize because depending on other - need a lot of computation
Automatic initialize Lazy initialization Proposing the right interface Providing a default value
Use lazy initialization based on accessors Accessor access should be used consistently!
S.Ducasse
15
!"#$
!"#$
S.Ducasse
16
Lazy Initialization Example
!"#$
Better
A lazy initialization scheme with default value ! Packet>>contents !
A lazy initialization scheme with computed value Dummy>>ratio !
Packet>>contents !
aPacket contents or self contents
!"#$
ratio isNil ifTrue: [ratio := self heavyComputation]
S.Ducasse
S.Ducasse
17
Instance Initialization
18
Strengthen Instance Creation Interface
How to ensure that an instance is well initialized?
Problem: A client can still create aPacket without address. Solution: Force the client to use the class interface creation.
Automatic initialize Lazy initialization Proposing the right interface Providing a default value
Providing an interface for creation and avoiding the use of new: Packet send: ‘Hello mac’ to: #Mac Packet class>>send: aString to: anAddress ^ self new contents: aString ; addressee: anAddress ; yourself
S.Ducasse
19
!"#$
S.Ducasse
20
!"#$
Examples of Instance Initialization
!"#$
Another Example
step 1. SortedCollection sortBlock: [:a :b| a name < b name]
Collection class>>with: anObject "Answer a new instance of a Collection containing anObject."
SortedCollection class>>sortBlock: aBlock "Answer a new instance of SortedCollection such that its elements are sorted according to the criterion specified in aBlock." !
!"#$
!"#$
22
Providing a Default Value
How to ensure that an instance is well initialized?
But you cannot simply chain the calls...so use initialize
!"#$
Do not invoke a super with a different method selector.
A>>initialize super initialize.
It’s bad style because it links a class and a superclass.
self doThat; andThat; end
B>>initialize
It makes the code difficult to understand
super initialize. self andFoo.
S.Ducasse
^ super new doThatAndThatEnd
B class>>forceClientInterface !
!
^ self doThat; andThat; end
A class>>new
Conclusion: Never override basic* methods else you will not be able to invoke them later
Even Better...Use initialize
^ self basicNew ???
Solution: Define the initialization behavior on the instance side A>>doThatAndThatEnd !
S.Ducasse
^ super new doThat; andThat; end
This is dangerous in case the software evolves.
31
S.Ducasse
32
Example
!"#$
Packet class>>new self error: 'Packet should be created using send:to:' ! Packet class>>send: aString to: anAddress ! ^ super new contents: aString ; addressee: anAddress Use basicNew and basicNew:
S.Ducasse
33
Super is static!
!"#$
With the super foo: A foo bar
^ 10 ^ self foo
B foo bar
^ 100 ^ super foo
C foo
S.Ducasse
^ 50
A new bar -> 10 B new bar -> 10 C new bar -> 10
Without the super foo: A new bar -> 10 B new bar -> 100 C new bar 34
creating an employee object, two pieces of information are absolutely required: a name and a social security number. If we create the instance then set the ...
TOYOTA MOTOR CORPORATION Overseas Customer Service Technical Division ... Then perform the whole procedure again from step (a) to step (k). ... Page 2 ...
garderie subventionnée et il en paye cinquante pour-cent (50%) des coûts. ... semblable pour les autres employé-e-s exclus au Bureau national. Stony Mountain.
Enable Interrupts to the 8051 CPU. CPUIRQENB1. PCI Master Abort. Received Interrupt. Enable. 1. This allows PCI Master Aborts to generate an interrupt (only ...
method combining the Canny edge detection and the Hough transform. Concerning the 3D ... The lines are represented in polar coordinates (Ï, θ). In step 3, a ...
sents an optimal control problem, whose aim consists in finding a control law enabling the vehicle to join a final point of the 3D space considering prescribed ...
a manual intervention or with a more sophisticated equip- ment. Hence, in ... The reduction of the pos- sible features in .... CONCLUSION AND FUTURE WORK.
Mise en place d'un traitement par messagerie électronique. Afin de parfaire l'organisation de l'audiencement correctionnel, de limiter autant que faire se peut.
Abstract pmpd is a collection of objects for pure data (pd). These objects provide real-time simulations, especially physical behaviors. pmpd can be used to ...
5 juil. 2012 - Internet, et de supprimer de celui-ci et de tous les autres sites Internet ainsi que dans la presse écrite et audiovisuelle toute référence à ...
This Le Procas Des Treize En Premiare Instance 56 Aoat Pdf file begin with Intro, Brief Discussion until the. Index/Glossary page, look at the table of content for additional information, if provided. It's going to discuss primarily concerning the pr
27 nov. 2009 - du Code de la Propriété Intellectuelle ainsi que 1382 du Code Civil, ... des plots métalliques comprennent un bloc de jonction de dimensions ...