0% found this document useful (0 votes)
2 views

Lec_7 Java Design Patterns Part 1 (1)

Uploaded by

mohamedhaider254
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lec_7 Java Design Patterns Part 1 (1)

Uploaded by

mohamedhaider254
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Java Design Patterns

Java Design Patterns 1


Outline
▪ Introduction to Design Patterns
▪ Pattern’s Elements
▪ Types of Design Patterns
▪ Java Design Patterns
▪ The Factory Pattern
▪ The Abstract Factory Pattern
▪ The Builder Pattern
▪ The Prototype Pattern
▪ The Singleton Pattern
▪ The Adapter Pattern
▪ The Bridge Pattern
▪ The Composite Pattern
▪ Java BluePrints Patterns Catalog

Java Design Patterns 2


Design Patterns
:: What is a Design Pattern?
“Each pattern describes a problem which occurs over
and over again in our environment, and then describes
the core of the solution to that problem, in such a way
that you can use this solution a million times over,
without ever doing it the same way twice.” [1]
[Christopher Alexander]

Design patterns capture best practices of


the
experienced object-oriented software developers.
Design patterns are solutions to general software
development problems.

Java Design Patterns 3


Design Patterns
:: Pattern’s Elements – Pattern Name

In general, a pattern has four essential elements.

▪ The pattern name


▪ The problem
▪ The solution
▪ The consequences

Java Design Patterns 4


Design Patterns
:: Pattern’s Elements – The Pattern Name

The pattern name is a handle we can use to describe a design


problem, its solutions, and consequences in a word or two.

▪ Naming a pattern immediately increases the design vocabulary.


It lets us design at a higher level of abstraction.
▪ Having a vocabulary for patterns lets us talk about them.
▪ It makes it easier to think about designs and to communicate
them and their trade-offs to others.

Java Design Patterns 5


Design Patterns
:: Pattern’s Elements – The Problem

The problem describes when to apply the pattern.

▪ It explains the problem and its context.


▪ It might describe specific design problems such as how to
represent algorithms as objects.
▪ It might describe class or object structures that are
symptomatic of an inflexible design.
▪ Sometimes the problem will include a list of conditions that
must be met before it makes sense to apply the pattern.

Java Design Patterns 6


Design Patterns
:: Pattern’s Elements – The Solution

The solution describes the elements that make up the design, their
relationships, responsibilities, and collaborations.

▪ The solution doesn't describe a particular concrete design or


implementation, because a pattern is like a template that can be
applied in many different situations.
▪ Instead, the pattern provides an abstract description of a design
problem and how a general arrangement of elements (classes
and objects in our case) solves it.

Java Design Patterns 7


Design Patterns
:: Pattern’s Elements – The Consequences

The consequences are the results and trade-offs of applying the


pattern.

▪ The consequences for software often concern space and time


trade-offs.
▪ They may address language and implementation issues as well.
▪ Since reuse is often a factor in object-oriented design, the
consequences of a pattern include its impact on a system's
flexibility, extensibility, or portability.
▪ Listing these consequences explicitly helps you understand and
evaluate them

Java Design Patterns 8


Design Patterns
:: Types
Erich Gamma, Richard Helm, Ralph Johnson and John Vlisides
in their Design Patterns book define 23 design patterns divided
into three types:
▪ Creational patterns are ones that create objects for you, rather
than having you instantiate objects directly. This gives your
program more flexibility in deciding which objects need to be
created for a given case.
▪ Structural patterns help you compose groups of objects into
larger structures, such as complex user interfaces or accounting
data.
▪ Behavioral patterns help you define the communication
between objects in your system and how the flow is controlled
in a complex program.

Java Design Patterns 9


Java Design Patterns
:: Why Use Patterns with Java?

▪ They have been proven. Patterns reflect the experience,


knowledge and insights of developers who have successfully
used these patterns in their own work.

▪ They are reusable. Patterns provide a ready-made solution that


can be adapted to different problems as necessary.

▪ They are expressive. Patterns provide a common vocabulary of


solutions that can express large solutions succinctly.

