APPENDIX A

image

FAQ

Which of these patterns is/are best?

There is no straightforward answer for this type of question. It depends on many factors (the situation, the demand, the constraints, etc.). But if you know all of these patterns, you will have the flexibility to decide. In real life, it is quite possible that we need to use a combination of these patterns to design a requirement.

Why should we use design patterns?

These are general reusable solutions for software design problems which we see repeatedly in real-world software development. They help us to avoid unnecessary and scattered implementations. Please refer to the section “Introduction” in Chapter 1 for the detailed answer.

What is the difference between a command and a memento pattern?

All actions are stored for the command pattern, but the memento pattern saves the state only on request.

In general, in the command pattern, we frequently see operations like undo’s and redo’s for every action, but the memento pattern does not need that.

What is the difference between the facade pattern and the builder pattern?

The aim of the facade pattern is to make a specific portion of code easier for use. It abstracts details away from the developer.

The builder pattern splits the construction process of an object from the representation. (See our code.) Our Director is calling the same Construct() method to create different types of vehicles (i.e., we can use the same construction process to create multiple types).

What is the difference between the builder pattern and the strategy pattern? It appears that they have similar UML representation.

First of all we must take care of the intent first. The builder pattern falls into the creational pattern and the strategy pattern falls into the behavioral pattern. Their areas of focus are different. With the builder pattern we can use the same construction process to create multiple types and with strategy pattern, we have the freedom to select an algorithm in runtime.

What is the difference between the command pattern and the interpreter pattern?

For the command pattern, commands are basically objects. On the other hand, for the interpreter pattern the commands are sentences. With interpreter, we try to evaluate an expression in an easy manner. Sometimes the interpreter pattern looks convenient, but we must note that the cost of building an interpreter may be significant if the grammar is complex—because we need a new class to evaluate a new rule in the grammar.

What is the difference between the chain of responsibility pattern and the observer pattern?

For observer patterns, all registered observers will be notified/get request (for the change in subject), but in the chain of responsibility, it is possible that we do not need to reach to the end of chain, so all processing objects need not to handle the same scenario. The request can be processed much earlier by some processing object that is placed at the beginning of the chain.

What is the difference between the mediator pattern and the observer pattern?

GOF also mentioned: These are competing patterns. The difference between them is that observer distributes communication by introducing observer and subject objects, whereas a mediator object encapsulates the communication between other objects. Here I’ll request you to consider our example of the mediator pattern. Two workers are always getting messages from their boss. It doesn’t matter whether they like those messages or not. But if they are simple observers, they should have an option to unregister their boss’s control on them, saying “I do not want to see messages from the boss.”

GoF also found that we may face fewer challenges to make reusable observers and subjects than to make reusable mediators, but if we try to understand the flow of communication, the mediator scores higher than the observer.

Consider a situation. You have already implemented an interpreter pattern. Later you found that you need to incorporate an additional way to interpret a special type of expression. What will your preferred option be?

We can combine the power of visitor patterns here. It will help us not to disturb the existing grammar classes.

Can we combine the iterator pattern with the composite pattern?

Yes. To design a recursive structure, this combination is quite common.

In which design pattern must opaque objects be present?

In the memento pattern. Memento itself is an opaque object. Remember that the caretaker is not allowed to make any change there.

What is the difference between the command pattern and the chain of responsibility pattern?

With the chain of responsibility, a request is forwarded to a chain with an expectation that one node of that chain will handle that request. However, we have no idea about who is going to handle that request. But with the command pattern, a request will go to an intended receiver (object).

What is the difference between a singleton class and a static class?

With a singleton class we can create objects. This is the main difference. And it means that we can use the concepts of inheritance and polymorphism (by extending the base class) with a singleton class. Experts also believe that static classes are not easy to mock and test.

Note: We cannot override the static methods in Java, but these methods are bounded during compile time, so in certain situations, a static class that consists of static methods can perform better than a singleton class with several non-static methods.

References

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.237.24