Practice Problems For OOM-25
Practice Problems For OOM-25
Q1. Answer the following questions corresponding to the singleton design pattern with
appropriate justification.
(a) How can you achieve thread-safe singleton patterns in Java?
(b) What are the differences between a static class and a singleton class?
(c) Can we create a clone of a singleton object?
(d) What would happen if we do not have a synchronized method for returning Singleton
instance in a multi-threaded environment?
Q3.
(a) Which design pattern suggests a request to pass through multiple classes but only
relevant classes should execute the request?
(b) Which design pattern let the user add new functionality to an existing object without
altering the structure of the object?
(c) Which design pattern is used for accessing elements of a collection object sequentially?
(d) Which design pattern is used for defining the one-to-many dependencies among objects
in a way that if one object’s state changes, the dependents are informed of it?
(e) Which design pattern provides a single class with simplified methods required by the
client and delegate the call to those methods for processing the request?
(f) Which design pattern is data-driven and the action is based on the data provided?
Q4.Streams are a fundamental abstraction in most I/O facilities. A stream can provide an
interface for converting objects into a sequence of bytes or characters. That lets us transcribe
an object to a file or to a string in memory for retrieval later. A straightforward way to do this
is to define an abstract Stream class with subclasses Memory Stream and File Stream. But
suppose we also want to be able to do the following:
• Compress the stream data using different compression algorithms (Runlength encoding,
Lempel-Ziv, etc.).
• Reduce the stream data to 7-bit ASCII characters so that it can be transmitted over an
ASCII communication channel.
Draw Decorator design pattern for obtaining an elegant way to add these responsibilities to
streams.
Q5.Consider a class TCP Connection that represents a network connection. A TCP
Connection object can be in one of several different states: Established, Listening, Closed.
When a TCP Connection object receives requests from other objects, it responds differently
depending on its current state. For example, the effect of an Open request depends on whether
the connection is in its Closed state or its Established state. The State pattern describes how
TCP Connection can exhibit different behavior in each state. The key idea in this pattern is to
introduce an abstract class called TCP State to represent the states of the network connection.
The TCP State class declares an interface common to all classes that represent different
operational states. Subclasses of TCP State implement state-specific behavior. For example,
the classes TCP Established and TCP Closed implement behavior particular to the Established
and Closed states of TCP Connection. The class TCP Connection maintains a state object (an
instance of a subclass of TCP State) that represents the current state of the TCP Connection.
The class Connection delegates all state-specific requests to this state object. TCP Connection
uses its TCP State subclass instance to perform operations particular to the state of the
connection. Whenever the connection changes state, the TCP Connection object changes the
state object it uses. When the connection goes from established to closed, for example, TCP
Connection will replace its TCP Established instance with a TCP Closed instance. Draw State
design pattern for TCP Connection.
Q6.The bridge pattern allows the Abstraction and the Implementation to be developed
independently and the client code can access only the Abstraction part without being
concerned about the Implementation part. Consider the following thread scheduler. We are
given these hierarchies which some designer has come up with. He has the thread scheduler
class here and then he sub-classed it into pre-emptive scheduler and time sliced scheduler.
Further, there is a Unix pre-emptive thread scheduler and Windows pre-emptive thread
scheduler. There are two types of pre-emptive thread scheduler; one for the Unix platform
and another for the Windows platform. Similarly, for the time sliced thread scheduler will
have two sub-classes; the Unix time sliced thread scheduler and the windows time sliced
thread scheduler. But later as maintenance was required, it was used for some time and then
new requirements arose. And then we need to support the Java platform and once we try to
support the Java platform, we saw that we not only should have a Java JVM pre-emptive
thread scheduler, but also the JVM time sliced thread scheduler. So, the hierarchy becomes
more and more complex as new changes occur to either the abstraction or the implementation
concepts, so, we need to apply the bridge pattern to separate out the abstraction and the
implementation and provide a bridge between that. Draw a Bridge design pattern for the
following thread scheduler.
Q7.
Consider the following naïve use of inheritance. Suppose, we have an Account class in a
bank. We have two types of accounts, the cash account and credit account. Additionally, the
customer’s account can belong to an individual or an institution. Further, we need to have
individual cash account and individual credit account and so on. What will be the solution to
above unrelated dimension inheritance problem using Single Responsibility Principle?
Q8. (A) Consider a method being used in an application class as Serve(Serviceable x). We
have an interface Serviceable which has methods f() and g(). A totally unrelated class X has
methods m() and n() already implemented which has behavior of f() and g(). Design all
solutions which make use of m() and n() methods for Serviceable and let method call
Serve(x) working without any error in application class.
(B) Describe how with the help of hooks/primitive methods a template pattern is
implemented. Define template and primitive methods and differentiate between them.
(C) List various ways to add operations in a class along with the constraints which
necessitates them, if you are aware about that some operations need to be added in the class
before designing the class but you don’t know how many and which operations.
(D) Identify the basic principles of object orientation used in creating the design pattern.
Justify your answer.
(E) Define and classify the design pattern along with the examples of each class.
Q9.(A) Consider an order processing system for an international e-commerce company. This
system must be able to process sales orders in many countries. The functions of the sales
order object is allow to fill out the order with GUI, Handle tax calculations, Process the order
and Print the sales receipt. Supposing after writing the applications I receive a request to
change the way of handling the tax. Earlier the application has been written to calculate the
tax using Indian tax rules. Now it is required that it should be able to handle the tax
calculations according to rules in United States or United Kingdom and many more. For
handling these new rules which design pattern is most appropriate and why, also draw its
class diagram.
(B) Describe the need of multiple interfaces and single implementation with the example
code illustrating the various design principles used in the above example.
Q10. (A). Consider a method serve(Serviceable obj), as it can be seen from its signature,
accepts an object exporting interface Serviceable. The interface Serviceable has two methods
f() and g() which are accessed by method serve. The class serviceable is final, however we
have to add certain methods at later point of time in the serviceable class as we are already
aware that this serviceable class is going to have some more behavior added during later
point of time. Design a class diagram hierarchy which provides this feature despite
serviceable being final so it could not be extended.
(B). You have to design a class diagram that provides an interface for creating families
of related or dependent objects without specifying their concrete classes.
(C). Provide an example solution in terms of code or class diagram where if certain
changes are made in the state of an object it should be notified to all the interested recipients.
(D). Describe the pattern which defines a family of algorithms, encapsulate each one and
make them interchangeable as per client context.