2022-CS264FZ-January
Q1
(a) What is the relationship and difference between the Singleton and Flyweight patterns?
Textbook
- The singleton pattern helps you maintain only one required object in the system.
- In other words, once the required object is created, you cannot create more. You need to reuse the existing object.
- The flyweight pattern is generally concerned about a large number of similar objects, because they may occupy big blocks of memory.
Both the Flyweight and Singleton patterns are concerned with the management and optimization of object instances.
They both contribute to system efficiency, albeit in different ways: Singleton by ensuring only one instance exists, and Flyweight by minimizing memory usage through sharing.
(b) How many types of adapters does the Adapter pattern support? Discuss the differences between them briefly.
- Class Adapter: achieved through inheritance and interface implementation in languages like Java.
- Object Adapter: Utilizes composition instead of inheritance. The object adapter implements the target interface and holds an instance of the Adaptee class.
(c) Explain the idea behind the Abstract Factory pattern.
The Abstract Factory is a pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It has following roles:
- Abstract / Concrete Factory
- Abstract / Concrete Product
(d) In the Facade pattern, is it possible to create more than one facade for a complex subsystem? Provide a justification for your answer.
Yes. Here’s why:
- Reduce Complexity and Performance Optimization: To maintain the simplicity, it might be beneficial to split the functionality into multiple facades, each handling a specific aspect of the subsystem.
- Security Reasons for different User Groups: Different user groups might need different interactions with the underlying complex subsystemsDifferent facades can expose different levels of functionality. For more sensitive parts of the subsystem, a facade could provide limited access, acting as an additional layer of security.
Here’s a simple example:
Imagine a smart home system with a complex subsystem
- UserFacade: For regular users to control lighting, temperature, and entertainment.
- AdminFacade: For administrators to configure all aspects of the system, including security settings.
- MaintenanceFacade: For technicians to diagnose and address issues with the hardware, without giving them access to security settings or user preferences..
(e) The Bridge pattern is similar to the State pattern. Is the statement correct? Why?
Textbook
No. In bridge pattern, items can be in different states, but the key intent was to show that
- How you can avoid tight coupling between the items and their states.
- How you can maintain two different hierarchies and both of them can extend without making an impact to each other.
However, the State pattern is concerned with allowing an object to alter its behavior when its internal state changes.
(The state pattern falls into the behavioral pattern and its intent is different.)
Q2
(a) What are the key advantages associated with the Command pattern?
Textbook
- Requests for creation and the ultimate execution are decoupled. Clients may not know how an invoker is performing the operations.
- You can create macros (sequence of commands).
- New commands can be added without affecting the existing system.
- Most importantly, you can support the undo/redo operations.
(b) In the Chain-of-Responsibility pattern, how can you handle the scenario where the request is not handled when you have reached the end of a chain?
Textbook
One simple solution is to use
try/catch
(or try/finally
or try/catch/finally
) blocks. In the end, if no one can handle the request, you may raise an exception with the appropriate messages and catch the exception in your intended catch block to draw your attention
(c) In the Mediator pattern, suppose that you are reducing the number of interconnections among various objects. What key benefits will you achieve from the reduction?
Textbook
More interconnections among objects can make a monolithic system where the system’s behavior is difficult to change (the system’s behavior is distributed among many objects). As a side effect, you may need to create many subclasses to bring those changes in the system.
(d) In most implementations of the Memento pattern, the Memento class does not have a public setter method. What is the reason behind this?
Textbook
“Only the originator that created a memento is allowed to access it.”
So, if you do not provide a public setter method for your memento class, the caretaker or
client cannot modify the memento instances that are created by an originator.
(e) How can we use the Iterator pattern? Please give an example
Defition
The Iterator pattern is used to provide a standard way to traverse through a collection of objects without exposing the underlying representation.
Example
In a library management system, an iterator can be used to iterate over a collection of books. The iterator provides methods like
next()
to access the next book and hasNext()
to check if more books are available, thereby enabling the client to process each book in the collection in a standardized manner, regardless of how the books are stored internally (e.g., in a list, set, or array).Q3
Suppose that a person is ready to go abroad. His friend asks him to buy a product. Please use the Proxy pattern to describe the scene.
(1) Write a Person
interface with a buyProduct
method.
(2) Write two classes of ProxyBuyPerson
and RealBuyPelrson
inheriting from the Person
interface. Class ProxyBuyPerson
should have a constructor and an object of RealBuyPerson
as its attribute. It should also overload the buyProduct
method to call the buyProduct
method on the RealBuyPerson
attribute.
(3) Class RealBuyPerson
should have a constructor to input a product’s name as its attribute (productName
). It should also overload the buyProduct
method to output a message “I need **(product name)”.
Q4
There is a weather website that publishes some real-time weather statistics (temperature and humidity). There are two subscribers to this website. Whenever the weather information on the website changes, it will be automatically pushed to the subscribers. Please use the Observer pattern to describe the scene.