Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Design Patterns

Quiz # 2
Instructor Dr. Wafa Basit
Total Points-15 Time Allowed-30 minutes
Each question carries 5 points.
Q1. From a design perspective, would it be appropriate to implement a class X via inheritance from class
Y in order to reuse the implementations of the many useful methods already defined in Y even if Y has
other methods that have no meaning in the context of X? Which SOLID design principle will be violated
and how? Explain briefly.

X
No, mere code reuse is not an appropriate use of inheritance. Inheritance defines an “is-a” relationship
between types (classes), so every object of a derived sub-class is also a valid object of the corresponding
super-class. Given that there are methods in Y that do not naturally belong in X, an object of type X
would not be a valid object of type Y, so there is no “is-a” relationship. A more strict design criterion is
the Liskov Substitution Principle, discussed in class; it would also be violated for the specific case of X
and Y.
For mentioning SRP, proper reason is necessary.

Q 2. Robert C. Martin was working for Xerox on a flexible printer system that would not only print but
also perform a variety of printer-related tasks like stapling and faxing. There was one main Job class that
was used by most other tasks. During the development of the system, making a modification to the Job
class therefore affected many other classes and a compilation cycle would take a long time. This made it
very time-consuming to modify the system. Since the big Job class had many methods specific to a
variety of different clients, the solution was to introduce multiple smaller interfaces for each client. A
client would then only operate on a specific, small interface to the Job class. Modifications to the Job
class would then affect a smaller number of interfaces and therefore less classes had to be recompiled.

1. Which SOLID design principle/s were violated by the initial design?SRP +reasons
2. Which principle was introduced by Robert?ISP + reasons
3. There are other S.O.L.I.D. principles that could have been violated by Xerox’s new design. Which
ones, why, and how would you redesign the system to adhere to them?Since job class is
implementing lot of small interfaces which means that ISP is not violated any more however,
big job class is still there violating SRP.
Behavior of Button:
• The button is capable of“sensing” whether it hasbeen activated/deactivated by the user.
• Once a change isdetected, it turns theLamp on, respectivelyoff.

 We cannot reuse Button since it depends directly on Lamp. (But there are plenty of other uses
forButton.)
 Button should not depend on the details represented by Lamp.
 These are symptoms of the real problem (Violation of the Dependency-Inversion Principle).
 The high-levelpolicy (detection of on/off gestures) underlying this (mini) design depends on the low-
level details.
 The underlying abstraction is the detection of on/off gestures and their delegation to a server
objectthat can handle them.
 If the interface of Lamp is changed, Button has to be at least tested or even adjusted, even though
thepolicy that Button represents is not changed!
 To make the high-level policy independent of details we should be able to define it independent of
thedetails of Lamp or any other specific device.

Now Button only depends on abstractions; it can be reused with various classes that implement
Switchable.
Changes in Lamp will not affect Button.
The dependencies have been inverted: Lamp now has to conform to the interface defined by Button.
Actually: both depend on an abstraction!

You might also like