Stéphane Ducasse 1 Elements of Design - Unit of Reuse.

Slides:



Advertisements
Similar presentations
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Advertisements

Stéphane Ducasse 1 Design Points - Subclassing vs Subtyping Stéphane Ducasse
ITEC200 – Week03 Inheritance and Class Hierarchies.
Stéphane Ducasse«ChapterNr».1 Selected Design Patterns Design Patterns are recurrent solutions to design problems They are pros and cons We already saw:
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
Stéphane Ducasse 1 The Taste of Smalltalk.
Streams  Allows the traversal of a collection  Associated with a collection —If the collection is a Smalltalk collection: InternalStream —If the collection.
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
Some Basic Points on Classes
Stéphane Ducasse«ChapterNr».1 Elements of Design Instance initialization Enforcing the instance creation Instance / Class methods Instance variables /
Stéphane Ducasse 1 Objects to the Roots: Learning from beauty.
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
Chapter 10 Classes Continued
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
Stéphane Ducasse«ChapterNr».1 Basic Objects, Conditionals and Loops Booleans Basic Loops Overview of the Collection hierarchy— more than 80 classes: (Bag,
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
S.Ducasse Stéphane Ducasse 1 Elements of Design.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
S.Ducasse Stéphane Ducasse 1 Smalltalk Coding Idioms.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
S.Ducasse Stéphane Ducasse 1 Design Heuristics.
Stéphane Ducasse 1 Elements of Design - Simple Smells.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Stéphane Ducasse 1 Visitor.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Inheritance Semantics and Method Lookup.
Object-oriented programming and design 1 Object Identity = vs. == copying objects Value Object ValueWithHistory.
Stéphane Ducasse 1 Elements of Design - Inheritance/Compositio n.
Stéphane Ducasse 1 Singleton.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Seaside Tutorial. Outline ● Focus on components ● Rendering basics ● Rendering anchors with callbacks (next, previous) ● Call: of a built-in component.
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 12 Inheritance and Class Design 1.
Stéphane Ducasse 1 Strategy.
Object-Oriented Principals Dwight Deugo Nesa Matic
Stéphane Ducasse 1 Some Points on Classes.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Stéphane Ducasse 1 Abstract Classes.
S.Ducasse 1 Instance Initialization How to ensure that an instance is well initialized? Automatic initialize Lazy initialization Proposing the right interface.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Sections Inheritance and Abstract Classes
Interface, Subclass, and Abstract Class Review
CSC 205 Java Programming II
Programming Language Concepts (CIS 635)
Java Inheritance.
Review of Previous Lesson
Chapter 8 Class Inheritance and Interfaces
Introduction to Smalltalk
CSE 3302 Programming Languages
Presentation transcript:

Stéphane Ducasse 1 Elements of Design - Unit of Reuse

S.Ducasse 2 Methods are Units of Reuse Dynamic binding + methods = reuse in subclasses

S.Ducasse 3 Methods are Unit of Reuse

S.Ducasse 4 Example: Forced to Duplicate! Node>>computeRatioForDisplay | averageRatio defaultNodeSize | averageRatio := 55. defaultNodeSize := mainCoordinate /maximiseViewRatio. self window add: (UINode new with: (bandWidth * averageRatio / defaultWindowSize) … We are forced to copy the complete method! SpecialNode>>computeRatioForDisplay |averageRatio defaultNodeSize| averageRatio := 55. defaultNodeSize := mainCoordinate + minimalRatio / maximiseViewRatio. self window add: (UINode new with: (self bandWidth * averageRatio / defaultWindowSize) …

S.Ducasse 5 Self sends: Plan for Reuse Node>>computeRatioForDisplay | averageRatio defaultNodeSize | averageRatio := 55. defaultNodeSize := self defaultNodeSize. self window add: (UINode new with: (bandWidth * averageRatio / defaultWindowSize) Node>>defaultNodeSize ^ mainCoordinate / maxiViewRatio SpecialNode>>defaultNodeSize ^ mainCoordinate+minimalRatio/maxiViewRatio

S.Ducasse 6 Do not Hardcode Class Names Node>>computeRatioForDisplay |averageRatio defaultNodeSize| averageRatio := 55. defaultNodeSize := mainWindowCoordinate / maximiseViewRatio. self window add: (UINode new with: (bandWidth * averageRatio / defaultWindowSize).... We are forced to copy the method! SpecialNode>>computeRatioForDisplay |averageRatio defaultNodeSize| averageRatio := 55. defaultNodeSize := mainWindowCoordinate / maximiseViewRatio. self window add: (ExtendedUINode new with: (bandWidth * averageRatio / defaultWindowSize).

S.Ducasse 7 Class Factories Node>>computeRatioForDisplay | averageRatio | averageRatio := 55. self window add: self UIClass new with: (self bandWidth * averageRatio / self defaultWindowSize)... Node>>UIClass ^ UINode SpecialNode>>UIClass ^ ExtendedUINode

S.Ducasse 8 Hook and Template

S.Ducasse 9 Hook and Template Methods Hooks: place for reuse Template: context for reuse

S.Ducasse 10 Hook and Template Methods Templates: Context reused by subclasses Hook methods: holes that can be specialized Hook methods do not have to be abstract, they may define default behavior or no behavior at all. This has an influence on the instantiability of the superclass.

S.Ducasse 11 Hook / Template Example: Printing Object>>printString "Answer a String whose characters are a description of the receiver." | aStream | aStream := WriteStream on: (String new: 16). self printOn: aStream. ^aStream contents

S.Ducasse 12 Hook Object>>printOn: aStream "Append to the argument aStream a sequence of charactersthat describes the receiver." | title | title := self class name. aStream nextPutAll: ((title at: 1) isVowel ifTrue: ['an '] ifFalse: ['a ']). aStream print: self class

S.Ducasse 13 Overriding the Hook Array>>printOn: aStream "Append to the argument, aStream, the elements of the Array enclosed by parentheses." | tooMany | tooMany := aStream position + self maxPrint. aStream nextPutAll: '#('. self do: [:element | aStream position > tooMany ifTrue: [ aStream nextPutAll: '...(more)...)'. ^self ]. element printOn: aStream] separatedBy: [aStream space]. aStream nextPut: $)

S.Ducasse 14 Overriding False>>printOn: aStream "Print false." aStream nextPutAll: 'false'

S.Ducasse 15 Specialization of the Hook The class Behavior that represents a class extends the default hook but still invokes the default one. Behavior>>printOn: aStream "Append to the argument aStream a statement of which superclass the receiver descends from." aStream nextPutAll: 'a descendent of '. superclass printOn: aStream

S.Ducasse 16 Another Example: Copying Complex (deepCopy, veryDeepCopy...) Recursive objects Graph of connected objects Each object wants a different copy of itself No up-front solution

S.Ducasse 17 Hook Example: Copying Object>>copy " Answer another instance just like the receiver. Subclasses normally override the postCopy message, but some objects that should not be copied override copy. " ^self shallowCopy postCopy Object>>shallowCopy "Answer a copy of the receiver which shares the receiver's instance variables."

S.Ducasse 18 postCopy Object>>postCopy "Finish doing whatever is required, beyond a shallowCopy, to implement 'copy'. Answer the receiver. This message is only intended to be sent to the newly created instance. Subclasses may add functionality, but they should always do super postCopy first. " ^self

S.Ducasse 19 Sounds Trivial?

S.Ducasse 20 Hook Specialisation Bag>>postCopy "Make sure to copy the contents fully." | new | super postCopy. new := contents class new: contents capacity. contents keysAndValuesDo: [:obj :count | new at: obj put: count]. contents := new.

S.Ducasse 21 Guidelines for Creating Template Methods Simple implementation. Implement all the code in one method. Break into steps. Comment logical subparts Make step methods. Extract subparts as methods Call the step methods Make constant methods, i.e., methods doing nothing else than returning. Repeat steps 1-5 if necessary on the methods created