抽象工厂模式中:使用反射来获取XML文件中对应的服务信息,来创建对象,调用相应的服务,实现不同的功能需求。使用抽象工厂是减低程序之间的耦合度(零耦合,抽象耦合,具体耦合)
static PartFactory aPartFactory = null;
public static PartFactory get(String fName)
{
try
{
aPartFactory = (PartFactory)Class.forName(fName).newInstance();
}
catch (Exception e)
{
e.printStackTrace();
aPartFactory = null;
}
return aPartFactory;
}
public static void main(String[] args)
{
PartFactory a = get("com.zzz.xbliuc.ab.BPartFactory");
House house = new House(a.createWindow(), a.createGlass());
System.out.println(house);
}
----------------------------------------
1.定义一个文件(panel.properties)
1=com.rt.factory.Fan
2=com.rt.factory.Yuan
3=com.rt.factory.Gong
2.引用
package com.rt.factory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Factory {
private static Properties shapes = new Properties();
static{
try {
InputStream in = Factory.class.getResourceAsStream("panel.properties");
shapes.load(in);//将配置文件中的信息加载到shapes对象中
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Shape getShape(int type){
try {
//获得与形状类型匹配的形状类名
String className = (String)shapes.get(String.valueOf(type));
//通过fanshe机制构造形状对象
return (Shape)Class.forName(className).newInstance();
} catch (Exception e) {
return null;
}
}
-------------反射示例------------
public static void main(String[] args) throws NumberFormatException,
IOException, ClassNotFoundException, InstantiationException,
IllegalAccessException {
System.out.print("请输入运算类型: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String sopl = br.readLine();
// 创建工厂对象
Factoty fy = new Factoty();
Operat a = fy.createOpe(sopl);
System.out.print(":");
BufferedReader n1 = new BufferedReader(new InputStreamReader(System.in));
double d1 = Double.parseDouble(n1.readLine());
System.out.print(":");
BufferedReader n2 = new BufferedReader(new InputStreamReader(System.in));
double d2 = Double.parseDouble(n1.readLine());
a.Operats(d1, d2);
System.out.println("结果 : " + a.getResult(d1, d2));
}
}
public class Factoty {
public Operat createOpe(String name) throws ClassNotFoundException,
InstantiationException, IllegalAccessException {
Operat operat = null;
Class classs = Class.forName(name);// 获取运算类的类对象
Object obj = classs.newInstance();// 获取运算类的实例对象
operat = (Operat) obj;// 强制类型转换
return operat;
}
}
public class Add extends Operat {
@Override
public double getResult(double a, double b) {
return a + b;
}
}
public class Div extends Operat {
@Override
public double getResult(double a, double b) {
return a / b;
}
}
public class Mul extends Operat {
@Override
public double getResult(double a, double b) {
return a * b;
}
}
public class Sub extends Operat {
@Override
public double getResult(double a, double b) {
return a - b;
}
}
public abstract class Operat {
private double a;
private double b;
public void Operats(double a, double b) {
this.a = a;
this.b = b;
}
public abstract double getResult(double a, double b);
}
-----------------------------------------
//反射示例
public class ReflectionTest {
public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
getReflection();
}
public static void getReflection() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
//自定义一个用户信息类,创建一个对象
UserInfo uInfo = new UserInfo();
Class c = uInfo.getClass();
//1.反射类中所有的公有属性
Field[] files = c.getFields();
for (int i = 0; i < files.length; i++) {
//System.out.println("所有公有属性:"+files[i].getName());
}
System.out.println("");
//2.反射类中所有的属性
Field[] filess = c.getDeclaredFields();
for (int i = 0; i < filess.length; i++) {
//System.out.println("所有的属性:"+filess[i].getName());
}
System.out.println("");
//3.反射类中私有属性
Field f = c.getDeclaredField("uId");//私有属性uId
f.setAccessible(true);
Integer i = (Integer)f.get(uInfo);
//System.out.println("反射类中私有属性的值: "+i+"\n");
//4.反射类中构造方法的参数
Constructor[] conts = c.getConstructors();
for (int j = 0; j < conts.length; j++) {
Class[] ty = conts[j].getParameterTypes();
for (int k = 0; k < ty.length; k++) {
//System.out.println("反射类中构造方法的参数: "+ty[k].getName()+" ");
}
}
System.out.println("");
//5.获取反射类的父类和接口
Class[] interfaces = c.getInterfaces();
for (Class class1 : interfaces) {
//System.out.println("实现的接口:"+class1.getName());
}
Class superClass = c.getSuperclass();
//System.out.println("继承的父类:"+superClass.getName()+"\n");
//6.获取反射类的方法,返回类型,参数名及参数类型
Method[] methods = c.getMethods();
for (Method method : methods) {
//System.out.println("所有的方法: "+"方法名:"+method.getName()+" ,返回类型:"+method.getReturnType().getName());
Class[] pTypes = method.getParameterTypes();
if(pTypes.length <= 0)continue;
//System.out.print("(");
for (int j = 0; j < pTypes.length; j++) {
//System.out.print("方法的参数:"+pTypes[j].getName());
if(pTypes.length > j+1){
//System.out.print(", ");
}
}
//System.out.println(") ");
System.out.println(" ");
}
}
}
static PartFactory aPartFactory = null;
public static PartFactory get(String fName)
{
try
{
aPartFactory = (PartFactory)Class.forName(fName).newInstance();
}
catch (Exception e)
{
e.printStackTrace();
aPartFactory = null;
}
return aPartFactory;
}
public static void main(String[] args)
{
PartFactory a = get("com.zzz.xbliuc.ab.BPartFactory");
House house = new House(a.createWindow(), a.createGlass());
System.out.println(house);
}
----------------------------------------
1.定义一个文件(panel.properties)
1=com.rt.factory.Fan
2=com.rt.factory.Yuan
3=com.rt.factory.Gong
2.引用
package com.rt.factory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Factory {
private static Properties shapes = new Properties();
static{
try {
InputStream in = Factory.class.getResourceAsStream("panel.properties");
shapes.load(in);//将配置文件中的信息加载到shapes对象中
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Shape getShape(int type){
try {
//获得与形状类型匹配的形状类名
String className = (String)shapes.get(String.valueOf(type));
//通过fanshe机制构造形状对象
return (Shape)Class.forName(className).newInstance();
} catch (Exception e) {
return null;
}
}
-------------反射示例------------
public static void main(String[] args) throws NumberFormatException,
IOException, ClassNotFoundException, InstantiationException,
IllegalAccessException {
System.out.print("请输入运算类型: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String sopl = br.readLine();
// 创建工厂对象
Factoty fy = new Factoty();
Operat a = fy.createOpe(sopl);
System.out.print(":");
BufferedReader n1 = new BufferedReader(new InputStreamReader(System.in));
double d1 = Double.parseDouble(n1.readLine());
System.out.print(":");
BufferedReader n2 = new BufferedReader(new InputStreamReader(System.in));
double d2 = Double.parseDouble(n1.readLine());
a.Operats(d1, d2);
System.out.println("结果 : " + a.getResult(d1, d2));
}
}
public class Factoty {
public Operat createOpe(String name) throws ClassNotFoundException,
InstantiationException, IllegalAccessException {
Operat operat = null;
Class classs = Class.forName(name);// 获取运算类的类对象
Object obj = classs.newInstance();// 获取运算类的实例对象
operat = (Operat) obj;// 强制类型转换
return operat;
}
}
public class Add extends Operat {
@Override
public double getResult(double a, double b) {
return a + b;
}
}
public class Div extends Operat {
@Override
public double getResult(double a, double b) {
return a / b;
}
}
public class Mul extends Operat {
@Override
public double getResult(double a, double b) {
return a * b;
}
}
public class Sub extends Operat {
@Override
public double getResult(double a, double b) {
return a - b;
}
}
public abstract class Operat {
private double a;
private double b;
public void Operats(double a, double b) {
this.a = a;
this.b = b;
}
public abstract double getResult(double a, double b);
}
-----------------------------------------
//反射示例
public class ReflectionTest {
public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
getReflection();
}
public static void getReflection() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
//自定义一个用户信息类,创建一个对象
UserInfo uInfo = new UserInfo();
Class c = uInfo.getClass();
//1.反射类中所有的公有属性
Field[] files = c.getFields();
for (int i = 0; i < files.length; i++) {
//System.out.println("所有公有属性:"+files[i].getName());
}
System.out.println("");
//2.反射类中所有的属性
Field[] filess = c.getDeclaredFields();
for (int i = 0; i < filess.length; i++) {
//System.out.println("所有的属性:"+filess[i].getName());
}
System.out.println("");
//3.反射类中私有属性
Field f = c.getDeclaredField("uId");//私有属性uId
f.setAccessible(true);
Integer i = (Integer)f.get(uInfo);
//System.out.println("反射类中私有属性的值: "+i+"\n");
//4.反射类中构造方法的参数
Constructor[] conts = c.getConstructors();
for (int j = 0; j < conts.length; j++) {
Class[] ty = conts[j].getParameterTypes();
for (int k = 0; k < ty.length; k++) {
//System.out.println("反射类中构造方法的参数: "+ty[k].getName()+" ");
}
}
System.out.println("");
//5.获取反射类的父类和接口
Class[] interfaces = c.getInterfaces();
for (Class class1 : interfaces) {
//System.out.println("实现的接口:"+class1.getName());
}
Class superClass = c.getSuperclass();
//System.out.println("继承的父类:"+superClass.getName()+"\n");
//6.获取反射类的方法,返回类型,参数名及参数类型
Method[] methods = c.getMethods();
for (Method method : methods) {
//System.out.println("所有的方法: "+"方法名:"+method.getName()+" ,返回类型:"+method.getReturnType().getName());
Class[] pTypes = method.getParameterTypes();
if(pTypes.length <= 0)continue;
//System.out.print("(");
for (int j = 0; j < pTypes.length; j++) {
//System.out.print("方法的参数:"+pTypes[j].getName());
if(pTypes.length > j+1){
//System.out.print(", ");
}
}
//System.out.println(") ");
System.out.println(" ");
}
}
}