Implement Singleton Pattern and Abstraction-Based Method
You are collaborating with a team developing a new application for publishing short stories. In the first stage of the project, a class diagram has been created, to lay out the necessary components and the relationships between them. Now, you have received the first version of the implemented program. It has some issues that need to be resolved
Note
In this activity, you will interact with a recording of a lab made on actual devices, rather than interact with the actual devices. In this activity, when entering a command, the entire command must be entered exactly as stated, including matching the case and spaces; shortcuts and tab completion are not supported. In GUIs, you will only be able to use the menu items required for the activity. You will not be able to use other menu items.
You may navigate away from this page once you begin initializing the lab.
You will be notified once the devices are ready.
Implement Singleton Pattern and Abstraction-Based Method
As you can see in the Topology figure, to maintain the modularity of the applications you are developing, you need to put some thoughts into how components interact with each other. Many good practices have been documented in a form of design patterns. Design patterns help with simplifying future modifications of the application, and also make your code easier to understand for other developers who are familiar with the documented design patterns. There are several patterns in the toolbox that you can choose from, each solves a specific problem.
Step 4
Click the AdminView module. Close the Python extension message.
Step 5
Click the StoryModel module. After reviewing the first screen, click the scroll bar to see the remainder.
Step 7
Next, click the app module in the sidebar.
Answer
The app module is used to tie everything together and run the application. It instantiates the model and view and subscribes the view to the model so that it can receive state updates. As a next step, it adds a new story through the admin view, and as a last step, it lists all stories, which in this case should be one.
You will use this module for interacting with the program.
Step 9
Based on the earlier description of the app module, when the application runs, it should set up the observer behavior, add a new story, and then print that story from the admin view. Run the app module and see what happens. In the terminal on the bottom of the screen, type python app.py and press Enter.
Step 10
Open the AdminController module again, and examine the __init__() method.
Answer
The controller creates a new fresh model object, even though the app already created a model object. The problem is that when the view component calls the controller for adding a new story, the controller uses a different model object than the app module that was created initially. A different object means different data. So, the new model object that was initiated in the controller does not have that view in the subscribers list, and there is no synchronization because of that.
Step 12
From the left sidebar, open the StoryModel module in the newer version of the program.
Step 16
Run the app module again, now with the singleton pattern implemented, and observe the output. In the terminal on the bottom of the screen, type python app.py and press Enter.
Step 19
Open the app module and look at how the new view is incorporated into the previous flow.
Step 21
Run the app module from the terminal, and observe the output. In the terminal on the bottom of the screen, type python app.py and press Enter. After reviewing the first screen, click the scroll bar to see the remaining output.
Answer
After the admin state was synced, an error occurred that prevented the syncing of the model and the user view. Tracing the error output, you can conclude that the problem is the notify() method inside the StoryModel class. The notify() method calls the update() method on each of the subscribed objects. But the UserView class does not implement the update() method; it calls it refresh() instead. This problem can be solved easily. You could rename the method update(), and it should be fine. But maybe it would be better to create some sort of contract between the actors in the observer pattern, so that you are sure this will not happen when someone else subscribes a different object that also does not implement the required method.
Step 23
From the sidebar on the left, open the observer module.
Step 24
Inspect the UserView module.
Step 26
Run the app module, and inspect the output. In the terminal on the bottom of the screen, type python app.py and press Enter.
Step 28
Click File > Save to save the file.
Step 29
Open the StoryModel. After reviewing the first screen, click the scroll bar to see the next part of the output. Then click the scroll bar to go back up.
Answer
The StoryModel changed slightly to accommodate the contract with the Observer class. It imports the Observer class and uses it in the subscribe() method. It is used for checking whether the object that wants to subscribe to the model inherits the Observer class. If it does not, then it cannot be put into the observers list. This way, the abstraction contract is fulfilled from both the observer and observable side.
Step 31
Run the app module again in the terminal, and inspect the output. In the terminal on the bottom of the screen, type python app.py and press Enter.
Step 34
Click File > Save to save the file.
Step 35
Click Terminal > New Terminal to open the terminal.
No comments:
Post a Comment