You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is certainly the most controversial pattern on this list.
182
182
183
-
In C++ you have to ensure that a new instance of the singleton cannot be made. This is typically done by protecting the constructors and making a static Instance() function which returns the current instance, or makes a new one if it is appropriate.
183
+
In C++ you have to ensure that a new instance of the singleton cannot be made. This is typically done by protecting the constructors and making a static \lstinline[language = c++]{Instance()} function which returns the current instance, or makes a new one if it is appropriate. \\
184
184
185
-
This is another easy pattern to implement in Python. It is a case of overriding the \_\_new()\_\_ function. This is a static method for all classes which is called on construction. This function is called with an argument of the class type requested and returns a new instance of that class. So to implement the singleton pattern it is a case of making sure that the \_\_new()\_\_ method returns the global instance. This can be done with the following code:
185
+
This is another easy pattern to implement in Python. It is a case of overriding the \lstinline{__new__()} function. This is a static method for all classes which is called on construction. This function is called with an argument of the class type requested and returns a new instance of that class. So to implement the singleton pattern it is a case of making sure that the \lstinline{__new__()} method returns the global instance. This can be done with the following code:
186
186
\begin{lstlisting}
187
187
def __new__(new_singleton):
188
188
"""Override the __new__ method so that it is a singleton."""
@@ -192,7 +192,7 @@ \section{Singleton}
192
192
print("")
193
193
return new_singleton.instance
194
194
\end{lstlisting}
195
-
Here the class name is Singleton and instance is a member variable initialised to None.
195
+
Here the class name is Singleton and instance is a member variable initialised to \lstinline{None}.
196
196
197
197
\chapter{Structural}
198
198
@@ -242,7 +242,16 @@ \section{Interpreter}
242
242
\section{Iterator}
243
243
% table of information for the start of a section
244
244
\patternSummaryGoalMotProCon{}{}{}{}
245
-
This is a pattern which falls into the catagory of included in Python already. This is because anything which can be iterated over is able to be. This goes for built in classes but if you want to make an iterable class you need to supply two methods; \_\_iter\_\_() and next().
245
+
This is a pattern which falls into the catagory of included in Python already. This is because anything which can be iterated over is able to be. This goes for built in classes but if you want to make an iterable class you need to supply two methods; \lstinline{__iter__()} and \lstinline{next()}. These two methods can be called to explicitly make a loop or they will be called in a for loop. For instance in the example code there are the following lines:
0 commit comments