0% found this document useful (0 votes)
20 views73 pages

Session 02 Design Patterns, Maven, Lombok

The document provides a comprehensive overview of design patterns in Java, categorizing them into creational, structural, and behavioral patterns, along with examples and advantages of each. It also introduces the Lombok project, a Java library that simplifies coding by automating repetitive tasks such as generating getters, setters, and logging. The document emphasizes the importance of design patterns for creating reusable and maintainable code in software development.

Uploaded by

spaguitte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views73 pages

Session 02 Design Patterns, Maven, Lombok

The document provides a comprehensive overview of design patterns in Java, categorizing them into creational, structural, and behavioral patterns, along with examples and advantages of each. It also introduces the Lombok project, a Java library that simplifies coding by automating repetitive tasks such as generating getters, setters, and logging. The document emphasizes the importance of design patterns for creating reusable and maintainable code in software development.

Uploaded by

spaguitte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 73

Session 01

ANTEA TRAINING
Design Patterns in Java
Session 02
• Summary

1. Design Patterns in Java


2. Lombok project
3. Maven
Session 02

Design Patterns in Java


Session 02
Design Patterns in Java
• What is Design Patterns ?
•A design patterns are well-proved solution for solving the
specific problem.
• Example of Problem:
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.
Design Patterns in Java
• Advantage of design pattern:
• They are reusable in multiple projects.

• They provide the solutions that help to define the system


architecture.
• They capture the experiences.

• They provide transparency.

• They are well-proved and testified solutions.

• Designpatterns don’t guarantee an absolute solution to a


problem. They provide clarity to the system architecture.
Design Patterns in Java
• Categorization of design patterns:
• Core Java Design Patterns :

• Creational Design Pattern.

• Structural Design Pattern.

• Behavioral Design Pattern.

• JEE Design Patterns:

• Presentation Layer Design Pattern.

• Business Layer Design Pattern.

• Integration Layer Design Pattern.


Design Patterns in Java

Creational Design Pattern


Core Java Design Patterns
Design Patterns in Java
• Creational design patterns:

• Creational design patterns are concerned with the way of


creating objects. These design patterns are used while
creating an object of a class.

• Hard-Coded code is not the good programming approach.

• Sometimes, the nature of the object must be changed


according to the nature of the program.
Creational Design Pattern
• Factory Method Pattern:
•A Factory Pattern or Factory Method Pattern says that
just define an interface or abstract class for creating an object
but let the subclasses decide which class to instantiate. In
other words, subclasses are responsible to create the instance
of the class.
Creational Design Pattern
• Advantage of Factory Design Pattern

• FactoryMethod Pattern allows the sub-classes to choose the


type of objects to create.

• Usage of Factory Design Pattern:

• When a class doesn't know what sub-classes will be required


to create.
• When a class wants that its sub-classes specify the objects to
be created.
• When the parent classes choose the creation of objects to its
sub-classes.
Creational Design Pattern
• Singleton design pattern:
• Singleton Pattern says that just
"define a class that has only one
instance and provides a global
point of access to it".
• EarlyInstantiation: creation of
instance at load time.
• Lazy Instantiation: creation of
instance when required.
Creational Design Pattern
• Advantage of Singleton design pattern:

• Savesmemory because object is not created at each request.


Only single instance is reused again and again.

• Usage of Singleton design pattern :

• Singletonpattern is mostly used in multi-threaded and


database applications.
• Itis used in logging, caching, thread pools, configuration
settings etc.
Creational Design Pattern
• Prototype Design Pattern:
• Prototype Pattern says that cloning of an existing object
instead of creating new one and can also be customized as
per the requirement.

• This pattern should be


followed, if the cost of
creating a new object is
expensive and resource
intensive.
Creational Design Pattern
• Advantage of Prototype design pattern:

• It hides complexities of creating objects.

• The clients can get new objects without knowing which type
of object it will be.
• Usage of Prototype Pattern

• When the classes are instantiated at runtime.


• When the cost of creating an object is expensive or complicated.
• When you want to keep the number of classes in an application
minimum.
• When the client application needs to be unaware of object creation
and representation.
Design Patterns in Java

Structural Design Pattern


Core Java Design Patterns
Structural Patterns in Java
• Structural design patterns:

• Structuraldesign patterns are concerned with how classes