▪ J2EE provides built in patterns.

Java Design Patterns 10


Java Design Patterns
:: Creational Patterns and Java I
▪ The creational patterns deal with the best way to create instances
of objects.
▪ In Java, the simplest way to create an instance of an object is by
using the new operator.

Fred = new Fred(); //instance of Fred class

▪ This amounts to hard coding, depending on how you create the


object within your program.
▪ In many cases, the exact nature of the object that is created could
vary with the needs of the program and abstracting the creation
process into a special “creator” class can make your program
more flexible and general.
Java Design Patterns 11
Java Design Patterns
:: Creational Patterns and Java II
▪ The Factory Pattern provides a simple decision making class
that returns one of several possible subclasses of an abstract base
class depending on the data that are provided.
▪ The Abstract Factory Pattern provides an interface to create
and return one of several families of related objects.
▪ The Builder Pattern separates the construction of a complex
object from its representation.
▪ The Prototype Pattern starts with an initialized and instantiated
class and copies or clones it to make new instances rather than
creating new instances.
▪ The Singleton Pattern is a class of which there can be no more
than one instance. It provides a single global point of access to
that instance.

Java Design Patterns 12


The Factory Pattern
:: How does it Work?
The Factory pattern returns an instance of one of several possible
classes depending on the data provided to it.

▪ Here, x is a base class and classes xy and xz are derived from it.
▪ The Factory is a class that decides which of these subclasses to
return depending on the arguments you give it.
▪ The getClass() method passes in some value abc, and returns
some instance of the class x. Which one it returns doesn't matter
to the programmer since they all have the same methods, but
different implementations.
Java Design Patterns 13
The Factory Pattern
:: The Base Class
▪ Let's consider a simple case where we could use a Factory class. Suppose we
have an entry form and we want to allow the user to enter his name either as
“firstname lastname” or as “lastname, firstname”.
▪ Let’s make the assumption that we will always be able to decide the name
order by whether there is a comma between the last and first name.

class Namer { //a class to take a string apart into two names
protected String last; //store last name here
protected String first; //store first name here
public String getFirst() {
return first; //return first name
}
public String getLast() {
return last; //return last name
}
}
Java Design Patterns 14
The Factory Pattern
:: The First Derived Class
In the FirstFirst class, we assume that everything before the last
space is part of the first name.
class FirstFirst extends Namer {
public FirstFirst(String s) {
int i = s.lastIndexOf(" "); //find separating space
if (i > 0) {
first = s.substring(0, i).trim(); //left = first name
last =s.substring(i+1).trim(); //right = last name
} else {
first = “” // put all in last name
last = s; // if no space
}
}
}

Java Design Patterns 15


The Factory Pattern
:: The Second Derived Class
In the LastFirst class, we assume that a comma delimits the last
name.
class LastFirst extends Namer { //split last, first
public LastFirst(String s) {
int i = s.indexOf(","); //find comma
if (i > 0) {
last = s.substring(0, i).trim(); //left= last name
first = s.substring(i + 1).trim(); //right= first name
} else {
last = s; // put all in last name
first = ""; // if no comma
}
}
}

Java Design Patterns 16


The Factory Pattern
:: Building the Factory
The Factory class is relatively simple. We just test for the existence
of a comma and then return an instance of one class or the other.
class NameFactory {
//returns an instance of LastFirst or FirstFirst
//depending on whether a comma is found
public Namer getNamer(String entry) {
int i = entry.indexOf(","); //comma determines name order
if (i>0)
return new LastFirst(entry); //return one class
else
return new FirstFirst(entry); //or the other
}
}

Java Design Patterns 17


The Factory Pattern
:: Using the Factory
NameFactory nfactory = new NameFactory();
String sFirstName, sLastName;
….
private void computeName() {
//send the text to the factory and get a class back
namer = nfactory.getNamer(entryField.getText());
//compute the first and last names using the returned class
sFirstName = namer.getFirst();
sLastName = namer.getLast();
}

Java Design Patterns 18


?
Java Design Patterns 19

You might also like