and representation are very straight forward for any one to get hold on design patterns.
I understand design patterns as a reusable software templates, which can be applied similar situations to practice the best possible solution, however architecture can be referred in many books but implementation take some time, so here what I share is an implementation of bridge pattern in java.
Problem :
Window and its Implementation both are tend to vary in its behaviour, which could blot a number of combination objects if we do not design them properly, so how to avoid ??
Solution:
Here we can think of simply establishing a bridge between two hierarchies,
· Window
· WindowImpl
Window has its own hierarchy, which shows if a window of a bordered window type or a transient window type, similarly an implementation of this shows a hierarchy which varies in terms of windows implementation, as if window implementation could have its own different types (i.e. PM window and XWindow…)
Below is working java implementation of above explained theory,
Hope content is useful for you at some extent, please write your comment for any further knowledge sharing.
/**
*
* @author TVARUN
*/
public abstract class Window {
WindowImpl wImpl;
public void setwImpl(WindowImpl wImpl) {
this.wImpl = wImpl;
}
public abstract void open();
public abstract void close();
public abstract void drawWindow();
}
/**
*
* @author TVARUN
*/
public class IconWindow extends Window{
private String skin = "ICON";
public IconWindow(WindowImpl wImpl) {
setwImpl(wImpl);
}
@Override
public void open() {
wImpl.open(skin);
}
@Override
public void close() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void drawWindow() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
/**
*
* @author TVARUN
*/
public class TransientWindow extends Window {
private String skin = "TRANSIENT";
public TransientWindow(WindowImpl impl) {
this.wImpl = impl;
}
@Override
public void open() {
wImpl.open(skin);
}
@Override
public void close() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void drawWindow() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
/**
*
* @author TVARUN
*/
public abstract class WindowImpl {
public abstract void open(String skin);
}
/**
*
* @author TVARUN
*/
public class XWindow extends WindowImpl {
@Override
public void open(String skin) {
System.out.println("XWindow opened with "+skin+" skin.");
}
}
/**
*
* @author TVARUN
*/
public class PMWindow extends WindowImpl {
@Override
public void open(String skin) {
System.out.println("PMWindow opened with "+skin+" skin.");
}
}
/**
*
* @author TVARUN
*/
public class Client {
public static void main(String arg[]){
Window icon = new IconWindow(new PMWindow());
icon.open();
icon = new TransientWindow(new PMWindow());
icon.open();
icon = new TransientWindow(new XWindow());
icon.open();
}
}
Thanks, Nice example covered. Well I also write on java and design patterns. (Click on the links to see it). Thanks again
ReplyDelete