and objects can be composed, to form larger structures.
• The structural design patterns simplifies the structure by
identifying the relationships.
• Thesepatterns focus on, how the classes inherit from each
other and how they are composed from other classes.
Structural Design Pattern
• Adapter Design Pattern:
• An Adapter Pattern says that just "converts the interface of a
class into another interface that a client wants".
• Inother words, to provide the interface according to client
requirement while using the services of a class with a
different interface.
• The Adapter Pattern is also known as Wrapper.
Structural Design Pattern
• Adapter Design Pattern:
Structural Design Pattern
• Adapter Design Pattern:
• Target
Interface: This is the desired interface class which will
be used by the clients.
• Adapter class: This class is a wrapper class which implements
the desired target interface and modifies the specific request
available from the Adaptee class.
• Adaptee class: This is the class which is used by the Adapter
class to reuse the existing functionality and modify them for
desired use.
• Client: This class will interact with the Adapter class.
Structural Design Pattern
• Advantage of Adapter Pattern:

• It allows two or more previously incompatible objects to


interact.
• It allows reusability of existing functionality.
• Usage of Adapter pattern:

• When an object needs to utilize an existing class with an


incompatible interface.
• When you want to create a reusable class that cooperates with
classes which don't have compatible interfaces.
• When you want to create a reusable class that cooperates with
classes which don't have compatible interfaces.
Structural Design Pattern
• Decorator Design Pattern:
•A Decorator Pattern says that just "attach a flexible
additional responsibilities to an object dynamically".
• In other words, The Decorator Pattern uses composition
instead of inheritance to extend the functionality of an object
at runtime.
• The Decorator Pattern is also known as Wrapper.
Structural Design Pattern
• Decorator Design Pattern:
Structural Design Pattern
• Advantage of Decorator Pattern:
• It provides greater flexibility than static inheritance.
• It enhances the extensibility of the object, because changes are made by
coding new classes.
• It simplifies the coding by allowing you to develop a series of functionality
from targeted classes instead of coding all of the behavior into the object.
• Usage of Decorator Pattern:
• When you want to transparently and dynamically add responsibilities to
objects without affecting other objects.
• When you want to add responsibilities to an object that you may want to
change in future.
• Extending functionality by sub-classing is no longer practical.
Design Patterns in Java

Behavioral Design Pattern


Core Java Design Patterns
Behavioral Design Pattern
• Behavioral Design Pattern:
• Behavioral design patterns are concerned with the interaction
and responsibility of objects.
• In
these design patterns, the interaction between the objects
should be in such a way that they can easily talk to each
other and still should be loosely coupled.
• That means the implementation and the client should be
loosely coupled in order to avoid hard coding and
dependencies.
Behavioral Design Pattern
• Iterator Design Pattern:
• Iterator Pattern is used "to access the elements of an
aggregate object sequentially without exposing its
underlying implementation".
• The Iterator pattern is also known as Cursor.

• In collection framework, we are now using Iterator that is


preferred over Enumeration.
Behavioral Design Pattern
• Iterator Design Pattern:
Behavioral Design Pattern
• Advantage of Iterator Pattern

• It supports variations in the traversal of a collection.

• It simplifies the interface to the collection.

• Usage of Iterator Pattern:

• When you want to access a collection of objects without


exposing its internal representation.
• When there are multiple traversals of objects need to be
supported in the collection.
Behavioral Design Pattern
• Observer Design Pattern:
• AnObserver Pattern says that "just define a one-to-one
dependency so that when one object changes state, all its
dependents are notified and updated automatically".
• The Observer pattern is also known as Dependents or Publish-
Subscribe.
Behavioral Design Pattern
• Advantage of Observer Pattern:

• Itdescribes the coupling between the objects and the


observer.
• It provides the support for broadcast-type communication.

• Usage of Observer Pattern:

• When the change of a state in one object must be reflected in


another object without keeping the objects tight coupled.
• When the framework we writes and needs to be enhanced in
future with new observers with minimal changes.
Behavioral Design Pattern
• State Design Pattern:
•A State Pattern says that "the class behavior changes based
on its state". In State Pattern, we create objects which
represent various states and a context object whose behavior
varies as its state object changes.
• The State Pattern is also known as Objects for States.
Behavioral Design Pattern
• Advantage of State Pattern:

• It keeps the state-specific behavior.

• It makes any state transitions explicit.

• Usage of State Pattern:

• When the behavior of object depends on its state and it must


be able to change its behavior at runtime according to the
new state.
• Itis used when the operations have large, multipart
conditional statements that depend on the state of an object.
Behavioral Design Pattern
• Strategy Design Pattern:
• A Strategy Pattern says that "defines a family of functionality,
encapsulate each one, and make them interchangeable".
• The Strategy Pattern is also known as Policy.
Behavioral Design Pattern
• Advantage of Strategy Pattern:

• It provides a substitute to sub classing.

• It
defines each behavior within its own class, eliminating the
need for conditional statements.
• It
makes it easier to extend and incorporate new behavior
without changing the application.

• Usage of Strategy Pattern:

• When the multiple classes differ only in their behaviors. e.g.


Servlet API.
• It is used when you need different variations of an algorithm.
Design Patterns in Java

JEE Design Pattern


Design Patterns in Java
JEE Design Pattern
• MVC or Model-View-Controller Design Pattern:

• Models are basically objects, or POJO's to be exact, used as


blueprints/models for all of the objects that will be used in
the application.
• Views represent the presentational aspect of the data and
information located in the models.
• Controllers controls both of these. They serve as a connection
between the two. Controllers both instantiate, update and
delete models, populate them with information, and then
send the data to the views to present to the end-user.
JEE Design Pattern
• MVC or Model-View-Controller Design Pattern:
JEE Design Pattern
• Front Controller Design Pattern:

• Aftersending a request, the Front Controller is the first


controller it reaches.
• Basedon the request, it decides which controller is the most
adequate to handle it, after which it passes the request to the
chosen controller.
• The Front Controller is most often used in Web
Applications in the form of a Dispatcher Servlet.
JEE Design Pattern
• Front Controller Design Pattern:
JEE Design Pattern
• Intercepting Filter Design Pattern:

• Filters
are used before the request is even passed to the
adequate controllers for processing.
• These filters can exist in the form of a Filter Chain and include
multiple filters, or simply exist as one Filter.
• Nevertheless, they run checks on authorization,
authentication, supported browsers, whether the request
path violates any constraints and restrictions etc.
JEE Design Pattern
• Intercepting Filter Design Pattern:
JEE Design Pattern
• Composite Entity Pattern Design Pattern:

• TheComposite Entity pattern represents a graph of objects,


which when updated, triggers an update for all the
dependent entities in the graph.
• Itis mainly employed in Enterprise JavaBeans (EJB) which
isn't a very popular API as it has been replaced by other
frameworks and tools like the Spring Framework and its
numerous tools.
JEE Design Pattern
• Composite Entity Pattern Design Pattern:
JEE Design Pattern
• Data Access Object Design Pattern:

• The
Data Access Object pattern, most often shortened to
DAO is a pattern in which objects are dedicated to the
communication with the Data Layer.
• These objects often instantiate "Session Factories" for this
purpose and handle all of the logic behind communicating
with the database.
• The
standard practice is to create a DAO interface, followed
by a concrete class implementing the interface and all
methods defined in it.
JEE Design Pattern
• Data Access Object Design Pattern:
JEE Design Pattern
• Service Locator Design Pattern:

•A pattern often seen in Web Applications, the Service Locator


pattern is used to decouple the Service Consumers and the
concrete classes like DAO implementations.
• The pattern looks for the adequate service, saves it in cache
storage to reduce the number of requests and therefore the
strain on the server and provides the application with their
instances.
• TheService Locator pattern is also known as IoC (inversion of
control or dependency injection).
JEE Design Pattern
• Service Locator Design Pattern:
JEE Design Pattern
• Transfer Object Design Pattern:

• This
pattern is used to transfer objects with lots of fields and
parameters in one go.
• The Transfer Object pattern employs new objects, used only
for transfer purposes, usually passed to the DAO.
• Theseobjects are serializable POJOs. They have fields, their
respective getters and setters, and no other logic.
JEE Design Pattern
• Transfer Object Design Pattern:
Session 02

Lombok project
Session 02
Lombok Project
• Introduction to Project:
• Project Lombok is a java library that automatically plugs into
your editor and build tools, to avoid the repetitive code.
• Available for most of IDEs like Jetbrains IntelliJ IDEA.

• Available for most Build Tools like maven, gradle.

• Never write another getter or equals method again, with one


annotation your class has a fully featured builder, Automate
your logging variables, and much more.
Lombok Project
• Getters/Setters, Constructors:
Lombok Project
• Getters/Setters, Constructors:
Lombok Project
• Lazy Getter or Lazy Loading:

• Often, applications need to perform some expensive


