Friday, June 28, 2024

Design Patterns

What are the different types of design patterns?

  • Creational design patterns
  • Structural design patterns
  • Behavioural design patterns.

List some design patterns used in micro services?

Some of the creational design patterns used in micro services are

  • Singleton
  • Factory
  • Abstract factory
  • Builder design pattern
  • Template design Pattern.
  • Chain of responsibility.
  • Decorator
  • Adapter design pattern.
Provider pattern
  • Provides an instance type to a generate type of class.
  • For example, spring security uses provider pattern
    • Here the provided delegates a task to manager To provide the OAuth manager or credential manager etc.
  • Provider framework is a system in which multiple service providers implement a service, And the system makes The implementation available to its clients thus decoupling them from the implementation.
  • There are three essential components of a service provider network. It has a interface service provider which all the providers implement a provider registration API which registries the providers and a service access API, which clients used to obtain an instance of the service.
Adapter design pattern
  • It is used to provide Implementation for a code that we cannot change.
  • We create an adapter interface and its implementation which provides methods to make the code we cannot change compatible with our code.
    • In the implementation of this class, we inject the object of class. We want to use from another provider.
  • We can’t change the another provider class directly, but we can change its behaviour in our adapter implementation.
  • It’s a structural design pattern.
  • In Stream classes We use adapter design pattern.
  • In Arrays.tolist() Adapter design pattern is being used.
  • We have two classes which cannot connect with each other.
    • We write an adapter class to connect those interfaces.
  • Needed when legacy code Has to be connected with new code and both are in compatible.
  • We don’t want to modify our client while interacting with legacy code.
  • We introduce an interface adapter that converts the code from our legacy class to new code.
Abstract factory design pattern
  • Abstract factory is a creational design pattern That helps you to produce families of related products without specifying their concrete classes.
  • Decouples the Creation of object, from client side and move it to factory side. So whenever we modify any creation, behaviour of a product or add a new line of product to factory, we do not have to modify the client code.
  • This helps us to maintain Single responsibility, principle, and open/close principal.
  • Abstract factory creates the factories that intern creates the objects.
  • For example, when we have certain downstream systems and based on them, we need to send data. We can create an abstract factory, selecting Downstream system and corresponding data..

 Builder Design Pattern

  • Builder design pattern gives you common solution for building an object.
  • It is used when construction of object is costlier.
  • We want to abstract the construction of object.
  • It is a creational design Pattern.
  • This Pattern is used when we want to build different immutable objects using the same object building process.
  • We don’t want to expose how object should be created.
  • When we want to construct an object partially or based on some logic, we use Builder design pattern.
  • We have an inner class, which is static and an outside class Which will have properties or fields and will have a constructor, and that constructor will be passing the reference of builder.
  • In static class, we will have a build method for every member And all these builder method will return object of itself, so we can chain.
  • There will be a build method, which is finally returning our actual object or constructed object.
  • When we are building a complex object, which has a lot of configurations in it, we use build a design pattern.
  • It is a creational design pattern
  • We only want specific attributes of the object to be configured.
  • Example
What is solid design principle?
  • Single responsibility
    • Every class or every object should have a single responsibility.
  • Open Close Principal
    • Objects or entities must be open for extension, but closed for modification
  • Liskov substitution principle
    • Let QX be a property payable about object of X of type T. Then qy should Be portable for object. Y of type S where Sis a sub type of T
  • Inversion of control
    • A client should never be forced to implement interface that it doesn’t want to use, or client shouldn’t be forced to depend on methods that they do not use.
  • Dependency Inversion
    • Entities must depend on actions, not on concretions, It states that the high-level module must not depend on the low level module, but they should depend on abstractions.
What is the difference between abstract factory and factory design pattern?
  • Abstract factory design pattern is the super set of factory design pattern.
  • If we are dealing with big scenarios, larger scenarios should go with abstract design pattern.
  • For example, we can have an app factory design pattern to create object for different widgets. We send name of the widget and factory will be widget object for us.
  • In factory pattern, we can add checks to the factory class on the implementation. We want to invoke.
  • We can also have another factory pattern to create dimensional information for the charts.
  • When we combine the above two We get a pattern that is abstract factory pattern. This Gives us both the type of chart as well as the information required for that chart. 
  • We can have multiple factory patterns to create same object when we bundle all of them together, we can create an abstract factory pattern And through it, we can give Them fully initialised object.
Chain of Responsibility Pattern
  • Operation solution is defined as chain of handlers where each handler is responsible for following a single task.
  • In case of any issue, the handler twice to transfer to next handle until the list is finished a hand site to stop the process by not passing the problem to the next handler
  • It is a behavioural design pattern which transforms particular behaviour Into standalone objects called as handlers.
  • A concrete handler contains code to process a request Which extends Base handler class. The base handler class implement the handler.
  • The client into act with handler, which is generally an interface. Based on the type of request, the handler type implementation Is called at runtime.
  • The handler is an interface With the common method to handle code.
  • The base handler is an optional class, where the boilerplate code common to all handlers resides.
  • The concrete handlers Are actual operators.
  • Allows us to remove all the order handlers at runtime.
  • Each handler, either process the request or pass it along the chain.
  • A client made trigger any handler within the chain.
  • Use case for example, double authentication.
Decorator Pattern
  • Structural design pattern
  • Let’s see attached new behaviour to an object by lacing this object inside the special wrapper that contains These behaviours
  • The Wrapper class can be abstract class or interface.
  • All decorators are composed of same notifier so that they can be interchanged.
  • Client interact with notify interface which contains wrapped and wrapper class object methods.
  • Concrete component is wrapped class, which defines the basic behaviour, which can be altered by decorators.
  • Best decorator references the wrapped object why are you interface so it can be replace both concrete component and its decorator.
  • These decorator override, the methods of the base decorator, But still make use of either before or after this own execution.
  • The client can wrap components layers of decorators, but must work via the component interface.
  • Wraps and existing object, Allowing you to add a new functionality to that object without altering its structure.
  • Assign extra behaviours to your object at runtime without breaking, the code that uses the object.
  • Applies the single responsibility and open/closed principal.
  • We can create an item, abstract class or interface.
  • The item base decorator may extend The item class.
  • All other decorators classes extend the decorator, but contain an object of entity.
  • So we can use multiple decorators at the same time, thus enhancing the functionality.
What is singleton design pattern?
  • A class using Singleton design pattern allows us to create only one instance of itself.
  • A class, which has only one object at a time is called as Singleton class.
  • When we need a single shared resource, like a configuration setting or a connection to a database.
  • First, we make the constructor as private, then we make the private static instance of the class. Finally, we provide a static method which provides instance of the class.
  • In Singleton design pattern, we create a static instance of the class.
  • Don’t allow user to create instance, with default constructor.
  • Create a static method which returns the instance of a class, the method should be synchronised so that it is thread safe.
  • Use double check, locking inside, get instance, method, one at object level and other at thread level.
    • Use a synchronised block inside
  • Use of double check, locking is noted in recent versions of Java instead use a static inner class.
    • Inner static class are lazily initialised.
  • We make it thread safe by synchronising a method that creates Instance or by using a static initialiser.
  • Using double check, locking, we won’t initialise instance, right away. 
  • We will make a public static method to get the instance and check if the instance is null. Then we will create a synchronised block. We will again check if the instance null. If still null, then we will initialise thus making it Thread safe And initialising, only first time.
  • We can also use Enums, which are by default thread safe since Java 1.5.
    • Enum by default, has a private constructor.