SlideShare a Scribd company logo
Introduction Summary References
CONTAINER CLASSES
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 21, 2015
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
OUTLINE I
INTRODUCTION
SUMMARY
REFERENCES
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
INTRODUCTION
We will examine how object-oriented programming
techniques can be applied to the problem of creating
container, or collection classes.
Containers in Dynamically Typed Languages.
The Tension between Reuse and Strong Typing.
Object Oriented Solutions.
Substitution and Downcasting.
Substitution and Overriding.
Templates.
Collection Traversals.
Iterators.
Visitors.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
CONTAINERS IN DYNAMICALLY TYPED LANGUAGES
Collection classes are simple to write in dynamically typed
languages, Since all variables are polymorphic.
Contains simply hold values in variables (which can hold
anything), and when they are removed from the container
they can be assigned to any variable.
Dynamically typed languages have historically come with a
rich set of container classes in their standard library.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
TENSION BETWEEN STRONG TYPING AND REUSE
The situation is very different when we move to statically
typed languages.
There, the strong typing can get in the way of software
reuse.
THE TENSION
lstsetlanguage=C++
type
Link = Record
value : integer
nextelement : ^ Link
end ;
What happens when we need a linked list of doubles? or of
a user defined type?
The strong typing is getting in the way of software reuse.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
CAN OO TECHNIQUES SOLVE THIS?
Can we bring any of the OO techniques (subsitution,
polymorphism) into the solution of this problem? We
examine three techniques:
Using Substitution and Downcasting.
Using Substitution and Overriding.
Using Templates (or Generics).
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
USING SUBSTITUTION AND DOWNCASTING
In Java and other languages, (almost) all values can be
stored in a variable of type Object.
Therefore, we write containers that store Object values.
Problem. Requires a cast when a value is removed from
the container.
SUBSTITUTION
Vector aVector = new Vector ( ) ;
Cat Felice = new Cat ( ) ;
aVector . addElement ( Felice ) ;
. . .
/ / cast used to convert Object value to Cat
Cat animal = ( Cat ) aVector . elementAt ( 0 ) ;
Worse problem, typing errors are detected when a value is
removed from the collection, not when it is inserted.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
HETEROGENEOUS COLLECTIONS
Because any value can be stored in an Object, this allows
heterogeneous collections; although the actual type must
be checked when a value is removed:
EXAMPLE
Stack stk = new Stack ( ) ;
stk . addElement (new Cat ( ) ) ;
stk . addElement (new Dog ( ) ) ;
/ / . . . adding more values
/ / now do something with Cat values
i f ( stk . peek ( ) instanceof Cat ) {
/ / do conversion to Cat
Cat aCat = ( Cat ) stk . pop ( ) ;
/ / . . also do something with cat values
/ / now do something with Dog values
} else i f ( stk . peek ( ) instanceof Dog) {
/ / do conversion to Dog
Dog aDog = (Dog) stk . pop ( ) ;
/ / . . do something with Dog value
}
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
USING CLASSES AS OBJECTS TO DO TYPE CHECKING
In languages in which classes are objects, and have the
ability to tell if an object is an instance, the container can
hold the class object that represents its type:
EXAMPLE
var
stack : TStack ;
aCat : TCat ;
aDog : TDog ;
begin
/ / create a stack that can hold only TCat values
stack := TStack . Create ( TCat ) ;
stack . push ( aCat ) ; / / ok
stack . push (aDog ) ; / / w i l l raise exception
. . .
end
Allows typing errors to be caught on insertion, but uses more
space and time.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
STRONG PRIMITIVE VALUES
In some languages (Java, Delphi) primitive types are not
objects.
These values cannot be stored using this scheme.
Thus, languages introduce wrapper classes that do
nothing but hold a primitive value.
EXAMPLE
Vector aVector = new Vector ( ) ;
aVector . add (new Integer ( 1 7 ) ) ; / / wrap up p r i m i t i v e i n t as Integer
. . .
Integer a = aVector . elementAt ( 1 ) ;
int x = a . intValue ( ) ; / / must subsequently be unwrapped
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
USING SUBSTITUTION AND OVERRIDING
In certain situations we can eliminate the downcast,
replacing it with overriding.
This only works, however, if you can predict ahead of time
what operations you want to do on a collection.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
AN EXAMPLE, JAVA WINDOW EVENTS
A good example is the way that window events are handled
in Java.
Each window maintains a list of listeners, each listener
must implement a fixed interface (WindowListener).
EXAMPLE
public class CloseQuit extends WindowAdapter {
/ / execute when the user c l i c k s in the close box
public void windowClosing ( WindowEvent e ) {
System . e x i t ( 0 ) ; / / h a l t the program
}
}
When an event occurs the window simply runs down the list of
listener, invoking the appropriate method in each. No
downcasting required.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
A THIRD ALTERNATIVE – GENERICS
CODE
template <class T> class L i s t {
public :
void addElement (T newValue ) ;
T firstElement ( ) ;
private :
Link ∗ f i r s t L i n k ;
private class Link { / / nested class
public :
T value ;
Link ∗ nextLink ;
Link (T v , Link ∗ n ) : value ( v ) , nextLink ( n ) { }
} ;
} ;
Allow for both strong typing and reuse.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
COLLECTION TRAVERSAL
Here is another difficult problem.
How do you allow access to elements of a collection
without exposing the internal structure?
Consider the conventional solution:
EXAMPLE
var
aList : L i s t ; (∗ the l i s t being manipulated ∗)
p : Link ; (∗ a pointer for the loop ∗)
begin
. . .
p := aList . f i r s t L i n k ;
while ( p <> n i l ) do begin
w r i t e l n ( p . value ) ;
p := p ^ . nextElement ;
end ;
Needed to expose the link type, and the field nextElement.
Can we avoid this problem?
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
There are two common solutions:
Create an iterator; an object that facilitates access to
elements and enumeration over the collection.
The vistor. Bundle the action to be performed as an object,
and pass it to the collection.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
ITERATOR
An iterator is an object that has two major responsibilities:
Provide access to the current element.
Provide a way to move to the next element.
Different languages use different interfaces, but the basic
idea is the same.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
AN ENUMERATION LOOP IN JAVA
EXAMPLE
/ / create the i t e r a t o r object
Enumeration e = aList . elements ( ) ;
/ / then do the loop
while ( e . hasMoreElements ( ) ) {
Object obj = e . nextElement ( ) ;
/ / . . . do something with obj
}
Interface consists of two methods, hasMoreElements and
nextElement.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
AN ITERATION LOOP IN C++
EXAMPLE
/ / create s t a r t i n g and stopping i t e r a t o r s
l i s t : : i t e r a t o r s t a r t = aList . begin ( ) ;
l i s t : : i t e r a t o r stop = aList . end ( ) ;
/ / then do the loop
for ( ; s t a r t != stop ; s t a r t ++ ) {
s t r i n g value = ∗ s t a r t ; / / get the value
/ / . . . do something with the value
}
Interface consists of three operations; comparison, increment,
and dereference.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
ITERATORS AND ENCAPSULATION
Note that the iterator must almost always have detailed
knowledge of the internal data structure.
Wasn’t our goal to hide these details?
Not exactly.
The iterator is usually developed by the same programmer
creating the data structure.
It can share detailed knowledge with the data structure.
And using the iterator does not require any knowledge of
how the iterator is implemented.
Often friends (in C++) or nested classes (in Java) can be
used to help the iterator share information with the
container.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
ALTERNATIVE, THE VISITOR
An alternative to iterators is the idea of a visitor.
Requires the ability to bundle the action to be performed
into an object, and hand it to the collection.
This is most common technique used in Smalltalk.
aList do: [:x | (’element is’ + x) print ].
The block is passed as argument to the list, which turns
around and executes the block on each element.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
THE VISITOR IN C++ IN THE STL
EXAMPLE
class printingObject {
public :
void operator ( ) ( int x )
{
cout << " value i s " << x << endl ;
}
} ;
printingObject p r i n t e r ; / / create an instance of the function object
for_each ( aList . begin ( ) , aList . end ( ) , p r i n t e r ) ;
The function for_each passes the object to element element in
the collection.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
SUMMARY
Collection Classes are easy to write in dynamically typed
languages.
In statically typed languages, there is a conflict between
reuse and strong typing.
Three approaches to overcoming this include.
Using substitution and downcasting.
Using substitution and overriding.
Using generics.
A related problem, looping over elements.
The iterator solution.
The visitor solution.
Container Classes Roaming Researchers, Inc. cbna
Introduction Summary References
REFERENCES
Images and content for developing these slides have been
taken from the follwoing book with the permission of the
author.
An Introduction to Object Oriented Programming, Timothy
Budd.
This presentation is developed with Beamer:
Szeged, dove.
Container Classes Roaming Researchers, Inc. cbna