operation and save the results for subsequent use.
• We need to read static data from a file or a database. It's
generally a good practice to retrieve this data once and then
cache it to allow in-memory reads within the application.
•A common pattern is to retrieve this data only when it's first
needed. In other words, only get the data when the
corresponding getter is called the first time. This is
called lazy-loading.
• Thissaves the application from repeating the expensive
operation.
Lombok Project
• Lazy Getter or Lazy Loading:
Lombok Project
• ToString() and EqualsAndHashCode() :

• We need to maintain is when generating toString(),


equals() and hashCode() methods.
• IDEs try to help with templates for autogenerating these in
terms of our class attributes.
• We can automate this with Lombok class-level annotations:

• @ToString: will generate a toString() method including all


class attributes. No need to write one ourselves and
maintain it as we enrich our data model.
• @EqualsAndHashCode: will generate
both equals() and hashCode() methods by default
considering all relevant fields.
Lombok Project
• Ensure Your Resources Are Released:

• Lombok provides an alternative way of achieving this, and


more flexibly via @Cleanup.

• Use it for any local variable whose resources you want to


make sure are released. No need for them to implement any
particular interface, you'll just get its close() method called.
Lombok Project
• Annotate Your Class to Get a Logger:

• Many of us add logging statements to our code sparingly by


creating an instance of a Logger from our framework of
choice.

• This
is such a common pattern that Lombok developers have
cared to simplify it for us:
Lombok Project
• Automate Objects Composition:

• Lets consider the following cases :

• We want Users and Customers to share some common


attributes for contact information's.
• We can define an interface and an adapter class for contact
information's

• Lombok's @Delegate can effectively compose the contact


information's to our Users and Customers.
Lombok Project
• Automate Objects Composition:
Lombok Project
• Automate Objects Composition:
Lombok Project
• Automate Objects Composition:
Lombok Project
• Conclusion Lombok Project:
• Thereare some other features we have not presented, I'd
encourage you to take a deeper dive into the official web site
for more details and use cases.

https://projectlombok.org/

• Also most functions we've shown have a number of


customization options you may find handy to get the tool
generate things the most compliant with your team practices
for naming etc.
Session 02

Maven Automation Tool


Session 02
Maven Automation Tool
• What is Maven?

• Maven is a automation tool that can be used for building and


managing any Java-based project.
• Maven make the day-to-day work of Java developers easier
and generally help with the comprehension of any Java-based
project.
• Maven simplifies the build process like ANT. But it is too
much advanced than ANT.
• Maven is used generally for projects build, dependency and
documentation.
• Maven is available for most of IDEs like Eclipse or Jetbrains
IntelliJ IDEA.
Maven Automation Tool
• What maven does?

• Maven can easily build a project.

• Maven can add jars and other dependencies of the project


easily.
• Maven provides project information (log document,
dependency list, unit test reports etc.)
• Maven is very helpful for a project while
updating central
repository of JARs and other dependencies.
• With Maven we can build any number of projects into output
types like the JAR, WAR etc without doing any scripting.
• Using maven we can easily integrate our project with source
control system (such as Git).
Maven Automation Tool
• When we should use Maven?

• If
there are a lot of dependencies for the project. Then it is
easy to handle those dependencies using maven.
• If
the dependency version update frequently. Then one has
to only update version ID in Pom file to update dependencies.
• Continuous builds, integration, and testing can be easily
handled by using maven.
• When one needs an easy way to Generating documentation
from the source code, Compiling source code, Packaging
compiled code into JAR files or ZIP files.
Maven Automation Tool
• How maven works?
Maven Automation Tool
• Core Concepts of Maven:
• POM Files: are XML file that contains information related to the
project and configuration information used by Maven to build
the project.
• Dependencies and Repositories: Dependencies are external
Java libraries required for Project and repositories are
directories of packaged JAR files.
• Build Life Cycles, Phases and Goals: A build life cycle consists of
a sequence of build phases, and each build phase consists of a
sequence of goals.
• Build Profiles: Build profiles a set of configuration values which
allows you to build your project using different configurations.
Maven Automation Tool
• The sample of pom.xml:
Maven Automation Tool
• Maven Repository:

• First maven searches in Local repository then Central


repository then Remote repository if Remote repository
specified in the POM.
Responsible Name of the document manager
File name Session 02 Design Patterns, Maven, Lombok.pptx
Revisions
R01 Data rev. Farouk Korteby Creation of the document

This document is © 2021 Antea s.r.l.

You might also like