ISF DP Computer Science

Design Patterns #


Syllabus Topics [HL] #

  • B3.2.5 Explain commonly used design patterns in OOP.

Key Vocabulary #

WordDefinition
SingletonDesign pattern that ensures that a class has only one instance and provides a global point of access to that instance.
FactoryDesign pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
ObserverDesign 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 principleStates 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
๐Ÿ’ป In the Terminal, type the following command to open the lab folder.
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 shell

When you want to exit the shell, you can type exit or ^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 initialize self.count = 0
  • Add a method increment_count(self) that adds 1 to the count
  • In the main code, call s1.increment_count() twice, then print s2.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 a deliver() 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