原文(talkchen于2001/01/17 15:33粘贴)
一个活生生的设计模式的例子,JDK1.2.2源代码,用来说明FACTORY设计模式的,在最后面我加上了自己的理解
--------------------------------------------------------------------------------
package javax.swing;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JComponent;
import javax.swing.border.*;
public class BorderFactory
{
/** Don't let anyone instantiate this class */
private BorderFactory() {
}
//// LineBorder
///////////////////////////////////////////////////////////
public static Border createLineBorder(Color color) {
return new LineBorder(color, 1);
}
public static Border createLineBorder(Color color, int thickness)
{
return new LineBorder(color, thickness);
}
//// BevelBorder
///////////////////////////////////////////////////////////
static final Border sharedRaisedBevel = new
BevelBorder(BevelBorder.RAISED);
static final Border sharedLoweredBevel = new
BevelBorder(BevelBorder.LOWERED);
public static Border createRaisedBevelBorder() {
return createSharedBevel(BevelBorder.RAISED);
}
public static Border createLoweredBevelBorder() {
return createSharedBevel(BevelBorder.LOWERED);
}
public static Border createBevelBorder(int type) {
return createSharedBevel(type);
}
public static Border createBevelBorder(int type, Color highlight,
Color shadow) {
return new BevelBorder(type, highlight, shadow);
}
public static Border createBevelBorder(int type,
Color highlightOuter, Color highlightInner,
Color shadowOuter, Color shadowInner) {
return new BevelBorder(type, highlightOuter, highlightInner,
shadowOuter, shadowInner);
}
static Border createSharedBevel(int type) {
if(type == BevelBorder.RAISED) {
return sharedRaisedBevel;
} else if(type == BevelBorder.LOWERED) {
return sharedLoweredBevel;
}
return null;
}
//// EtchedBorder
///////////////////////////////////////////////////////////
static final Border sharedEtchedBorder = new EtchedBorder();
public static Border createEtchedBorder() {
return sharedEtchedBorder;
}
public static Border createEtchedBorder(Color highlight, Color
shadow) {
return new EtchedBorder(highlight, shadow);
}
//// TitledBorder
///////////////////////////////////////////////////////////
public static TitledBorder createTitledBorder(String title) {
return new TitledBorder(title);
}
public static TitledBorder createTitledBorder(Border border) {
return new TitledBorder(border);
}
public static TitledBorder createTitledBorder(Border border,
String title) {
return new TitledBorder(border, title);
}
public static TitledBorder createTitledBorder(Border border,
String title,
int titleJustification,
int titlePosition) {
return new TitledBorder(border, title, titleJustification,
titlePosition);
}
public static TitledBorder createTitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont) {
return new TitledBorder(border, title, titleJustification,
titlePosition, titleFont);
}
public static TitledBorder createTitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont,
Color titleColor) {
return new TitledBorder(border, title, titleJustification,
titlePosition, titleFont, titleColor);
}
//// EmptyBorder
///////////////////////////////////////////////////////////
final static Border emptyBorder = new EmptyBorder(0, 0, 0, 0);
public static Border createEmptyBorder() {
return emptyBorder;
}
public static Border createEmptyBorder(int top, int left,
int bottom, int right) {
return new EmptyBorder(top, left, bottom, right);
}
//// CompoundBorder
//////////////////////////////////////////////////////////
public static CompoundBorder createCompoundBorder() {
return new CompoundBorder();
}
public static CompoundBorder createCompoundBorder(Border
outsideBorder,
Border insideBorder) {
return new CompoundBorder(outsideBorder, insideBorder);
}
//// MatteBorder
///////////////////////////////////////////////////////////
public static MatteBorder createMatteBorder(int top, int left,
int bottom, int right,
Color color) {
return new MatteBorder(top, left, bottom, right, color);
}
public static MatteBorder createMatteBorder(int top, int left,
int bottom, int right,
Icon tileIcon) {
return new MatteBorder(top, left, bottom, right, tileIcon);
}
}
/***************************************************************************
*1、类的构造函数是私有的,目的在于不让任何人进行实例化
*2、类的方法都是一些共有的、静态的方法,所以这个类无需实例化就可以使用它的方法
*3、这个类有一些静态border对象,可以直接用于其他的对象的共享使用,当生成默认的不带参数的border
* 时,会直接返回这些实例;sharedRaisedBeval,sharedLowerBevel,sharedEtchedBorder,emptyBorder
* 当带有参数的时候,就会生成具体border对象。
*4、通过这个类的作用,它屏蔽了具体的子类的生成,对外提供一种方便的接口
*/
======
原文(huhu71于2001/01/19 13:32粘贴)
你的注释很好,要了解更多的设计模式的使用,可以
--------------------------------------------------------------------------------
看看Jakarta的项目,各种模式的应用都能够从源代码中找到答案。
比如:Turbine,ECS,以及最简单的Jyve等等,多读一些顶级程序员写出来的源程序,你什么都知道了。
=====
请问那里有这些东西,能告诉我吗?thank you in advance
- <0b> talkchen 2001/01/19 17:07 (5次点击)