

% 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{Hooks and Templates}
\subtitle{An application of self-sends are plans for reuse}
\author{Damien Cassou, Stéphane Ducasse and Luc Fabresse}
\institute{http://stephane.ducasse.free.fr}
\date{}
\slidesid{W6S05}

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{Remember...}

\begin{itemize}
\item A message send leads to a choice
\item A class hierarchy defines the choices
\item Code can be reused and refined in subclasses
\item Sending a message in a class defines a hook:
\begin{itemize}
\item i.e., a place where subclasses can inject variations
\end{itemize}

\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{The Template Method}

\begin{itemize}
\item a template method specifies a skeleton
\item the skeleton has hooks, i.e., places to be customized
\begin{itemize}
\item hooks may or may not have a default behavior
\end{itemize}

\end{itemize}


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

\end{frame}



\begin{frame}[fragile]

\frametitle{printString Template Method}

\begin{listing}{}
(Delay forSeconds: 10) printString
> 'a Delay(10000 msecs)'
\end{listing}

\begin{listing}{}
Object >> printString
   "Answer a String whose characters are a description of the receiver."
   ^ self printStringLimitedTo: 50000

Object >> printStringLimitedTo: limit
   | limitedString |
   limitedString := String
                                        streamContents: [ :s | self printOn: s ]
                                        limitedTo: limit.
   limitedString size < limit ifTrue: [ ^ limitedString ].
   ^ limitedString , '...etc...'
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{printOn: Default Hook}

\begin{listing}{}
Node new
> a Node

Apple new
> an Apple
\end{listing}

Default behavior:

\begin{listing}{}
Object >> printOn: aStream
   "Append to the argument, aStream, a sequence of characters that identifies the receiver."
   | title |
   title := self class name.
   aStream
      nextPutAll: (title first isVowel ifTrue: [ 'an ' ] ifFalse: [ 'a ' ]);
      nextPutAll: title
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{printOn: Refinement}

\begin{listing}{}
(Delay forSeconds: 1)
> a Delay(1000 msecs)
\end{listing}

Reusing and extending default behavior:

\begin{listing}{}
Delay >> printOn: aStream
   super printOn: aStream.
   aStream
      nextPutAll: '(';
      print: millisecondDelayDuration;
      nextPutAll: ' msecs)'
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{printOn: Redefinition}

\begin{listing}{}
true not
> false
\end{listing}

Redefinition in \code{False}:

\begin{listing}{}
False >> printOn: aStream
   aStream nextPutAll: 'false'
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{printOn: Redefinition}

\begin{listing}{}
1 to: 100
> (1 to: 100)
1 to: 100 by: 3
> (1 to: 100 by: 3)
\end{listing}

Redefinition in \code{Interval}:

\begin{listing}{}
Interval >> printOn: aStream
   aStream
      nextPut: $(;
      print: start;
      nextPutAll: ' to: ';
      print: stop.
   step ~= 1
      ifTrue: [ aStream nextPutAll: ' by: '; print: step ].
   aStream nextPut: $)
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Another Template Method: Object Copy}

\begin{itemize}
\item copying objects is complex:
\begin{itemize}
\item graph of connected objects
\item cycles
\item each class may want a different copy strategy
\end{itemize}

\item simple solution for simple cases: \code{copy}/\code{postCopy}
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Object Copy}

\begin{listing}{}
Object >> copy
  "Answer another instance just like the receiver. Subclasses typically override postCopy. Copy is a template method in the sense of Design Patterns. So do not override it. Override postCopy instead. Pay attention that normally you should call postCopy of your superclass too."
    ^ self shallowCopy postCopy
\end{listing}

\begin{listing}{}
Object >> shallowCopy
  "Answer a copy of the receiver which shares the receiver's instance variables. Subclasses that need to specialize the copy should specialize the postCopy hook method."
  <primitive: 148>
  ...
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Default hook}

\begin{listing}{}
Object >> postCopy
  "I'm a hook method in the sense of Design Patterns TemplateHook/Methods. I'm called by copy. self is a shallow copy, subclasses should copy fields as necessary to complete the full copy"
  ^ self
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{postCopy: Refinement}

\begin{listing}{}
Collection subclass: #Bag
   instanceVariableNames: 'contents'
   classVariableNames: ''
   package: 'Collections-Unordered'
\end{listing}

\begin{listing}{}
Bag >> postCopy
   super postCopy.
   contents := contents copy
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{postCopy: Deeper copy}

\begin{listing}{}
Dictionary >> postCopy
   "Must copy the associations, or later store will affect both the original and the copy"
   array := array collect: [ :association |
      association ifNotNil: [ association copy ] ]
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Conclusion}

\begin{itemize}
\item Template Method is a very common design pattern
\item Sending a message defines a hook
\item Sending a message increases potential reuse
\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:
