欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

比较有意思的一个模式

程序员文章站 2024-03-12 22:38:32
...
package org.cxz.designPattern.combo;

import java.io.BufferedInputStream;
import java.util.Properties;

public abstract class Father {
private static final String configFilePath = "config.properties";
private static final String key = "className";
protected static String _className = null;
static {
try {
Properties props = new Properties();
props.load(new BufferedInputStream(Father.class
.getResourceAsStream(configFilePath)));
_className = props.getProperty(key);
} catch (Exception e) {
e.printStackTrace();
}
}

public abstract void doBusiness();

public static Father getInstance() {
Father f = null;
try {
f = (Father) Class.forName(_className).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f;
}
}

在程序中不需要关注具体的实现类。
package org.cxz.designPattern.combo;


public class Main2 {
public static void main(String[] args) {
Father.getInstance().doBusiness();
}
}


package org.cxz.designPattern.combo;

public class Son extends Father {

@Override
public void doBusiness() {
System.out.println("Do Business");
}

}

className=org.cxz.designPattern.combo.Son
相关标签: Java F#