Java 6
Java 6
Application Programming in
Java
Week 6
"Java is fun".matches("Java.*")
"Java is cool".matches("Java.*")
"Java is powerful".matches("Java.*")
Converting Characters and Numeric Values to Strings
Design Patterns in Java
• A design patterns are well-proved solution for solving the
specific problem/task.
• Design patterns represent the best practices used by
experienced object-oriented software developers. Design
patterns are solutions to general problems that software
developers faced during software development. These
solutions were obtained by trial and error by numerous
software developers over quite a substantial period of time.
Advantage of design pattern:
1. They are reusable in multiple projects.
2. They provide the solutions that help to define the system
architecture.
3. They capture the software engineering experiences.
4. They provide transparency to the design of an application.
5. They are well-proved and testified solutions since they have been
built upon the knowledge and experience of expert software
developers.
6. 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.
Categorization of design patterns:
Basically, design patterns are categorized into two parts:
• Core Java (or JSE) Design Patterns.
• JEE Design Patterns.
Types of Design Patterns
1.Creational Design Pattern
• Factory Pattern (Today)
• Abstract Factory Pattern
• Singleton Pattern (Today)
• Prototype Pattern
• Builder Pattern. (Friday)
2. Structural Design Pattern
• Adapter Pattern (Friday)
• Bridge Pattern
• Composite Pattern (Friday or self-study)
• Decorator Pattern (Today)
• 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 (Friday or self-study)
• State Pattern
• Strategy Pattern
• Template Pattern
• Visitor Pattern
Factory Pattern
• Factory pattern is one of the most used design patterns in
Java. This type of design pattern comes under creational
pattern as this pattern provides one of the best ways to
create an object.
• In Factory pattern, we create object without exposing the
creation logic to the client and refer to newly created object
using a common interface.
• 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.
How does this principle help us to build better software? Let’s see a
few of its benefits:
Testing — A class with one responsibility will have far fewer test cases
Lower coupling — Less functionality in a single class will have fewer
dependencies
Organization — Smaller, well-organized classes are easier to search than
monolithic ones
2. Open for Extension, Closed for Modification
Simply put, classes should be open for extension, but closed
for modification. In doing so, we stop ourselves from
modifying existing code and causing potential new bugs in
an otherwise happy application.
Of course, the one exception to the rule is when fixing bugs
in existing code.
3. Liskov Substitution
Next up on our list is Liskov substitution, which is arguably the most
complex of the 5 principles. Simply put, if class A is a subtype of class B,
then we should be able to replace B with A without disrupting the
behavior of our program.
4. Interface Segregation
The ‘I ‘ in SOLID stands for interface segregation, and it simply means
that larger interfaces should be split into smaller ones. By doing so, we
can ensure that implementing classes only need to be concerned
about the methods that are of interest to them.
5. Dependency Inversion
The principle of Dependency Inversion refers to the
decoupling of software modules. This way, instead of high-
level modules depending on low-level modules, both will
depend on abstractions.