Skip to content

Commit 8db9e83

Browse files
committed
Old changes being commited
Hadn't commited
1 parent eeeda7f commit 8db9e83

11 files changed

Lines changed: 379 additions & 124 deletions

Adapter.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,50 @@
22
# Written by: DGC
33

44
#==============================================================================
5-
class Adapter(object):
6-
5+
class RCCar(object):
6+
7+
def __init__(self):
8+
self.speed = 0
9+
10+
def change_speed(self, speed):
11+
self.speed = speed
12+
print("RC car is moving at " + str(self.speed))
13+
14+
#==============================================================================
15+
class RCAdapter(object):
16+
17+
def __init__(self):
18+
self.car = RCCar()
19+
20+
def move_forwards(self):
21+
self.car.change_speed(10)
22+
23+
def move_backwards(self):
24+
self.car.change_speed(-10)
25+
26+
def stop(self):
27+
self.car.change_speed(0)
28+
29+
#==============================================================================
30+
class RemoteControl(object):
31+
732
def __init__(self):
8-
print("initalised")
33+
self.adapter = RCAdapter()
34+
35+
def stick_up(self):
36+
self.adapter.move_forwards()
37+
38+
def stick_down(self):
39+
self.adapter.move_backwards()
40+
41+
def stick_middle(self):
42+
self.adapter.stop()
943

1044
#==============================================================================
1145
if (__name__ == "__main__"):
12-
pass
46+
controller = RemoteControl()
47+
controller.stick_up()
48+
controller.stick_middle()
49+
controller.stick_down()
50+
controller.stick_middle()
51+

Analogy_List.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Abstract Factory----------
2-
Adapter-------------------
2+
Adapter------------------- RC Car controller
33
Bridge-------------------- Shape area calculation
4-
Builder-------------------
4+
Builder------------------- Making cars
55
Chain of Responsibility---
66
Command-------------------
77
Composite-----------------

Builder.py

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

4-
class something(object):
5-
4+
#==============================================================================
5+
class Vehicle(object):
6+
7+
def __init__(self, type_name):
8+
self.type = type_name
9+
self.wheels = None
10+
self.doors = None
11+
self.seats = None
12+
13+
def view(self):
14+
print(
15+
"This vehicle is a " +
16+
self.type +
17+
" with; " +
18+
str(self.wheels) +
19+
" wheels, " +
20+
str(self.doors) +
21+
" doors, and " +
22+
str(self.seats) +
23+
" seats."
24+
)
25+
26+
#==============================================================================
27+
class VehicleBuilder(object):
28+
"""
29+
An abstract builder class, for concrete builders to be derived from.
30+
"""
31+
def __init__(self):
32+
self.vehicle = None
33+
34+
def make_wheels(self):
35+
raise NotImplementedError
36+
37+
def make_doors(self):
38+
raise NotImplementedError
39+
40+
def make_seats(self):
41+
raise NotImplementedError
42+
43+
def build(self):
44+
self.make_wheels()
45+
self.make_doors()
46+
self.make_seats()
47+
return self.vehicle
48+
49+
#==============================================================================
50+
class CarBuilder(VehicleBuilder):
51+
652
def __init__(self):
7-
print("initalised")
53+
self.vehicle = Vehicle("Car ")
54+
55+
def make_wheels(self):
56+
self.vehicle.wheels = 4
57+
58+
def make_doors(self):
59+
self.vehicle.doors = 3
60+
61+
def make_seats(self):
62+
self.vehicle.seats = 5
63+
64+
#==============================================================================
65+
class BikeBuilder(VehicleBuilder):
66+
67+
def __init__(self):
68+
self.vehicle = Vehicle("Bike")
69+
70+
def make_wheels(self):
71+
self.vehicle.wheels = 2
72+
73+
def make_doors(self):
74+
self.vehicle.doors = 0
75+
76+
def make_seats(self):
77+
self.vehicle.seats = 2
878

79+
#==============================================================================
80+
if (__name__ == "__main__"):
81+
car_maker = CarBuilder()
82+
car = car_maker.build()
83+
car.view()
984

85+
bike_maker = BikeBuilder()
86+
bike = bike_maker.build()
87+
bike.view()

Design_Patterns_In_Python.tex

Lines changed: 99 additions & 99 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,77 +300,77 @@ \section{Model View View-Model}
300300

301301
\chapter{Summary}
302302

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}
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} (Boston, MA: Addison-Wesley, January 15, 1995)
372+
\bibitem{python-patterns-site} http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style
373+
\end{thebibliography}
374374
\end{document}
375375

376376

Facade.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
class Engine(object):
88

99
def __init__(self):
10+
# how much the motor is spinning in revs per minute
1011
self.spin = 0
1112

1213
def start(self, spin):
1314
if (spin > 2000):
14-
self.spin = spin / 15
15+
self.spin = spin // 15
1516

1617
#==============================================================================
1718
class StarterMotor(object):
1819

1920
def __init__(self):
20-
# how much the motor is spinning in revs per minute
21+
# how much the starter motor is spinning in revs per minute
2122
self.spin = 0
2223

2324
def start(self, charge):

0 commit comments

Comments
 (0)