More Related Content

What's hot

Oop2011 actor presentation_stal
Oop2011 actor presentation_stalOop2011 actor presentation_stal
Oop2011 actor presentation_stal
Michael Stal
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
Michael Stal
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
Abhijeet Dubey
 
(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii
Nico Ludwig
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
Bartosz Kosarzycki
 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphism
umesh patil
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
Michael Stal
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
Michael Stal
 
DIWE - Programming with JavaScript
DIWE - Programming with JavaScriptDIWE - Programming with JavaScript
DIWE - Programming with JavaScript
Rasan Samarasinghe
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programming
Riccardo Cardin
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)
Andrew Petryk
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
Eugene Lazutkin
 
Objective c
Objective cObjective c
Objective c
ricky_chatur2005
 
Logic-based program transformation in symbiosis with Eclipse
Logic-based program transformation in symbiosis with EclipseLogic-based program transformation in symbiosis with Eclipse
Logic-based program transformation in symbiosis with Eclipse
Coen De Roover
 
C#4.0 features
C#4.0 featuresC#4.0 features
Java Serialization Deep Dive
Java Serialization Deep DiveJava Serialization Deep Dive
Java Serialization Deep Dive
Martijn Dashorst
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
Manav Prasad
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
Garik Kalashyan
 

What's hot (20)

