Skip to content

Commit a8ded00

Browse files
committed
Code Changes.
Written Bridge.py, edited Iterator.py and Singleton.py. Commented out title page/appendicies for ease of writing and speed of compiling. Ignore C++ examples and latex rubbish.
1 parent 75a73b2 commit a8ded00

8 files changed

Lines changed: 210 additions & 159 deletions

File tree

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ignore emacs temp files
2+
*~
3+
4+
# ignore the latex rubbish
5+
*.aux
6+
*.out
7+
*.log
8+
*.snm
9+
*.nav
10+
*.toc
11+
*.synctex.gz
12+
13+
# ignore the finished product, attach as a download later
14+
*.pdf
15+
16+
# ignore the C++ examples I've made to better my understanding
17+
C++/*

Bridge.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,60 @@
11
#!/usr/bin/env python
22
# Written by: DGC
33

4-
class something(object):
5-
4+
#==============================================================================
5+
class Shape(object):
6+
67
def __init__(self):
7-
print("initalised")
8+
self.calculator = None
9+
10+
def area(self):
11+
"""
12+
Returns the area of the shape calculated using the shape specific
13+
implementation.
14+
"""
15+
assert self.calculator != None, "self.calculator not defined."
16+
return self.calculator.calculate()
17+
18+
#==============================================================================
19+
class Rectangle(Shape):
20+
21+
def __init__(self, x, y):
22+
self.calculator = RectangleAreaImpl(x, y)
23+
self.x = x
24+
self.y = y
25+
26+
#==============================================================================
27+
class RectangleAreaImpl(object):
28+
29+
def __init__(self, x, y):
30+
self.x = x
31+
self.y = y
32+
33+
def calculate(self):
34+
return self.x * self.y
35+
36+
#==============================================================================
37+
class Triangle(Shape):
38+
39+
def __init__(self, base, height):
40+
self.calculator = TriangleAreaImpl(base, height)
41+
self.base = base
42+
self.height = height
43+
44+
#==============================================================================
45+
class TriangleAreaImpl(object):
846

47+
def __init__(self, base, height):
48+
self.base = base
49+
self.height = height
950

51+
def calculate(self):
52+
return 0.5 * self.base * self.height
53+
54+
#==============================================================================
55+
if (__name__ == "__main__"):
56+
rect = Rectangle(4, 5)
57+
print("4 x 5 Rectangle area: %d" %(rect.area()))
58+
59+
tri = Triangle(4.0, 5.0);
60+
print("Base 4 height 5 Triangle area: %d" %(tri.area()))

Design_Patterns_In_Python.tex

Lines changed: 99 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -95,34 +95,34 @@
9595

9696
\begin{document}
9797

98-
% title page
99-
\thispagestyle{empty}
100-
% Upper part of the page
101-
\begin{center}
102-
\includegraphics[width=0.25\textwidth]{Python_Logo.png}\\[1cm]
103-
\end{center}
104-
% Title
105-
\HRule \\[0.4cm]
106-
{\Huge \bfseries \color{RoyalBlue}Design Patterns in Python} \\[0.4cm]
107-
{\huge or How I Learned to Stop Worrying and Love The Gang of Four} \\[0.4cm]
108-
\HRule \\[1.5cm]
109-
110-
% Author
111-
\begin{minipage}{0.4\textwidth}
112-
\begin{flushleft}
113-
\Large David Corne
114-
\end{flushleft}
115-
\end{minipage}
116-
117-
\vfill
118-
% Bottom of the page
119-
% {\large August 2012.}
120-
121-
\frontmatter
122-
\tableofcontents
123-
124-
\chapter{Acknowledgments}
125-
\mainmatter
98+
% % title page
99+
% \thispagestyle{empty}
100+
% % Upper part of the page
101+
% \begin{center}
102+
% \includegraphics[width=0.25\textwidth]{Python_Logo.png}\\[1cm]
103+
% \end{center}
104+
% % Title
105+
% \HRule \\[0.4cm]
106+
% {\Huge \bfseries \color{RoyalBlue}Design Patterns in Python} \\[0.4cm]
107+
% {\huge or How I Learned to Stop Worrying and Love The Gang of Four} \\[0.4cm]
108+
% \HRule \\[1.5cm]
109+
%
110+
% % Author
111+
% \begin{minipage}{0.4\textwidth}
112+
% \begin{flushleft}
113+
% \Large David Corne
114+
% \end{flushleft}
115+
% \end{minipage}
116+
%
117+
% \vfill
118+
% % Bottom of the page
119+
% % {\large August 2012.}
120+
%
121+
% \frontmatter
122+
% \tableofcontents
123+
%
124+
% \chapter{Acknowledgments}
125+
% \mainmatter
126126

127127
\chapter{Introduction}
128128
There are many places this is usful, there have been a few tries to do this.
@@ -300,97 +300,77 @@ \section{Model View View-Model}
300300

301301
\chapter{Summary}
302302

303-
304-
305-
306-
307-
308-
309-
310-
311-
312-
313-
314-
315-
316-
317-
318-
319-
320-
321-
322-
323-
\part*{Appendicies}
324-
\addcontentsline{toc}{part}{Appendicies}
325-
\appendix
326-
\chapter{Creational Code Examples}
327-
328-
\section{Abstract Factory}
329-
\lstinputlisting{Abstract_Factory.py}
330-
331-
\section{Builder}
332-
\lstinputlisting{Builder.py}
333-
\section{Factory Method}
334-
\lstinputlisting{Factory_Method.py}
335-
\section{Prototype}
336-
\lstinputlisting{Prototype.py}
337-
\section{Singleton}
338-
\lstinputlisting{Singleton.py}
339-
340-
\chapter{Structural Code Examples}
341-
342-
\section{Adapter}
343-
\lstinputlisting{Adapter.py}
344-
\section{Bridge}
345-
\lstinputlisting{Bridge.py}
346-
\section{Composite}
347-
\lstinputlisting{Composite.py}
348-
\section{Decorator}
349-
\lstinputlisting{Decorator.py}
350-
\section{Facade}
351-
\lstinputlisting{Facade.py}
352-
\section{Flyweight}
353-
\lstinputlisting{Flyweight.py}
354-
\section{Proxy}
355-
\lstinputlisting{Proxy.py}
356-
357-
\chapter{Behavioural Code Examples}
358-
359-
\section{Chain of Responsibility}
360-
\lstinputlisting{Chain_of_Responsibility.py}
361-
\section{Command}
362-
\lstinputlisting{Command.py}
363-
\section{Interpreter}
364-
\lstinputlisting{Interpreter.py}
365-
\section{Iterator}
366-
\lstinputlisting{Iterator.py}
367-
\section{Mediator}
368-
\lstinputlisting{Mediator.py}
369-
\section{Memento}
370-
\lstinputlisting{Memento.py}
371-
\section{Observer}
372-
\lstinputlisting{Observer.py}
373-
\section{State}
374-
\lstinputlisting{State.py}
375-
\section{Strategy}
376-
\lstinputlisting{Strategy.py}
377-
\section{Template Method}
378-
\lstinputlisting{Template_Method.py}
379-
\section{Visitor}
380-
\lstinputlisting{Visitor.py}
381-
382-
\chapter{Non GOF Patterns/GUI Patterns Code Examples}
383-
\section{Model View Presenter}
384-
\lstinputlisting{Model_View_Presenter.py}
385-
\section{Model View Controller}
386-
\lstinputlisting{Model_View_Controller.py}
387-
\section{Model View View-Model}
388-
\lstinputlisting{Model_View_View-Model.py}
389-
390-
\begin{thebibliography}{99}
391-
\bibitem{GOF} Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, \emph{Design Patterns: Elements of Reusable Object-Oriented Software} (: Addison-Wesley Professional, January 15, 1995)
392-
\bibitem{python-patterns-site} http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style
393-
\end{thebibliography}
303+
% \part*{Appendicies}
304+
% \addcontentsline{toc}{part}{Appendicies}
305+
% \appendix
306+
% \chapter{Creational Code Examples}
307+
%
308+
% \section{Abstract Factory}
309+
% \lstinputlisting{Abstract_Factory.py}
310+
%
311+
% \section{Builder}
312+
% \lstinputlisting{Builder.py}
313+
% \section{Factory Method}
314+
% \lstinputlisting{Factory_Method.py}
315+
% \section{Prototype}
316+
% \lstinputlisting{Prototype.py}
317+
% \section{Singleton}
318+
% \lstinputlisting{Singleton.py}
319+
%
320+
% \chapter{Structural Code Examples}
321+
%
322+
% \section{Adapter}
323+
% \lstinputlisting{Adapter.py}
324+
% \section{Bridge}
325+
% \lstinputlisting{Bridge.py}
326+
% \section{Composite}
327+
% \lstinputlisting{Composite.py}
328+
% \section{Decorator}
329+
% \lstinputlisting{Decorator.py}
330+
% \section{Facade}
331+
% \lstinputlisting{Facade.py}
332+
% \section{Flyweight}
333+
% \lstinputlisting{Flyweight.py}
334+
% \section{Proxy}
335+
% \lstinputlisting{Proxy.py}
336+
%
337+
% \chapter{Behavioural Code Examples}
338+
%
339+
% \section{Chain of Responsibility}
340+
% \lstinputlisting{Chain_of_Responsibility.py}
341+
% \section{Command}
342+
% \lstinputlisting{Command.py}
343+
% \section{Interpreter}
344+
% \lstinputlisting{Interpreter.py}
345+
% \section{Iterator}
346+
% \lstinputlisting{Iterator.py}
347+
% \section{Mediator}
348+
% \lstinputlisting{Mediator.py}
349+
% \section{Memento}
350+
% \lstinputlisting{Memento.py}
351+
% \section{Observer}
352+
% \lstinputlisting{Observer.py}
353+
% \section{State}
354+
% \lstinputlisting{State.py}
355+
% \section{Strategy}
356+
% \lstinputlisting{Strategy.py}
357+
% \section{Template Method}
358+
% \lstinputlisting{Template_Method.py}
359+
% \section{Visitor}
360+
% \lstinputlisting{Visitor.py}
361+
%
362+
% \chapter{Non GOF Patterns/GUI Patterns Code Examples}
363+
% \section{Model View Presenter}
364+
% \lstinputlisting{Model_View_Presenter.py}
365+
% \section{Model View Controller}
366+
% \lstinputlisting{Model_View_Controller.py}
367+
% \section{Model View View-Model}
368+
% \lstinputlisting{Model_View_View-Model.py}
369+
%
370+
% \begin{thebibliography}{99}
371+
% \bibitem{GOF} Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, \emph{Design Patterns: Elements of Reusable Object-Oriented Software} (: Addison-Wesley Professional, January 15, 1995)
372+
% \bibitem{python-patterns-site} http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style
373+
% \end{thebibliography}
394374
\end{document}
395375

396376

Iterator.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ class ReverseIterator(object):
88

99
def __init__(self, iterable_object):
1010
self.list = iterable_object
11+
# start at the end of the iterable_object
1112
self.index = len(iterable_object)
1213

1314
def __iter__(self):
15+
# return an iterator
1416
return self
1517

1618
def next(self):
@@ -32,8 +34,7 @@ def next(self):
3234
"Sunday"
3335
]
3436
it = ReverseIterator(days)
35-
#for day in it:
36-
# print(day)
37-
print(iter(it))
37+
for day in it:
38+
print(day)
3839

3940

Python_Logo.png

2.18 KB
Loading

0 commit comments

Comments
 (0)