



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















Ian Holyer
Ian Holyer
●
They are a way of exploring design space
They are a way of exploring design space
●
But, they are more systematic
But, they are more systematic
●
They summarise other people's experiences
They summarise other people's experiences
●
They are woolly ideas for design
They are woolly ideas for design
●
They are too high level to write as library classes
They are too high level to write as library classes
●
So instead they are just reusable ideas
So instead they are just reusable ideas
●
Another point of view is that they are deficiencies in
Another point of view is that they are deficiencies in
current languages - design patterns should be built into
current languages - design patterns should be built into
the language or should be completely natural everyday
the language or should be completely natural everyday
programming in the language
programming in the language
●
To read about design patterns, there are endless good
To read about design patterns, there are endless good
(and bad) web pages, here are some good ones:
(and bad) web pages, here are some good ones:
home.earthlink.net/~huston2/dp/patterns.html
ootips.org/
www.antipatterns.com/
●
(If patterns are good things to do, “antipatterns” are
(If patterns are good things to do, “antipatterns” are
things to avoid.)
things to avoid.)
●
It is also a good idea to do your own Google searches:
It is also a good idea to do your own Google searches:
design patterns
design patterns in Java
●
The first piece of jargon is GoF which stands for Gang
The first piece of jargon is GoF which stands for Gang
of Four, the 4 authors of the first design patterns book
of Four, the 4 authors of the first design patterns book
●
Some people regard the patterns in the GoF book as the
Some people regard the patterns in the GoF book as the
only patterns you ever need
only patterns you ever need
●
Other people refer to GoF patterns, but allow other
Other people refer to GoF patterns, but allow other
patterns as well
patterns as well
●
We are going to start with structural patterns
We are going to start with structural patterns
●
Most of these are based on delegation
Most of these are based on delegation
●
The idea of delegation is “don't do it yourself , get the
The idea of delegation is “don't do it yourself , get the
right object to do it for you”
right object to do it for you”
●
More specifically, if one object is responsible for doing
More specifically, if one object is responsible for doing
something, it may get another object to do the work
something, it may get another object to do the work
●
Some authors present delegation as a fundamental
Some authors present delegation as a fundamental
design pattern
design pattern
●
It is presented as the idea that when you want to
It is presented as the idea that when you want to
change the facilities that a class provides in some way,
change the facilities that a class provides in some way,
delegation provides a more general way of doing this
delegation provides a more general way of doing this
than inheritance
than inheritance
●
Remember, use inheritance only when you can
Remember, use inheritance only when you can
persuade yourself that there is a real “isa” relationship
persuade yourself that there is a real “isa” relationship
between the classes, and otherwise use delegation
between the classes, and otherwise use delegation
●
To summarise a design pattern, it is conventional to
To summarise a design pattern, it is conventional to
use a UML class diagram 'template'. For Proxy:
use a UML class diagram 'template'. For Proxy:
Thing
ProxyThing RealThing
Program
method(...) method(...)
method(...)
uses
delegates to
●
The UML diagram may not make sense until after you
The UML diagram may not make sense until after you
have looked at some examples and understood the
have looked at some examples and understood the
pattern and, but it does act as a memory aid later
pattern and, but it does act as a memory aid later
(italics means interface)
●
As a first example of proxies, from the Java libraries,
As a first example of proxies, from the Java libraries,
look at the
look at the
immutable
immutable
or
or
unmodifiable
unmodifiable
lists that we
lists that we
have seen before
have seen before
●
In the
In the java.util
java.util package, there is a method:
package, there is a method:
Collections.unmodifiableList(list)
●
It takes an argument list of any kind and returns a
It takes an argument list of any kind and returns a
proxy read-only list object of type UnmodifiableList
proxy read-only list object of type UnmodifiableList
(or UnmodifiableRandomAccessList)
(or UnmodifiableRandomAccessList)
●
If you have a list which is important to your internal
If you have a list which is important to your internal
structure, instead of returning a copy to a caller, you
structure, instead of returning a copy to a caller, you
can call the above method and return a proxy
can call the above method and return a proxy
●
The UML diagram for the list situation is this:
The UML diagram for the list situation is this:
List
UnmodifiableList ArrayList
Program
get(...) get(...)
get(...)
uses
delegates to
●
The proxy list delegates to a real list of some particular
The proxy list delegates to a real list of some particular
type such as ArrayList (though the proxy code only
type such as ArrayList (though the proxy code only
knows that it is a List)
knows that it is a List)
(italics means interface)
●
This is another example of the Proxy pattern (or Cache
This is another example of the Proxy pattern (or Cache
or Pool etc, but we are looking at only one aspect)
or Pool etc, but we are looking at only one aspect)
●
The idea is that database (JDBC) code is often written
The idea is that database (JDBC) code is often written
to open a connection, use it to query, then close it
to open a connection, use it to query, then close it
●
Opening a connection over a network is expensive, so
Opening a connection over a network is expensive, so
this is not good for a high turnover of small queries
this is not good for a high turnover of small queries
●
On a web site, there is only one DB username &
On a web site, there is only one DB username &
password, so only
password, so only
one
one
permanent connection is needed
permanent connection is needed
●
But if the site is busy, one connection is a bottleneck,
But if the site is busy, one connection is a bottleneck,
so you need a
so you need a Pool
Pool of permanent database connections
of permanent database connections
●
A Pool is a cache of identical objects, so it doesn't
A Pool is a cache of identical objects, so it doesn't
matter which one you use
matter which one you use
●
Here is how DB code uses a connection
Here is how DB code uses a connection
Connection c = ...getConnection(...);
... make queries using c ...
c.close();
●
What we want to do is to replace a raw connection by a
What we want to do is to replace a raw connection by a
proxy connection so this code doesn't change
proxy connection so this code doesn't change
●
Suppose we can arrange for
Suppose we can arrange for getConnection()
getConnection() to
to
return a proxy connection, so we can override
return a proxy connection, so we can override close()
close()
●
What does the proxy connection look like?
What does the proxy connection look like?
●
We need a proxy Connection like this
We need a proxy Connection like this
class PoolConnection implements Connection
{
private Connection connection;
private Pool pool;
...
}
●
The PoolConnection object contains references to a
The PoolConnection object contains references to a
raw connection, and to the Pool it comes from
raw connection, and to the Pool it comes from
●
All methods are
All methods are delegated
delegated to the raw connection,
to the raw connection,
except close, which is overridden so that it gives the
except close, which is overridden so that it gives the
raw connection back to the pool without closing it
raw connection back to the pool without closing it
●
Here's how the DB code gets a connection
Here's how the DB code gets a connection
... c = DriverManager.getConnection(...);
●
This is library code that we can't change, but it has
This is library code that we can't change, but it has
been written to manage a variety of Driver objects
been written to manage a variety of Driver objects
●
It takes the name of a driver class as a string and loads
It takes the name of a driver class as a string and loads
that class at run time, so we can create a proxy Driver
that class at run time, so we can create a proxy Driver
class DriverManager {
... Driver d; ... find right driver ...
... Connection c = d.connect(); ...
●
Here's what the DriverManager class looks like
Here's what the DriverManager class looks like
●
We need a proxy Driver class like this:
We need a proxy Driver class like this:
class Pool implements Driver
{
private Driver original;
private Connection[] connections;
...
}
●
The Pool object opens some connections, keeps them
The Pool object opens some connections, keeps them
in an array, keeps track of which ones are in use, and
in an array, keeps track of which ones are in use, and
responds to
responds to connect()
connect() calls by choosing a free one
calls by choosing a free one
●
Other Driver methods are delegated to the original
Other Driver methods are delegated to the original