Oop2011 actor presentation_stal
Oop2011 actor presentation_stalOop2011 actor presentation_stal
Oop2011 actor presentation_stal
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphism
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
 
DIWE - Programming with JavaScript
DIWE - Programming with JavaScriptDIWE - Programming with JavaScript
DIWE - Programming with JavaScript
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programming
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
Objective c
Objective cObjective c
Objective c
 
Class
ClassClass
Class
 
Logic-based program transformation in symbiosis with Eclipse
Logic-based program transformation in symbiosis with EclipseLogic-based program transformation in symbiosis with Eclipse
Logic-based program transformation in symbiosis with Eclipse
 
C#4.0 features
C#4.0 featuresC#4.0 features
C#4.0 features
 
Java Serialization Deep Dive
Java Serialization Deep DiveJava Serialization Deep Dive
Java Serialization Deep Dive
 
Presentation_1370675757603
Presentation_1370675757603Presentation_1370675757603
Presentation_1370675757603
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
 

Similar to Container Classes

Generics
GenericsGenerics
Generics
adil raja
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
Martin Odersky
 
Kotlin Advanced - language reference for Android developers
Kotlin Advanced - language reference for Android developers Kotlin Advanced - language reference for Android developers
Kotlin Advanced - language reference for Android developers
STX Next
 
Design patterns with Kotlin
Design patterns with KotlinDesign patterns with Kotlin
Design patterns with Kotlin
Alexey Soshin
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
Ratnala Charan kumar
 
scalar.pdf
scalar.pdfscalar.pdf
scalar.pdf
Martin Odersky
 
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
Sudarshan Dhondaley
 
Designpatterns illustrated
Designpatterns illustratedDesignpatterns illustrated
Designpatterns illustrated
Nhat Vo Van
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
Jack Nutting
 
PATTERNS09 - Generics in .NET and Java
PATTERNS09 - Generics in .NET and JavaPATTERNS09 - Generics in .NET and Java
PATTERNS09 - Generics in .NET and Java
Michael Heron
 
Java mcq
Java mcqJava mcq
Java mcq
avinash9821
 
Unit ii
Unit   iiUnit   ii
Unit ii
donny101
 
Traits inscala
Traits inscalaTraits inscala
Traits inscala
Knoldus Inc.
 
Traits in scala
Traits in scalaTraits in scala
Traits in scala
Knoldus Inc.
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
Akshay soni
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDD
Shai Yallin
 
Stl Containers
Stl ContainersStl Containers
Stl Containers
ppd1961
 

Similar to Container Classes (20)

Generics
GenericsGenerics
Generics
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Kotlin Advanced - language reference for Android developers
Kotlin Advanced - language reference for Android developers Kotlin Advanced - language reference for Android developers
Kotlin Advanced - language reference for Android developers
 
Design patterns with Kotlin
Design patterns with KotlinDesign patterns with Kotlin
Design patterns with Kotlin
 
Java02
Java02Java02
Java02
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
scalar.pdf
scalar.pdfscalar.pdf
scalar.pdf
 
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
 
C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
Designpatterns illustrated
Designpatterns illustratedDesignpatterns illustrated
Designpatterns illustrated
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
 
