Skip to content

Structural Pattern


Flyweight  object can be shared: Now either that whole object is sharable and u store it in a cache. Or you can partition that object into shareable and non shareable part – where in the intrinsic state is the cached one, the extrinsic state is provided by the client.

Reduced memory footprint of the object created – is called flyweight.


Composite pattern:
Problem: Need to treat the container and component in a similar manner – do not wish to differentiate between the two – remove the complexity of handling leaf and component differently.

There can be different layers of nesting of containers within containers – and in the end it has a leaf.


Two flavors – in some cases only the container class has the additional methods , in other cases – all operations even if not required by leaf are pushed up to parent component so that both inherit it.


Proxy Pattern

So first you create the .class file with that name – and having all the methods.Gets all methods , fields etc and writes them to output stream -> Byte Array output stream. Proxy builds a Java class into a byte array and then loads it into the JVM with a private native defineClass0 method.

Proxy is an interface that has a method called newProxyInstance that takes the class, interface, and the wrapper invoker.

The class and interface are used to define a runtime class CL1

CL1 {
//InvocationHandler h;

//has all the methods of INTF1

CL1 – the runtime created class has a reference to the wrapper impl h.
It uses the calls of all the methods to be delegated to h.

Once CL1 is loaded in memory after being defined through reflection and a native call named defineClass0 then the constructor of the class is called with the parameter of h. and a new instance of CL1 is created.

This instance is returned back as the proxy instance. All calls now on this instance will call the invoke method of the wrapper class and will be upto the wrapper how it delegates back to the original subject.

Proxy pattern – do not use a wrapper class because of additional methods not implemented limitations.Need to take care of the

  • exceptions that can be thrown.
  • equals method

Structural patterns: proxy,adapter, composite and decorator.

In proxy – clients talk to subject not to the implementation (hide the impl) . In decorator clients talk to the implementation class that has added on features. Dynamic proxy uses only interfaces (JavaCompiler allows subclassing and also for improved performance).

CGLIB Proxy -vs – JDK Proxy….


class adapter – override
object adapter – does not allow to override methods

public constructr(final Enumeration en)

this(new Iterator() { //using the en over here to next() { en.nextElement();}
// question is: how are we using the final variables in the nested anonymous classes?

Object Adapter: implements target interface and delegates calls to nested objects inside. (Delegation) /OR/ extends the target class and delegates to nested objects.

Class Adapter: Extends the existing class and implements target interface and call’s the super class methods

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: