

% 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{Dice new vs. self class new}
\subtitle{}
\author{Damien Cassou, Stéphane Ducasse and Luc Fabresse}
\institute{http://stephane.ducasse.free.fr}
\date{}
\slidesid{W6S03}

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{From the Exercise}

To support 

\begin{listing}{}
(DiceHandle new add: (Dice faces: 4); yourself) 
	+ (DiceHandle new add: (Dice faces: 6); yourself)
\end{listing}

We defined \code{+} as 

\begin{listing}{}
DiceHandle >> + aDiceHandle
  | handle |
  handle := DiceHandle new.
  self dice do: [ :each | handle addDice: each ]. 
  aDiceHandle dice do: [ :each | handle addDice: each ]. 
  ^ handle
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{What Is The Difference...}

Between 

\begin{listing}{}
DiceHandle >> + aDiceHandle
  | handle |
  handle := DiceHandle new.
\end{listing}

And 

\begin{listing}{}
DiceHandle >> + aDiceHandle
  | handle |
  handle := self class new.
\end{listing}

Let us see....
\end{frame}



\begin{frame}[fragile]

\frametitle{What If We Create A New Subclass}

\begin{listing}{}
DiceHandle subclass: MemoDiceHandle
  ....
\end{listing}

\begin{listing}{}
(MemoDiceHandle new add: (Dice faces: 4); yourself) 
   + (MemoDiceHandle new add: (Dice faces: 6); yourself)
> aDiceHandle
\end{listing}

We get a \code{DiceHandle} instance back and not a \code{MemoDiceHandle} instance!!!
\end{frame}



\begin{frame}[fragile]

\frametitle{Solution 1: Creating a Hook}

\begin{listing}{}
DiceHandle >> + aDiceHandle
  | handle |
  handle := self handleClass new.
  self dice do: [ :each | handle addDice: each ]. 
  aDiceHandle dice do: [ :each | handle addDice: each ]. 
  ^ handle
\end{listing}

\begin{listing}{}
DiceHandle >> handleClass
  ^ DiceHandle
\end{listing}

A subclass may redefine \code{handleClass} 

\begin{listing}{}
MemoDiceHandle >> handleClass
  ^ MemoDiceHandle
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Solution 1: Creating a Hook}

\begin{listing}{}
(MemoDiceHandle new add: (Dice faces: 4); yourself) 
   + (MemoDiceHandle new add: (Dice faces: 6); yourself)
> aMemoDiceHandle
\end{listing}

We get an instance of the subclass!
\end{frame}



\begin{frame}[fragile]

\frametitle{But We Can Do Better!}

Let us see

\begin{itemize}
\item In each subclass we should redefine the hook method  \code{handleClass}
\item This is tedious
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Solution 2}

\begin{listing}{}
DiceHandle >> + aDiceHandle
  | handle |
  handle := self class new.
  self dice do: [ :each | handle addDice: each ]. 
  aDiceHandle dice do: [ :each | handle addDice: each ]. 
  ^ handle
\end{listing}

\begin{itemize}
\item \code{self class} always returns the class of the receiver
\item We get instances of the same kind of the receiver 
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Conclusion}

If we define a subclass of \code{DiceHandle}, 
and send the message \code{+} to an instance

\begin{itemize}
\item With \code{DiceHandle new}, \code{+} does not return an instance of the subclass but of \code{DiceHandle}
\end{itemize}

\begin{itemize}
\item With \code{self class new}, \code{+} returns an instance of the receiver: an instance of a potential subclass
\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:
