Design Patterns #
Syllabus Topics [HL] #
- B3.2.5 Explain commonly used design patterns in OOP.
Key Vocabulary #
| Word | Definition |
|---|---|
| Singleton | Design pattern that ensures that a class has only one instance and provides a global point of access to that instance. |
| Factory | Design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. |
| Observer | Design pattern that defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. |
| Open/Closed principle | States that well designed software should be open for extension, but closed for modification"; that is, it allows its behaviour to be extended without modifying its source code. |
[0] Set up #
๐ป Clone your repo in the correct folder.
Be sure to replace yourgithubusername with your actual username.
cd ~/desktop/dpcs/unit03_oop
git clone https://github.com/isf-dp-cs/lab_design_patterns_yourgithubusername
cd lab_design_patterns_yourgithubusername
๐ป Enter the Poetry Shell to start the lab. As a reminder, we will run this command at the start of each lab, but only when we are inside a lab folder.
poetry shell
๐พ ๐ฌ Exiting the poetry shellWhen you want to exit the shell, you can type
exitor^D
[1] Singleton #
๐ป At the bottom of the file singleton.py, create two Singleton objects, and print them out."
s1 = Singleton()
s2 = Singleton()
print(s1)
print(s2)
๐ป In singleton.py, modify the Singleton class to track an “access count.”
- Add an
__init__(self)method to initializeself.count = 0 - Add a method
increment_count(self)that adds 1 to thecount - In the main code, call
s1.increment_count()twice, then prints2.count.
s1.increment_count()
s1.increment_count()
print(s2.count)
[2] Factory #
๐ป In factory.py, modify class a new method of transport type to the shipping company.
- Create a new class (e.g.
Motorbike) that inherits from Transport and give it adeliver()method. - Update
ShippingCompany.get_transport()so that certain input causes the new class to be used. (e.g. if the delivery is on HK Island, use a motorbike) - Use the factory to create the new transport method and deliver an item with it.
The Open/Closed principle means that well designed software should be open for extension, but closed for modification"; that is, it allows its behaviour to be extended without modifying its source code.
[3] Observer #
๐ป Allow parents to subscribe to the Qilin Post.
- Create a Parent class that inherits from Observer.
- Make its update method print: “[ISF Notification]: {state}”.
- In your script, create your Parent and subscribe them to the Qilin Post.
- Unsubscribe ethan the Qilin Post
- Publish the Third issue of the Qilin Post and be sure your parent and ms. genzlinger receive updates.
[4] Deliverables #
โกโจ Once you finish the lab, be sure to complete these two steps:๐ Update Syllabus Checklist: Go to your Syllabus Content Checklist in your Google Drive and update it accordingly.
๐ป Push your work to Github
- git status
- git add -A
- git status
- git commit -m “describe your code here”
- git push
- remote