PATTERNS09 - Generics in .NET and Java
PATTERNS09 - Generics in .NET and JavaPATTERNS09 - Generics in .NET and Java
PATTERNS09 - Generics in .NET and Java
 
Imp_Points_Scala
Imp_Points_ScalaImp_Points_Scala
Imp_Points_Scala
 
Java mcq
Java mcqJava mcq
Java mcq
 
Unit ii
Unit   iiUnit   ii
Unit ii
 
Traits inscala
Traits inscalaTraits inscala
Traits inscala
 
Traits in scala
Traits in scalaTraits in scala
Traits in scala
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDD
 
Stl Containers
Stl ContainersStl Containers
Stl Containers
 

More from adil raja

ANNs.pdf
ANNs.pdfANNs.pdf
ANNs.pdf
adil raja
 
A Software Requirements Specification
A Software Requirements SpecificationA Software Requirements Specification
A Software Requirements Specification
adil raja
 
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial VehiclesNUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
adil raja
 
DevOps Demystified
DevOps DemystifiedDevOps Demystified
DevOps Demystified
adil raja
 
On Research (And Development)
On Research (And Development)On Research (And Development)
On Research (And Development)
adil raja
 
Simulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge ResearchSimulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge Research
adil raja
 
The Knock Knock Protocol
The Knock Knock ProtocolThe Knock Knock Protocol
The Knock Knock Protocol
adil raja
 
File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Sockets
adil raja
 
Remote Command Execution
Remote Command ExecutionRemote Command Execution
Remote Command Execution
adil raja
 
CMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor PakistanCMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor Pakistan
adil raja
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
adil raja
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
adil raja
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
adil raja
 
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPReal-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
adil raja
 
VoIP
VoIPVoIP
VoIP
adil raja
 
ULMAN GUI Specifications
ULMAN GUI SpecificationsULMAN GUI Specifications
ULMAN GUI Specifications
adil raja
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
adil raja
 
ULMAN-GUI
ULMAN-GUIULMAN-GUI
ULMAN-GUI
adil raja
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
adil raja
 

More from adil raja (20)

ANNs.pdf
ANNs.pdfANNs.pdf
ANNs.pdf
 
A Software Requirements Specification
A Software Requirements SpecificationA Software Requirements Specification
A Software Requirements Specification
 
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial VehiclesNUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
 
DevOps Demystified
DevOps DemystifiedDevOps Demystified
DevOps Demystified
 
On Research (And Development)
On Research (And Development)On Research (And Development)
On Research (And Development)
 
Simulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge ResearchSimulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge Research
 
The Knock Knock Protocol
The Knock Knock ProtocolThe Knock Knock Protocol
The Knock Knock Protocol
 
File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Sockets
 
Remote Command Execution
Remote Command ExecutionRemote Command Execution
Remote Command Execution
 
Thesis
ThesisThesis
Thesis
 
CMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor PakistanCMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor Pakistan
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPReal-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
 
VoIP
VoIPVoIP
VoIP
 
ULMAN GUI Specifications
ULMAN GUI SpecificationsULMAN GUI Specifications
ULMAN GUI Specifications
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 
ULMAN-GUI
ULMAN-GUIULMAN-GUI
ULMAN-GUI
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 

Recently uploaded

GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Yara Milbes
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 

Recently uploaded (20)

GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 

