OOP & Design Patterns: Graph Design, Decorator Pattern, String Iterator, Travel Planning, Study notes of Design Patterns

A series of exercises focused on object-oriented programming (oop) and design patterns. The exercises cover various topics such as reverse engineering a graph module design, implementing the decorator pattern in python, creating an ascii character iterator, and solving a travel planning problem using a graph. Each exercise includes detailed instructions and expected outcomes.

Typology: Study notes

2021/2022

Uploaded on 08/05/2022

char_s67
char_s67 🇱🇺

4.5

(116)

1.9K documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
OOP and Design patterns Exercises
1. (20 min) The graph module (provided on the Wiki page) contains a set
of classes for representing graphs. On a piece of paper reverse engineer its
design:
(a) Write down all class names, their methods and public properties; try
to understand what all of them do.
(b) Figure out how different classes are depending on each other.
(c) Use the classes to represent the following graph:
(d) What are the weaknesses of this design? The code contains comments
that raise some questions about the design, think about these.
(e) How would you improve this design? How might an alternative design
look like? We will come back to this later, so don’t spend too much
time on it now.
2. (40 min) Modify the code in starbuzz.py to use the Decorator Pattern.
(a) Define a class BeverageDecorator which is instantiated with a bev-
erage object and contains two methods: get cost which adds the
cost of the decorator to the total drink cost and get description
which updates the description of the drink.
(b) Subclassing BeverageDecorator define new ingredients: Milk and
Cream. Use the ingridients to produce new drinks combinations.
(c) (Optional ) Write unittests for your code and add propper docstrings.
3. (30 min) Implement a Python iterator which iterates over string characters
(ASCII only) returning their ASCII code (obtained by ord function):
(a) Define a new iterator class which contains two methods:
init a constructor taking the ASCII string as a argument,
next returns the ASCII code of the next character or raises a
StopIteration exception if the string end was encountered.
(b) Define a new iterable class which wraps around a string and contains
iter method which returns the iterator instance.
1
pf2

Partial preview of the text

Download OOP & Design Patterns: Graph Design, Decorator Pattern, String Iterator, Travel Planning and more Study notes Design Patterns in PDF only on Docsity!

OOP and Design patterns Exercises

  1. (20 min) The graph module (provided on the Wiki page) contains a set of classes for representing graphs. On a piece of paper reverse engineer its design:

(a) Write down all class names, their methods and public properties; try to understand what all of them do. (b) Figure out how different classes are depending on each other. (c) Use the classes to represent the following graph:

(d) What are the weaknesses of this design? The code contains comments that raise some questions about the design, think about these. (e) How would you improve this design? How might an alternative design look like? We will come back to this later, so don’t spend too much time on it now.

  1. (40 min) Modify the code in starbuzz.py to use the Decorator Pattern.

(a) Define a class BeverageDecorator which is instantiated with a bev- erage object and contains two methods: get cost which adds the cost of the decorator to the total drink cost and get description which updates the description of the drink. (b) Subclassing BeverageDecorator define new ingredients: Milk and Cream. Use the ingridients to produce new drinks combinations. (c) (Optional ) Write unittests for your code and add propper docstrings.

  1. (30 min) Implement a Python iterator which iterates over string characters (ASCII only) returning their ASCII code (obtained by ord function):

(a) Define a new iterator class which contains two methods:

  • init – a constructor taking the ASCII string as a argument,
  • next – returns the ASCII code of the next character or raises a StopIteration exception if the string end was encountered. (b) Define a new iterable class which wraps around a string and contains iter method which returns the iterator instance.

(c) Test your code using explicit calls of next method (see example in the lecture) and for loop. (d) (Optional ) Implement the same functionality using generators. (e) (Optional ) Define a new iterator which returns the same ASCII codes but in a random order. Use it to iterate over your iterable object.

  1. (55 min) In this exercise, we want to solve a travel planning problem based on the graph module. We want to represent a set of cities as nodes in a graph, with edges between nodes representing different kinds of trans- portation. Cities must have a name assigned to them. Transportation edges have three properties (in addition to connecting two cities): the travel time, the cost and the kind of transportation (e.g., a string “train”).

(a) Think about how you can extend the original design to accomodate the new requirements. Can you come up with a better design? For example, the popular NetworkX graph library for Python uses quite a different design approach (you can look at its online documentation for inspiration). (b) Change the design as you like (refactor) and implement the new requirements. If you prefer to stick with the old design you are free to do that as well (e.g., deriving a CityNode and TransporationEdge class). Optionally update the unittests as well (unittests will be discussed later in this school, so you can skip this step for now). (c) Implement the following city graph as an example:

(d) Now find the quickest path from Berlin to Cologne, using the algo- rithm that is already provided in the graph module. (e) Provide the quickest path for alternative weight functions (hint: maybe get rid of inheritance being used to define the weight function). Use this to find the the cheapest and fastest paths between Berlin and Cologne.