Skip to main content

Design Pattern

 Spring Core

-------------

=> Spring Core is a base module in Spring Framework.

=> All the other modules of Spring are developed on top of the Spring Core Module only.

=> Spring Core provides fundamental concepts of Spring Framework.

(IOC Container & Dependency Injection)

=> Spring Core is all about Managing dependencies among the classes.

(Creating Objects & Injecting Objects)

---------------------------------------------------------------------------

-> In our application several classes will be available.

-> One class method wants to talk to another class method.

-> We can establish communication among the classes in 2 ways

1) Inheritance

2) Composition


-> If we use Inheritance or Composition then our classes will become tightly coupled.

-> Instead of we are creating objects we can ask Spring Core to manage dependencies among our classes.

---------------------------------------------------------------------------

-> If we want Spring Core Module to manage dependencies among the classes with loosely coupling then we have to develop our classes by following some best practices.

-> "Spring Core" suggesting Developers to follow "Strategy Design Pattern" to develop classes so that "Spring Core" can easily manage dependencies among the classes with loosely coupling.

---------------------------------------------------------------------------

Strategy Design Pattern

---------------------------------------------------------------------------

-> It comes under Behavioural Design Pattern.

-> It enables our code to choose an alogrithm at run time

-> When we have multiple algorithms then we can use this pattern to choose one algorithm at run time

-> It will make our classes loosely coupled

Rules

+++++

1) Favour Compositon over inhertience.

2) Code to interfaces instead of implementation classes.

3) Code should be open for extension and code should be closed for modification



+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Dependency Injection

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

-> The process of injecting one class object into another class object is called as Dependency Injection.

-> We can perform Dependency Injection in 3 ways.

1) Setter Injection

2) Constructor Injection

                3) Field Injection


-> The process of injecting one class object into another class object using Setter method then it is called as Setter Injection.

Ex :: 

   BillCollector bc = new BillCollector();

   bc.setPayment(new CreditCardPayment());

-> The process of injecting one class object into another class object using Constructor is called as Constructor Injection.

Ex::

BillCollector bc1 = new BillCollector(new DebitcardPayment());






-> The process of injecting one class object into another class objcet using variable is called as Field Injection.

Note: If variable is declared as public then we can access that variable outside of the the class we can initialize directley.

                                                   ex : obj.variable = value;  // Initialization

Note: If variable is declared as private then we can't access that variable outside of the class directley. To access private       variables outside of the class we can use Reflection api.



Comments

© 2020 The JavaDS Hub

Designed by Open Themes & Nahuatl.mx.