Sei sulla pagina 1di 17

Structural Design Pattern

CSS Session 13
1

Agenda
What are Structural Design Patterns
and how we can make use of them?
Situation 1
Situation 2
Situation 3

Structural Design Patterns


In software engineering, structural design patterns are
design patterns that ease the design by identifying a simple
way to realize relationships between entities.
Like structuring a tree of objects where every object has the
same interface
Or Like, create a simplified interface of an existing interface
to ease usage for common tasks
Or Like a high quantity of objects share a common
properties object to save space
Or like, a chain of processes where the output of each
process is the input of the next
Or like , adapting one interface for a class into one that a
client expects.
Or like, a class functioning as an interface to another thing
3

Situation 1
I have Developer which knows how
to code and Tester who knows how to
test the code.
Now, we want our developer to be a
tester, which means tester methods
would be called on our developer
object.
So, client must not feel difference,
whether he is calling tester or
developer object.

Situation 1

Situation 1 : How did DP helped us ?


We used Adapter pattern.
It helped to allow 2 incompatible
interfaces to work together.
Used when client expects a (target)
interface
Adapter class allows use of available
interface and Target interface.

Adapter Pattern from Java API


Similar situation: If you want to read the system input
through command prompt in java then given below code
is common way to do it.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter String");
String s = br.readLine();
System.out.print("Enter input: " + s);
BufferedReader expects Character Stream, while System.in provide byte stream.
Link for Stream Decoder
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/nio/cs/Str
eamDecoder.java#StreamDecoder.0haveLeftoverChar

Adapter Patter from CSS


Code
XSLProcessor.java
Source xsl = new SAXSource(new
InputSource(is));

Situation 2
I have Perforce Server, and I want to
download the code of XYZ Branch. I have
one constructor to initialize the project
which I want to download and one method
to display the code.
Problem is , now whenever , call this API, it
would start downloading the code. We want
that code should be downloaded every
time, it should check if disk already have
that code, dont download.
Second is , we want to block our API access
to Testing team
9

Situation 2

10

Situation 2: How did DP helped us ?


The proxy design pattern allows you to provide an
interface to other objects by creating a wrapper
class as the proxy. The wrapper class, which is the
proxy, can add additional functionality to the object
of interest without changing the object's code
A proxy may hide information about the real object
to the client.
A proxy may perform optimization like on demand
loading.
A proxy may do additional house-keeping job like
audit tasks.
11

Proxy Design Pattern from


Java distributed
API computing through
The java.rmi package simplifies
remote method invocation. A client (running in a JVM) can
invoke a method on a remote server object (running in a
separate JVM, possibly on a different machine altogether) as if
it were a local object.
To implement this, Java employs the Proxy pattern.
The client holds a reference to a local object that acts as a
proxy for the remote object. This proxy implements the
(remote) interfaces of the remote object, forwards all calls to
that server object, and decodes its responses.
Example : ContractPaymentSoapBindingStub.
contractPaymentUpdate()
try {
java.lang.Object _resp = _call.invoke(new
java.lang.Object[] {posUserId, posUserName,
caUserLoginName, basketId, paymentReceiptNumber,
new java.lang.Double(totalPayment)});

12

Situation 3
Suppose , we have to place C1 Order
which takes us , few parameter from
us
Void createOrder(ActionCode code,
int orderNumber);
ActionCode is class having code as
parameter, to tell C1, which order do
we want to create.

13

Situation 3: How did DP helped us ?


We used FlyWeight Pattern
It is used when we need to create a
large number of similar objects
To reduce memory between shared
objects that are similar in way, rather
than creating new ones

14

FlyWeight Design Pattern from Java


API
The intern() method in class String returns a
canonical representation for the string object. In
particular, if two string objects have the same state
(sequence of characters), then their interned versions
will be the same object. This is an application of the
Flyweight pattern to improve efficiency.
A flyweight is a shared object that can be used in
multiple contexts simultaneously.
Another example of the application of Flyweight
pattern is the sharing of information about a class
among all its instances using an object of type Class.

15

References
http://
en.wikipedia.org/wiki/Structural_patt
ern
http://
en.wikipedia.org/wiki/Adapter_patter
n
http://
en.wikipedia.org/wiki/Flyweight_patte
rn
16

Thank you

17

Potrebbero piacerti anche