

% add the PATH to retrieve cls and sty beamer styles files
\makeatletter
\providecommand*{\input@path}{}
\edef\input@path{{../../support/beamer/}\input@path}% prepend
\makeatother

% \documentclass{pharoslides}
% \documentclass[aspectratio=169]{pharoslides} % format 16/9
\documentclass[withvideo]{pharoslides} % format 16/9 + incrustation video

\graphicspath{{./figures/}{../}{../../support/beamer/imgs/}}

%Information to be included in the title page:
\title{Advanced Points on Classes}
\subtitle{}
\author{Damien Cassou, Stéphane Ducasse and Luc Fabresse}
\institute{http://stephane.ducasse.free.fr}
\date{}
\slidesid{W7S01}

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{Roadmap}

\begin{itemize}
\item Sharing state between instances of a class
\item Instance variables of classes
\item Class initialization
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Roadmap}

\begin{itemize}
\item \textbf{Sharing state between instances of a class}
\item Instance variables of classes
\item Class initialization
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Sharing State?}

How do you share state between instances of a class?

\begin{itemize}
\item in Java, an \symbol{34}instance\symbol{34} variable can be \code{static}
\item in Pharo, we use \textbf{class variables}
\end{itemize}

\begin{listing}{}
Object subclass: #Color
   instanceVariableNames: 'rgb cachedDepth...'
   classVariableNames: 'ColorRegistry ComponentMask...'
   package: 'Graphics-Primitives'
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Class Variables}

\begin{itemize}
\item shared by the instances of the class and subclasses
\item accessible from instance and class methods
\item start with an uppercase letter
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Class Variable Access}


\begin{center}
\includegraphics[width=1.0\textwidth]{figures/ColorAndColorClass2.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{Class Variable Access}


\begin{center}
\includegraphics[width=0.95\textwidth]{figures/Color.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{Roadmap}

\begin{itemize}
\item Sharing state between instances of a class
\item \textbf{Instance variables of classes}
\item Class initialization
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Class Instance Variables}

A class can have instance variables like any object

\begin{itemize}
\item a class is an instance of a class (its metaclass)
\item a metaclass can specify \textbf{class instance variables}
\end{itemize}

\begin{listing}{}
RPackageSet class
   instanceVariableNames: 'cachePackages'
\end{listing}

\begin{itemize}
\item accessible from class methods only
\item start with a lowercase letter
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{No Sharing with Class Instance Variables}

Each instance has a different value for \code{cachePackages}


\begin{center}
\includegraphics[width=1.0\textwidth]{figures/classInstanceVariable.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{Singleton Design Pattern}

\begin{itemize}
\item Intent: Enforce that a class has only one instance
\item A solution: Keep the instance in a variable of the class
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Singleton with a Class Instance Variable}

\begin{listing}{}
WebServer class
   instanceVariableNames: 'uniqueInstance'

WebServer class >> new
   self error: 'Can''t create a new instance'

WebServer class >> uniqueInstance
   ^ uniqueInstance
      ifNil: [ uniqueInstance := super new ]
\end{listing}

Consequence:

\begin{itemize}
\item each subclass has its own value for \code{uniqueInstance}
\begin{itemize}
\item each subclass of \code{WebServer} has its own singleton
\end{itemize}

\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Singleton with a Class Variable}

\begin{listing}{}
Object subclass: #WebServer
   instanceVariableNames: ''
   classVariableNames: 'UniqueInstance'
   package: 'Web'

WebServer class >> new
   self error: 'Can''t create a new instance'

WebServer class >> uniqueInstance
   ^ UniqueInstance
      ifNil: [ UniqueInstance := super new ]
\end{listing}

Consequence:

\begin{itemize}
\item only one singleton for the complete class hierarchy
\begin{itemize}
\item class variable values are shared
\end{itemize}

\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Roadmap}

\begin{itemize}
\item Sharing state between instances of a class
\item Instance variables of classes
\item \textbf{Class initialization}
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Class Initialization}

\begin{itemize}
\item Everything is an object
\item An object is initialized at creation time
\item Classes are objects too
\end{itemize}

How and when are classes initialized?
\end{frame}



\begin{frame}[fragile]

\frametitle{Class Initialization}

A class is initialized

\begin{itemize}
\item at load time after its methods are loaded
\item or explicitly by the programmer:
\end{itemize}

\begin{listing}{}
Color initialize
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Color Initialization}

\begin{listing}{}
Color class >> initialize
   "Externally, the red, green, and blue components of color are floats in the range [0.0..1.0]. Internally, they are represented as integers in the range [0..ComponentMask] packing into a small integer to save space and to allow fast hashing and equality testing."
   ComponentMask := 1023.
   HalfComponentMask := 512. "used to round up in integer calculations"
   ComponentMax := 1023.0. "used to normalize components"
   RedShift := 20.
   GreenShift := 10.
   BlueShift := 0.
   self initializeIndexedColors.
   self initializeColorRegistry.
   self initializeGrayToIndexMap.
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Warning}

\begin{itemize}
\item don't write \code{super initialize} in a class \code{initialize} method
\begin{itemize}
\item this will initialize superclasses that are already initialized
\end{itemize}

\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{What You Should Know}

\begin{itemize}
\item state is shared between instances through class variables
\item a class can store values in class instance variables
\item a class is initialized through the class method \code{initialize}
\end{itemize}
\end{frame}




% \setlength{\textwidth}{\originaltextwidth}
\setbeamertemplate{background canvas}{\insertBorderForHandout}

\begin{frame}[plain]

\begin{minipage}{\originaltextwidth}
\centering
\large


\vspace{2em}

A course by

\vspace{1em}

\hspace{-20px}\includegraphics[height=32px]{inriaLogo}\hspace{3em}\raisebox{7px}{and}\hspace{3em}\raisebox{-6px}{\includegraphics[height=30px]{logoutop}}

\vspace{2em}

in collaboration with

\vspace{1em}

\includegraphics[height=40px]{unisciellogo}\hskip 1.5em%
\raisebox{5px}{\includegraphics[height=30px]{unitlogo}}\hskip 2em%
\raisebox{8px}{\includegraphics[height=24px]{univLilleLogo}}\hskip 2em%
\raisebox{3px}{\includegraphics[height=29px]{logo-IMT}}\hskip 2em%
\raisebox{1px}{\includegraphics[height=32px]{minesDouaiLogo}}

\vspace{2.5em}

\includegraphics[width=50px]{by-nc-nd} \tiny \hskip 0.5em \raisebox{5px}{Inria 2016}

\medskip

\tiny
Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 France

\url{https://creativecommons.org/licenses/by-nc-nd/3.0/fr/} 

\end{minipage}
\end{frame}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
