Skip to content

Commit 75a73b2

Browse files
committed
Written a bit more
1 parent 6203973 commit 75a73b2

4 files changed

Lines changed: 18 additions & 6 deletions

File tree

Design_Patterns_In_Python.tex

100755100644
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ \section{Singleton}
180180

181181
This is certainly the most controversial pattern on this list.
182182

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. \\
184184

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:
186186
\begin{lstlisting}
187187
def __new__(new_singleton):
188188
"""Override the __new__ method so that it is a singleton."""
@@ -192,7 +192,7 @@ \section{Singleton}
192192
print("")
193193
return new_singleton.instance
194194
\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}.
196196

197197
\chapter{Structural}
198198

@@ -242,7 +242,16 @@ \section{Interpreter}
242242
\section{Iterator}
243243
% table of information for the start of a section
244244
\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:
246+
% [firstline = 35]
247+
\begin{lstlisting}
248+
for day in it:
249+
print(day)
250+
\end{lstlisting}
251+
Which give exactly the same result as:
252+
%\lstlisting[firstline = 35]{
253+
%thing
254+
% }
246255

247256
\section{Mediator}
248257
% table of information for the start of a section

Iterator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ def next(self):
3232
"Sunday"
3333
]
3434
it = ReverseIterator(days)
35-
for day in it:
36-
print(day)
35+
#for day in it:
36+
# print(day)
37+
print(iter(it))
38+
39+

Singleton.py

100755100644
File mode changed.

primes.py

100755100644
File mode changed.

0 commit comments

Comments
 (0)