Showing posts with label design pattern. Show all posts
Showing posts with label design pattern. Show all posts
Wednesday, 13 February 2019
Learn about Design Patterns Used in Spring Framework
- Dependency injection or inversion of control (IOC):
- Factory Design Pattern:
- Spring BeanFactory Container: – It is the simplest container present in the spring framework which provides the basic support for DI (Dependency Injection). We use the following interface to work with this container. [org.springframework.beans.factory.BeanFactory].
- Spring ApplicationContext Container: – It is another container present in spring container which adds extra enterprise-specific functionality. These functionalities includes the capability to resolve textual messages from a properties file and publishing application events to the attentive event listeners. We use the following interface to work with this container. [org.springframework.context.ApplicationContext]. below are the most commonly used ApplicationContext implementations. FileSystemXmlApplicationContext (need to provide the full path of the XML bean configuration file to the constructor). ClassPathXmlApplicationContext (need to set CLASSPATH of the bean configuration XML file in order to load the metadata of the beans from an XML file). WebXmlApplicationContext (the container loads the XML file within a web application which has metadata of all beans).
- Proxy Design Pattern:
In the proxy design pattern, a class is used to represent the functionality of another class. It is an example of structural pattern. Here, an object is created that has an original object to interface its functionality to the outer world. Proxy design pattern is widely used in AOP, and remoting. - Singleton Design Pattern:
Singleton design pattern ensures that there will exist only the single instance of the object in the memory that could provide services. In the spring framework, the Singleton is the default scope and the IOC container creates exactly one instance of the object per spring IOC container. Spring container will store this single instance in a cache of singleton beans, and all following requests and references for that named bean will get the cached object as return bean. It is recommended to use the singleton scope for stateless beans. We can set up the bean scope as Singleton or prototype (which creates a new bean object for every new request) in the configuration XML file as shown below.1234<!-- A bean definition with singleton scope --><bean id = "..." class = "..." scope = "singleton/prototype"><!-- collaborators and configuration for this bean go here --></bean> - Model View Controller (MVC):
It is a design pattern which comes into picture when we use the spring framework for web programming. Spring MVC is known to be a light weight implementation as controllers are POJOs against traditional servlets which makes the testing of controllers very comprehensive. A controller returns a logical view name and the view selection with the help of a separate ViewResolver. Therefore, Spring MVC controllers can be used along with different view technologies such as JSP, etc. - Front Controller Design Pattern:
The front controller design pattern is a technique in software engineering to implement centralized request handling mechanism which is capable of handling all the requests through a single handler. Such handler can perform the authentication, authorization, and logging or request tracking (i.e., pass the requests to the corresponding handlers). Spring framework provides support for the DispatcherServlet that ensure to dispatch an incoming request to your controllers. - View Helper:
Spring framework provides a large number of custom JSP tags as well as velocity macros which assist in the separation of code from the presentation (i.e., views). - Template method:
Spring framework provides number of template to kick start work and complete that piece of work as the best programming practice such as opening and closing connection for JDBC or JMS, etc. E.g., JdbcTemplate, JmsTemplate, and JpaTemplate.
Dependency injection is a technique in software engineering where an object can supply the dependencies of another object. Such dependency which can be used by an object is known as Service and the injection is the passing of the dependency to an object that uses it. Inversion of control or IOC is a design principle in software engineering through which custom-written computer program portions can receive the control flows from a generic framework. The Spring framework has an IOC container which is responsible for the objects creation, wiring the objects together, configuring these objects and handling the entire life cycle of these objects from their creation until they are completely destroyed. The container has the Dependency Injection (DI) which is used to manage the components present in an application. Such objects are known as Spring Beans. The dependency injection or IOC container is the main principle which is used in the spring framework for the decoupling process.The Spring framework uses the factory design pattern for the objects creation of beans by using the following two approaches.Example: –1234567891011121314package com.eduonix.springframework.applicationcontext;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class App {public static void main(String[] args) {ApplicationContext context = new FileSystemXmlApplicationContext("C:/work/IOC Containers/springframework.applicationcontext/src/main/resources/bean-factory-config.xml");HelloApplicationContext obj = (HelloApplicationContext) context.getBean("helloApplicationContext");obj.getMsg();}}Conclusion: –
In this chapter, we have discussed about the various design patterns which are extensively used by the spring framework. Such design patterns are the best programming practices which makes the spring framework as one of the best framework in Java programming. It is one of the most powerful framework which is widely used by the Java developer across globe in many organizations.
Thursday, 24 January 2019
Design pattern-overview
Design Patterns in Java
- A design patterns are well-proved solution for solving the specific problem/task.
Now, a question will be arising in your mind what kind of specific problem? Let me explain by taking an example.
Problem Given:
Suppose you want to create a class for which only a single instance (or object) should be created and that single object can be used by all other classes.
Suppose you want to create a class for which only a single instance (or object) should be created and that single object can be used by all other classes.
Solution:
Singleton design pattern is the best solution of above specific problem. So, every design pattern has some specification or set of rules for solving the problems. What are those specifications, you will see later in the types of design patterns.
Singleton design pattern is the best solution of above specific problem. So, every design pattern has some specification or set of rules for solving the problems. What are those specifications, you will see later in the types of design patterns.
- But remember one-thing, design patterns are programming language independent strategies for solving the common object-oriented design problems. That means, a design pattern represents an idea, not a particular implementation.
- By using the design patterns you can make your code more flexible, reusable and maintainable.
- It is the most important part because java internally follows design patterns.
- To become a professional software developer, you must know at least some popular solutions (i.e. design patterns) to the coding problems.
Advantage of design pattern:
- They are reusable in multiple projects.
- They provide the solutions that help to define the system architecture.
- They capture the software engineering experiences.
- They provide transparency to the design of an application.
- They are well-proved and testified solutions since they have been built upon the knowledge and experience of expert software developers.
- Design patterns don?t guarantee an absolute solution to a problem. They provide clarity to the system architecture and the possibility of building a better system.
When should we use the design patterns?
We must use the design patterns during the analysis and requirement phase of SDLC(Software Development Life Cycle).
Design patterns ease the analysis and requirement phase of SDLC by providing information based on prior hands-on experiences.
Categorization of design patterns:
Basically, design patterns are categorized into two parts:
- Core Java (or JSE) Design Patterns.
- JEE Design Patterns.
Core Java Design Patterns
In core java, there are mainly three types of design patterns, which are further divided into their sub-parts:
1.Creational Design Pattern
- Factory Pattern
- Abstract Factory Pattern
- Singleton Pattern
- Prototype Pattern
- Builder Pattern.
2. Structural Design Pattern
- Adapter Pattern
- Bridge Pattern
- Composite Pattern
- Decorator Pattern
- Facade Pattern
- Flyweight Pattern
- Proxy Pattern
3. Behavioral Design Pattern
- Chain Of Responsibility Pattern
- Command Pattern
- Interpreter Pattern
- Iterator Pattern
- Mediator Pattern
- Memento Pattern
- Observer Pattern
- State Pattern
- Strategy Pattern
- Template Pattern
- Visitor Pattern
Subscribe to:
Posts (Atom)
40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot
40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot 1. What is Tight Coupling? When a class (ClassA) is depen...