Container Classes

  • 1. Introduction Summary References CONTAINER CLASSES Muhammad Adil Raja Roaming Researchers, Inc. cbna April 21, 2015 Container Classes Roaming Researchers, Inc. cbna
  • 2. Introduction Summary References OUTLINE I INTRODUCTION SUMMARY REFERENCES Container Classes Roaming Researchers, Inc. cbna
  • 3. Introduction Summary References INTRODUCTION We will examine how object-oriented programming techniques can be applied to the problem of creating container, or collection classes. Containers in Dynamically Typed Languages. The Tension between Reuse and Strong Typing. Object Oriented Solutions. Substitution and Downcasting. Substitution and Overriding. Templates. Collection Traversals. Iterators. Visitors. Container Classes Roaming Researchers, Inc. cbna
  • 4. Introduction Summary References CONTAINERS IN DYNAMICALLY TYPED LANGUAGES Collection classes are simple to write in dynamically typed languages, Since all variables are polymorphic. Contains simply hold values in variables (which can hold anything), and when they are removed from the container they can be assigned to any variable. Dynamically typed languages have historically come with a rich set of container classes in their standard library. Container Classes Roaming Researchers, Inc. cbna
  • 5. Introduction Summary References TENSION BETWEEN STRONG TYPING AND REUSE The situation is very different when we move to statically typed languages. There, the strong typing can get in the way of software reuse. THE TENSION lstsetlanguage=C++ type Link = Record value : integer nextelement : ^ Link end ; What happens when we need a linked list of doubles? or of a user defined type? The strong typing is getting in the way of software reuse. Container Classes Roaming Researchers, Inc. cbna
  • 6. Introduction Summary References CAN OO TECHNIQUES SOLVE THIS? Can we bring any of the OO techniques (subsitution, polymorphism) into the solution of this problem? We examine three techniques: Using Substitution and Downcasting. Using Substitution and Overriding. Using Templates (or Generics). Container Classes Roaming Researchers, Inc. cbna
  • 7. Introduction Summary References USING SUBSTITUTION AND DOWNCASTING In Java and other languages, (almost) all values can be stored in a variable of type Object. Therefore, we write containers that store Object values. Problem. Requires a cast when a value is removed from the container. SUBSTITUTION Vector aVector = new Vector ( ) ; Cat Felice = new Cat ( ) ; aVector . addElement ( Felice ) ; . . . / / cast used to convert Object value to Cat Cat animal = ( Cat ) aVector . elementAt ( 0 ) ; Worse problem, typing errors are detected when a value is removed from the collection, not when it is inserted. Container Classes Roaming Researchers, Inc. cbna
  • 8. Introduction Summary References HETEROGENEOUS COLLECTIONS Because any value can be stored in an Object, this allows heterogeneous collections; although the actual type must be checked when a value is removed: EXAMPLE Stack stk = new Stack ( ) ; stk . addElement (new Cat ( ) ) ; stk . addElement (new Dog ( ) ) ; / / . . . adding more values / / now do something with Cat values i f ( stk . peek ( ) instanceof Cat ) { / / do conversion to Cat Cat aCat = ( Cat ) stk . pop ( ) ; / / . . also do something with cat values / / now do something with Dog values } else i f ( stk . peek ( ) instanceof Dog) { / / do conversion to Dog Dog aDog = (Dog) stk . pop ( ) ; / / . . do something with Dog value } Container Classes Roaming Researchers, Inc. cbna
  • 9. Introduction Summary References USING CLASSES AS OBJECTS TO DO TYPE CHECKING In languages in which classes are objects, and have the ability to tell if an object is an instance, the container can hold the class object that represents its type: EXAMPLE var stack : TStack ; aCat : TCat ; aDog : TDog ; begin / / create a stack that can hold only TCat values stack := TStack . Create ( TCat ) ; stack . push ( aCat ) ; / / ok stack . push (aDog ) ; / / w i l l raise exception . . . end Allows typing errors to be caught on insertion, but uses more space and time. Container Classes Roaming Researchers, Inc. cbna
  • 10. Introduction Summary References STRONG PRIMITIVE VALUES In some languages (Java, Delphi) primitive types are not objects. These values cannot be stored using this scheme. Thus, languages introduce wrapper classes that do nothing but hold a primitive value. EXAMPLE Vector aVector = new Vector ( ) ; aVector . add (new Integer ( 1 7 ) ) ; / / wrap up p r i m i t i v e i n t as Integer . . . Integer a = aVector . elementAt ( 1 ) ; int x = a . intValue ( ) ; / / must subsequently be unwrapped Container Classes Roaming Researchers, Inc. cbna
  • 11. Introduction Summary References USING SUBSTITUTION AND OVERRIDING In certain situations we can eliminate the downcast, replacing it with overriding. This only works, however, if you can predict ahead of time what operations you want to do on a collection. Container Classes Roaming Researchers, Inc. cbna
  • 12. Introduction Summary References AN EXAMPLE, JAVA WINDOW EVENTS A good example is the way that window events are handled in Java. Each window maintains a list of listeners, each listener must implement a fixed interface (WindowListener). EXAMPLE public class CloseQuit extends WindowAdapter { / / execute when the user c l i c k s in the close box public void windowClosing ( WindowEvent e ) { System . e x i t ( 0 ) ; / / h a l t the program } } When an event occurs the window simply runs down the list of listener, invoking the appropriate method in each. No downcasting required. Container Classes Roaming Researchers, Inc. cbna
  • 13. Introduction Summary References A THIRD ALTERNATIVE – GENERICS CODE template <class T> class L i s t { public : void addElement (T newValue ) ; T firstElement ( ) ; private : Link ∗ f i r s t L i n k ; private class Link { / / nested class public : T value ; Link ∗ nextLink ; Link (T v , Link ∗ n ) : value ( v ) , nextLink ( n ) { } } ; } ; Allow for both strong typing and reuse. Container Classes Roaming Researchers, Inc. cbna
  • 14. Introduction Summary References COLLECTION TRAVERSAL Here is another difficult problem. How do you allow access to elements of a collection without exposing the internal structure? Consider the conventional solution: EXAMPLE var aList : L i s t ; (∗ the l i s t being manipulated ∗) p : Link ; (∗ a pointer for the loop ∗) begin . . . p := aList . f i r s t L i n k ; while ( p <> n i l ) do begin w r i t e l n ( p . value ) ; p := p ^ . nextElement ; end ; Needed to expose the link type, and the field nextElement. Can we avoid this problem? Container Classes Roaming Researchers, Inc. cbna
  • 15. Introduction Summary References There are two common solutions: Create an iterator; an object that facilitates access to elements and enumeration over the collection. The vistor. Bundle the action to be performed as an object, and pass it to the collection. Container Classes Roaming Researchers, Inc. cbna
  • 16. Introduction Summary References ITERATOR An iterator is an object that has two major responsibilities: Provide access to the current element. Provide a way to move to the next element. Different languages use different interfaces, but the basic idea is the same. Container Classes Roaming Researchers, Inc. cbna
  • 17. Introduction Summary References AN ENUMERATION LOOP IN JAVA EXAMPLE / / create the i t e r a t o r object Enumeration e = aList . elements ( ) ; / / then do the loop while ( e . hasMoreElements ( ) ) { Object obj = e . nextElement ( ) ; / / . . . do something with obj } Interface consists of two methods, hasMoreElements and nextElement. Container Classes Roaming Researchers, Inc. cbna
  • 18. Introduction Summary References AN ITERATION LOOP IN C++ EXAMPLE / / create s t a r t i n g and stopping i t e r a t o r s l i s t : : i t e r a t o r s t a r t = aList . begin ( ) ; l i s t : : i t e r a t o r stop = aList . end ( ) ; / / then do the loop for ( ; s t a r t != stop ; s t a r t ++ ) { s t r i n g value = ∗ s t a r t ; / / get the value / / . . . do something with the value } Interface consists of three operations; comparison, increment, and dereference. Container Classes Roaming Researchers, Inc. cbna
  • 19. Introduction Summary References ITERATORS AND ENCAPSULATION Note that the iterator must almost always have detailed knowledge of the internal data structure. Wasn’t our goal to hide these details? Not exactly. The iterator is usually developed by the same programmer creating the data structure. It can share detailed knowledge with the data structure. And using the iterator does not require any knowledge of how the iterator is implemented. Often friends (in C++) or nested classes (in Java) can be used to help the iterator share information with the container. Container Classes Roaming Researchers, Inc. cbna
  • 20. Introduction Summary References ALTERNATIVE, THE VISITOR An alternative to iterators is the idea of a visitor. Requires the ability to bundle the action to be performed into an object, and hand it to the collection. This is most common technique used in Smalltalk. aList do: [:x | (’element is’ + x) print ]. The block is passed as argument to the list, which turns around and executes the block on each element. Container Classes Roaming Researchers, Inc. cbna
  • 21. Introduction Summary References THE VISITOR IN C++ IN THE STL EXAMPLE class printingObject { public : void operator ( ) ( int x ) { cout << " value i s " << x << endl ; } } ; printingObject p r i n t e r ; / / create an instance of the function object for_each ( aList . begin ( ) , aList . end ( ) , p r i n t e r ) ; The function for_each passes the object to element element in the collection. Container Classes Roaming Researchers, Inc. cbna
  • 22. Introduction Summary References SUMMARY Collection Classes are easy to write in dynamically typed languages. In statically typed languages, there is a conflict between reuse and strong typing. Three approaches to overcoming this include. Using substitution and downcasting. Using substitution and overriding. Using generics. A related problem, looping over elements. The iterator solution. The visitor solution. Container Classes Roaming Researchers, Inc. cbna
  • 23. Introduction Summary References REFERENCES Images and content for developing these slides have been taken from the follwoing book with the permission of the author. An Introduction to Object Oriented Programming, Timothy Budd. This presentation is developed with Beamer: Szeged, dove. Container Classes Roaming Researchers, Inc. cbna