4 完整解决方案
图6 界面皮肤库结构图
[java] view plaincopy
-
//在本实例中我们对代码进行了大量简化,实际使用时,界面组件的初始化代码较为复杂,还需要使用JDK中一些已有类,为了突出核心代码,在此只提供框架代码和演示输出。
-
//按钮接口:抽象产品
-
interface
Button { -
public void display(); - }
-
-
//Spring按钮类:具体产品
-
class
SpringButton implementsButton { -
public void display() { -
System.out.println("显示浅绿色按钮。"); -
} - }
-
-
//Summer按钮类:具体产品
-
class
SummerButton implementsButton { -
public void display() { -
System.out.println("显示浅蓝色按钮。"); -
} - }
-
-
//文本框接口:抽象产品
-
interface
TextField { -
public void display(); - }
-
-
//Spring文本框类:具体产品
-
class
SpringTextField implementsTextField { -
public void display() { -
System.out.println("显示绿色边框文本框。"); -
} - }
-
-
//Summer文本框类:具体产品
-
class
SummerTextField implementsTextField { -
public void display() { -
System.out.println("显示蓝色边框文本框。"); -
} - }
-
-
//组合框接口:抽象产品
-
interface
ComboBox { -
public void display(); - }
-
-
//Spring组合框类:具体产品
-
class
SpringComboBox implementsComboBox { -
public void display() { -
System.out.println("显示绿色边框组合框。"); -
} - }
-
-
//Summer组合框类:具体产品
-
class
SummerComboBox implementsComboBox { -
public void display() { -
System.out.println("显示蓝色边框组合框。"); -
} - }
-
- //界面皮肤工厂接口:抽象工厂
-
interface
SkinFactory { -
public Button createButton(); -
public TextField createTextField(); -
public ComboBox createComboBox(); - }
-
- //Spring皮肤工厂:具体工厂
-
class
SpringSkinFactory implementsSkinFactory { -
public Button createButton() { -
return new SpringButton(); -
} -
-
public TextField createTextField() { -
return new SpringTextField(); -
} -
-
public ComboBox createComboBox() { -
return new SpringComboBox(); -
} - }
-
- //Summer皮肤工厂:具体工厂
-
class
SummerSkinFactory implementsSkinFactory { -
public Button createButton() { -
return new SummerButton(); -
} -
-
public TextField createTextField() { -
return new SummerTextField(); -
} -
-
public ComboBox createComboBox() { -
return new SummerComboBox(); -
} - }
[java] view plaincopy
-
import
javax.xml.parsers.*; -
import
org.w3c.dom.*; -
import
org.xml.sax.SAXException; -
import
java.io.*; -
-
public
class XMLUtil { -
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
-
public static Object getBean() { -
try { -
//创建文档对象 -
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); -
DocumentBuilder builder = dFactory.newDocumentBuilder(); -
Document doc; -
doc = builder.parse(new File( "config.xml")); -
-
//获取包含类名的文本节点 -
NodeList nl = doc.getElementsByTagName_r("className"); -
Node classNode=nl.item(0).getFirstChild(); -
String cName=classNode.getNodeValue(); -
-
//通过类名生成实例对象并将其返回 -
Class c=Class.forName(cName); -
Object obj=c.newInstance(); -
return obj; -
} -
catch(Exception e) { -
e.printStackTrace(); -
return null; -
} -
} - }
[java] view plaincopy