Saturday, February 6, 2021

Design Pattern Notes

1. 4 Structural design patterns are there.

2. Factory design pattern. Super class with multiple subclasses(either through interface, abstract or a normal java class) and based on input(like if-else/switch) , need to return one of the input. Approach to code for interface rather than implementation. Provides abstraction between implementation and client classes through inheritance. Have a single factory class for each sub-class and based on if-else block returns different subclasses.

3. In Abstract factory design pattern, we get rid of if-else block and have a factory class for each sub class. Abstract Factory class will return the the sub-class based on the input factory class. Abstract Factory is "factory of factories".

4. When objects contains a lot of attributes, Builder Pattern solves the issue with a large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that actually provide the final object. Composed of static nested inner classes and build() method.

5. Prototype pattern based on deep/shallow copy.



No comments:

Post a Comment

Top DataStructures Problem from Medium-2

  Array: Find a pair with the given sum in an array Maximum Sum Subarray Problem (Kadane’s Algorithm) Longest Increasing Subsequence Problem...