Design Pattern Examples
Overview of object-oriented design patterns
program.py
Go to the documentation of this file.
5
6import os
7import sys
8
9from .adapter import Adapter_Exercise
10from .bridge import Bridge_Exercise
11from .command import Command_Exercise
12from .composite import Composite_Exercise
13from .decorator import Decorator_Exercise
14from .facade import Facade_Exercise
15from .flyweight import Flyweight_Exercise
16from .handlerchain import HandlerChain_Exercise
17from .interpreter import Interpreter_Exercise
18from .iterator import Iterator_Exercise
19from .mediator import Mediator_Exercise
20from .memento import Memento_Exercise
21from .nullobject import NullObject_Exercise
22from .observer import Observer_Exercise
23from .proxy import Proxy_Exercise
24from .state import State_Exercise
25from .strategy import Strategy_Exercise
26from .visitor import Visitor_Exercise
27
28from .version import app_version
29
30
31
32class Program:
33
34
35 class Exercise:
36
37
42 def __init__(self, exercise_name, exercise):
43 self.name = exercise_name
44 self.exercise_to_run = exercise
45
46
50
51
52
53 class Options:
54
55
56 def __init__(self):
58
59
62
63
64
68 def Help(self, exercises: list[Exercise]):
69 usage = \
70 "{0} v({1}) by Stephen P. Lepisto\n" + \
71 "usage: {0} [--help][-?][options] [exercise_name][[ exercise_name][...]]\n" + \
72 "\n" + \
73 "Runs through a series of exercises showing off design patterns. If no exercise_name\n" + \
74 "is given, then run through all exercises.\n" + \
75 "\n" + \
76 "Options:\n" + \
77 "--help, -?\n" + \
78 " This help text.\n" + \
79 "--version\n" + \
80 " Show just the version number of this application.\n"
81
82 print(usage.format("DesignPatternExamples_python", app_version))
83
84 print("Exercises available:")
85 for exercise in exercises:
86 print(" {0}".format(exercise.name))
87
88
89 def ShowVersion(self):
90 print(app_version)
91
92
93
101 def ParseOptions(self, args, options : Options, exercises) -> bool:
102 optionsValid = True
103
104 if args:
105 for argument in args:
106 if argument in ["--help", "-?", "/?"]:
107 self.Help(exercises)
108 optionsValid = False
109 break
110 elif argument in ["--version"]:
111 self.ShowVersion()
112 optionsValid = False
113 break
114 options.exercise_names.append(argument)
115 return optionsValid
116
117
118
121 def Run(self, args=None):
122 exercises = [
123 Program.Exercise("Adapter", Adapter_Exercise),
124 Program.Exercise("Bridge", Bridge_Exercise),
125 Program.Exercise("Command", Command_Exercise),
126 Program.Exercise("Composite", Composite_Exercise),
127 Program.Exercise("Decorator", Decorator_Exercise),
128 Program.Exercise("Facade", Facade_Exercise),
129 Program.Exercise("Flyweight", Flyweight_Exercise),
130 Program.Exercise("HandlerChain", HandlerChain_Exercise),
131 Program.Exercise("Interpreter", Interpreter_Exercise),
132 Program.Exercise("Iterator", Iterator_Exercise),
133 Program.Exercise("Mediator", Mediator_Exercise),
134 Program.Exercise("Memento", Memento_Exercise),
135 Program.Exercise("NullObject", NullObject_Exercise),
136 Program.Exercise("Observer", Observer_Exercise),
137 Program.Exercise("Proxy", Proxy_Exercise),
138 Program.Exercise("State", State_Exercise),
139 Program.Exercise("Strategy", Strategy_Exercise),
140 Program.Exercise("Visitor", Visitor_Exercise),
141 ]
142
143 options = Program.Options()
144 if self.ParseOptions(args, options, exercises):
145 for exercise in exercises:
146 if len(options.exercise_names) == 0 or exercise.name in options.exercise_names:
147 exercise.exercise_to_run()
Represents a single exercise or example for a design pattern.
Definition: program.py:35
exercise_to_run
Method to call to run the exercise.
Definition: program.py:44
def __init__(self, exercise_name, exercise)
Constructor.
Definition: program.py:42
Represents the command line options provided to the program, if any.
Definition: program.py:53
Contains all the top-level Design Pattern Examples to match C#.
Definition: program.py:32
bool ParseOptions(self, args, Options options, exercises)
Helper method to parse the given options and store the results in the given Options structure.
Definition: program.py:101
def Run(self, args=None)
Run the specified examples.
Definition: program.py:121
def ShowVersion(self)
Helper method to show just the version of the application.
Definition: program.py:89
def Help(self, list[Exercise] exercises)
Helper method to show usage information for this program.
Definition: program.py:68
static void ShowVersion(void)
Helper function to show just the version of the application.
Definition: program.c:111