**DONOTDELETE**
(Unregistered)
07/31/03 09:43 AM
Not all Borders in BorderFactory are Flyweights

Actually only RaisedBevelBorder, LoweredBorder and EtchedBorder use shared instances and thus are implemented as flyweights. For all the other Borders (in JDK1.3.1: LineBorder, TitledBorder, EmptyBorder, CompoundBorder, and MatteBorder) the BorderFactory returns a new instance!

You can test it with the following or better take a look at the sources under "BorderFactory.java"
Border b1, b2;
b1 = BorderFactory.createCompoundBorder();
b2 = BorderFactory.createCompoundBorder();
System.out.println( "CompoundBorder="+ ((b1 == b2) ? "flyweight" : "no"));
b1 = BorderFactory.createEmptyBorder();
b2 = BorderFactory.createEmptyBorder();
System.out.println( "EmptyBorder="+ ((b1 == b2) ? "flyweight" : "no"));
b1 = BorderFactory.createEtchedBorder();
b2 = BorderFactory.createEtchedBorder();
System.out.println( "EtchedBorder="+ ((b1 == b2) ? "flyweight" : "no"));
b1 = BorderFactory.createLineBorder(Color.blue);
b2 = BorderFactory.createLineBorder(Color.blue);
System.out.println( "LineBorder="+ ((b1 == b2) ? "flyweight" : "no"));
b1 = BorderFactory.createLoweredBevelBorder();
b2 = BorderFactory.createLoweredBevelBorder();
System.out.println( "LoweredBevelBorder="+ ((b1 == b2) ? "flyweight" : "no"));
b1 = BorderFactory.createRaisedBevelBorder();
b2 = BorderFactory.createRaisedBevelBorder();
System.out.println( "RaisedBevelBorder="+ ((b1 == b2) ? "flyweight" : "no"));
b1 = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.blue);
b2 = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.blue);
System.out.println( "MatteBorder="+ ((b1 == b2) ? "flyweight" : "no"));
b1 = BorderFactory.createTitledBorder("");
b2 = BorderFactory.createTitledBorder("");
System.out.println( "TitledBorder="+ ((b1 == b2) ? "flyweight" : "no"));

which outputs the following:

CompoundBorder=no
EmptyBorder=flyweight
EtchedBorder=flyweight
LineBorder=no
LoweredBevelBorder=flyweight
RaisedBevelBorder=flyweight
MatteBorder=no
TitledBorder=no



Contact us JavaWorld

Powered by UBB.threads™ 6.5.5

Featured White Papers


RESEARCH CENTERS: Java Standard Edition | Java Enterprise Edition | Java Micro Edition | Development Tools
About Us | Advertise | Contact Us | Terms of Service/Privacy
Copyright, 2006-2008 Network World, Inc. All rights reserved.