Pages

April 26, 2016

Part 1: Java Design Pattern Interview Questions & Answers

What are Design Patterns?
Design patterns are tried and tested ways to solve particular design issues by various programmers in the world. We can say design patterns are an extension of code reuse.

Design patterns versus frameworks
Are design patterns the same thing as frameworks? The answer to that is NO. Design patterns are more like general guidelines on how to solve specific programming problems, but they do not specify the detailed code that’s necessary to solve those problems.

What are the benefits of using design patterns?
  • Reusable in multiple projects.
  • Design patterns make our code easy to understand and debug.
  • They provide an industry standard approach to solve a recurring problem.
  • Design patterns also provide transparency to the design of an application.

Name types of Design Patterns?
  • Creational Patterns - These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new operators. This gives the program more flexibility in deciding which objects need to be created for a given use case. e.g Singleton, Factory pattern.
  • Structural Patterns - These design patterns concern class and object composition. (Composition is a 'has-a' relationship e.g Room 'has-a' Chair.) Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities. Structural design patterns are concerned with how classes and objects can be composed, to form larger structures.e.g Private Class Data, Decorator etc.
  • Behavioral Patterns - These design patterns are specifically concerned with communication between objects. e.g Template Method, Visitor, Iterator, Observer etc.
What is the SOLID principle in Java? Or What is the open closed design principle in Java?
1). Single Responsibility Principle: One class should have one and only one responsibility. We should write, change and maintain a class for only one purpose.

2). Open Closed Principle: Software components should be open for extension, but closed for modification. It means that your classes should be designed in such a way that whenever fellow developers want to change the flow of control in specific conditions in application, all they need to do is extend your class and override some functions and that’s it. If other developers are not able to design desired behavior due to constraints put by your class, then you should reconsider changing your class.

3). Liskov’s Substitution Principle: Derived types must be completely substitutable for their base types. It means that the classes fellow developers created by extending your class should be able to fit in application without failure. i.e. if a fellow developer poorly extended some part of your class and injected it into the framework/ application then it should not break the application or should not throw fatal exceptions.

4). Interface Segregation Principle: It is applicable to interfaces as the single responsibility principle holds to classes. Clients should not be forced to implement unnecessary methods which they will not use. e.g: If you created an interface Reportable and added two methods generateExcel() and generatedPdf(). Now another developer wants to use this interface but he intends to use reports only in PDF format and not in excel. He will have to implement two methods, out of which one is an extra burden put on him by the designer of software. Either he will implement another method or leave it blank. Which is wrong. Instead of one interface, we should create two interfaces by breaking the existing one. They should be like PdfReportable and ExcelReportable. This will give the flexibility to users to use only required functionality only.

5). Dependency Inversion Principle: Depend on abstractions, not on concretions. We should design our software in such a way that various modules can be separated from each other using an abstract layer to bind them together.

Please CLICK HERE to read Part 2 of Java Design Pattern Interview Questions & Answers in which I tried to explain the Singleton design pattern in detail.

If you enjoy our content and want to support us, please consider donating via  gpay, phonepay or paytm on +91 9920600280. Also, I’d appreciate it if you’d buy me a coffee☕ 

https://www.buymeacoffee.com/greekykhs


Keep learning and growing!
-K Himaanshu Shuklaa..

No comments:

Post a Comment