You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. What is the basic function (功能) of factory method?
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.
In factory method, there is a one-to-one correspondance between concrete product and concrete creator. When a new concrete product is added, we only need to add a concrete creator.
Program to an interface (i.e., Abstract Product and Abstract Creator), not an implement.
Satisfy the open-closed principle.
Partition different tasks (different concrete products) and assign them to different objects (different concrete creators). It satisfy the single responsibility principle (SRP).
2. Please give an example using factory method design pattern.
3. Please give a typical UML class diagram of factory method.
4. Please state the single responsibility principle.
Assign each class a single task: partition different tasks and assign them to different objects.
When a class has multiple tasks, we need to create a family of classes instead of the single class.
5. What is the main idea of parameterized factory method? What is the advantages of parameterized factory method compared with factory method and simple factory?
Parameterized factory can be regarded as a combination of simple factory and factory method. It is still a factory method, but each concrete factory can be parameterized to create different concrete products.
Advantages
Still open for extension and closed for modification. When we want to add a new product, we can simply add a new concrete factory for this product.
At the initial design stage, when there are a lot of concrete products, parameterized factory avoids the large number of concrete factories.
6. Please state the basic function (功能) of abstract factory.
Abstract factory and the derived factories are used to create a family of related objects. It provides an interface for creating families of related or dependent objects without specifying the objects' concrete classes.
7. Please give a typical UML class diagram of abstract factory.
8. What is the key difference among simple factory, factory method, parameterized factory method, and abstract factory?
Simple factory:does not comply with the open-closed principle.
Factory method: easy for extension, but the number of classes increases dramatically when there are many different products.
Parameterized factory method: simple factory + factory method. At the initial design stage, simple factory is used for creating a lot of different products. For future extension, factory method is used for avoiding the modification of existing code.
Abstract factory: for creating a family/series of related products.
Observer
9. What is the basic function (功能) of observer pattern?
Define a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically.
10. Please give an example using observer pattern?