1 / 3100%
CSIS 354
IMPLEMENTING DESIGN PATTERNS WITH PYTHON ASSIGNMENT
INSTRUCTIONS
OVERVIEW
Software Design Patterns may be thought of as blue prints or recipes for implementing common
models in software. Much like re-using proven classes in Object Oriented design, re-using
proven patterns tends to produce more secure and reliable results often in reduced time compared
to re-inventing the wheel. In fact, some high-level languages have integrated facilities to support
many common patterns with only minimal, if any, user written software. That said, in software
design and construction, often the challenge is to know that a pattern exists and be able to
recognize when it is a good fit for a project. To give you a familiar frame of reference, below is a
Singleton Pattern implemented in Java:
public class MySingleton {
// reference to class instance
private static MySingleton instance = null;
// Private Constructor
private MySingleton() {
instance = this;
}
// Returns single instance to class
public static MySingleton getInstance() {
if (instance == null) {
instance = new MySingleton();
}
return instance;
}
public static void main(String[] args)
{
MySingleton s1 = MySingleton.getInstance();
MySingleton s2 = MySingleton.getInstance();
System.out.println("s1: " + s1 + " s2: " + s2);
}
}
If you run this example, you will see that the address for s1 and s2 are exactly the same. That is
because the pattern restricts the existence of more than one instance in a process. Of course this
Page 1 of 3
CSIS 354
example only implements the pattern but other functionality can be added to this class just as any
other class. The primary features of a singleton are:
1. Private or restricted constructor
2. Internal variable for holding reference to single instance
3. Static method for retrieving the instance
The primary goal of this assignment is not to teach you how to write singleton patterns in
Python, (though that’s part of it), but to familiarize you with the concept of design patterns as
well as give you experience in adapting one into one of your own designs.
INSTRUCTIONS
1. Using an Internet Search Engine, search for common software design patterns. There are
many good resources such as Software Design Pattern which can be found under the
Implementing Design Patterns with Python Resources located on the Implementing
Design Patterns with Python Assignment page. Describe, in your own words, how one
of these patterns could be used in an application or software task. You are expected to
write professionally, with proper grammar and spelling, but you are not required to
adhere to a specific format or cite any sources.
2. Pick one of your previous assignments, either from MindTap or one of your Distributed
Systems assignments and integrate a Singleton Pattern into it. Hint: Any class can be
made into a Singleton.
Test your code completely. At each step of the test take a screen shot and embed it into a Word
document. Submit the Word document to the appropriate assignment.
Submit your Python source files and Design Pattern document.
# Write your code here
from functools import reduce
def read_numbers(filename):
with open(filename, 'r') as file:
return [int(number) for number in file.read().split()]
def sum_numbers(numbers):
return reduce(lambda x, y: x + y, numbers)
def main():
filename=input("enter the file name: ")
numbers = read_numbers(filename)
total = sum_numbers(numbers)
average = total / len(numbers)
print(f"The average is: {average}")
Page 2 of 3
CSIS 354
if __name__ == "__main__":
main()
Page 3 of 3
Powered by TCPDF (www.tcpdf.org)
Students also viewed