Java Reference
In-Depth Information
// of Swing components as we've seen before, i.e. using JDialog
getWindow().getJFrame().addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
}
// The following will prevent minimizing
@Override
public void windowIconified(WindowEvent we) {
getWindow().getJFrame().setState(JFrame.NORMAL);
}
});
}
}
import javax.swing.JScrollPane;
public class ScrollingWindowDecorator extends WindowDecorator {
public ScrollingWindowDecorator(Window window) {
super(window);
// Add a simple message just to show the decorator is working:
getWindow().addPanel("Decorated with ScrollingWindowDecorator");
((JScrollPane)
getWindow().getJFrame().getContentPane()).setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
((JScrollPane)
getWindow().getJFrame().getContentPane()).setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
}
}
public class WindowTester {
public static void main(String[] args) {
Window decoratedWindow1 = new NaggingWindowDecorator(new NormalWindow());
Window decoratedWindow2 = new ScrollingWindowDecorator(new
NaggingWindowDecorator(new NormalWindow()));
}
}
Note that this pattern shares a lot of similarities with the bridge pattern. The difference is that in
this pattern, you continue to use the interface ( Window ) to define objects and the abstract class has
to implement this interface as well. In the bridge pattern, the abstract class can (but does not have
to) implement the defined interface and will be used in the client code ( AbstractPrintSpool is used
instead of the Printer interface).
Façade Pattern
The façade pattern also deals with wrapping objects and shares similarities with the adapter pattern.
Again, it is related to the previous patterns:
The adapter pattern converts the interface to the one the client code is expecting.
The bridge pattern adds a layer of abstraction between the client code and implementations
of an interface.
 
Search WWH ::




Custom Search