among them DRY, LSP, OCP, SRP, ISP, DIP 1) What design pattern is used in the co
ID: 3915998 • Letter: A
Question
among them DRY, LSP, OCP, SRP, ISP, DIP
1) What design pattern is used in the code? Explain the reason.
2) If you want to add a new shape – “rectangle”, please either modify the code or
draw a class diagram to show how you can add this new shape to the current design. (No
matter code or model, please clearly mark the adding part, which means please clearly
indicate what you change.)
Explanation / Answer
1. What design pattern is used in the code? Explain the reason.
In given example both OCP and LSP principal are being used. we will understand both the principal one by one.
A. OCP ( Open / Close principal ) :
The basic thumb rule of this principal is any Software entities (classes, modules, functions, etc.) in project should be open for extension, but closed for modification.
Means if we have exsiting product and suddenly one clien t requiremnt comes and say now we need to add some more functionality in the exisitng product. as a good designer one thought always comes into the mind that how we can add the functionality whithout modifying the exsiting chnages beacause modifying the exsitng code requires lot of developement , testing efforets and it always be on high risk that after modifying the changes it might be possible that something which was elarier running but now it is gettign failed so make the chnages in such a ways where without modifying the exisitng code we can add more fucntinality in our modules or project. if we follow this principal then we almost achieve the goal where we do not have to modify the existing running code and we can easily add more methods or behaviours.
i.e
in secode question, it has been asked that If you want to add a new shape – “rectangle”, how we can do it. we will see in below section that how we can add the new fucntinality
The real challenge in implementing the Open/Closed Principle is to identify the place to close the modification points ( closed for modification) so that system don't allow us to modify the exisitng behaviour and only allow to add new functionality beacuse future requirement will never be predict 100% so LSP principal comes into the picture.
B. LSP ( Liskov substitutions principal ) :
Principal says functions that use references to base classes must be able to use objects of the derived class without knowing it which means a parent class can hold the address of child class without knowing the behaviour of child class.
in our code we have parent class or interface shape and child classes ( Circle and Square ) which is implementing parent interface ( shape) and we are using both the references in ShapePattern class in such a way that shape (interface) is holding the reference of circle and square ( dervied class) witout knowing the behaviour of chiled or derived class.
now the question is where we are restricitng the behaviour so that we can say our code is following LSP principal so i would say by implementing interface and defining methods ( draw) in that interface is restricting the behaviour becuase every class which will implement shape interface , that has to implement draw method otherwise programer will face the compliation issue
as we are using encapsulation and polymorphism oops concept.
if we are using both OCP and LSP principal then we can says we are following Dependency Inversion Principle ( use both OCP and LSP strictly) in out code.
2. If you want to add a new shape – “rectangle”, please either modify the code or draw a class diagram to show how
Below given chnages we need to make to add this new shape to the current design.
A. Need to add below class in out code
public class Rectangle implements ShapePattern
{
public void draw(){
System.out.println("Inside Rectangle::draw() method.");
}
}
B. In ShapePattern class need to add condition for rectangle shape
}else if(shapeType.equalsIgnoreCase("RECTANGLE"){
return new Rectangle();
}
C. In PatternDempo class we need to add below two instruction
Shape shape3 = shapePattern.getShape("RECTANGLE");
shape3.draw();
code is written above on same line the code is written in original example.
Now we can see we can easily add new functionality in our existing code without modifying exisitng code ( OCP principal ) and at the same time only the draw method or behaviour is
present so we implemented the draw method in our new rectangle class which follow ( LSP principal )
If you found any difficulty to make understanding on above given concept or answer then let me know i will be adding some more stuff there. Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.