Patrick_DK(疾风摩郎) : D:\jdom\jdom-b7\samples\SAXBuilderDemo.java 采用类似底下的方法应该就可以实现! public class SAXBuilderDemo { /** * <p> * This provides a static entry point for creating a JDOM * <code>{@link Document}</code> object using a SAX 2.0 * parser (an <code>XMLReader</code> implementation). * </p> * * @param args <code>String[]</code> * <ul> * <li>First argument: filename of XML document to parse</li> * <li>Second argument: optional name of SAX Driver class</li> * </ul> */ public static void main(String[] args) { if ((args.length != 1) && (args.length != 2)) { System.out.println( "Usage: java samples.SAXBuilderTest " + "[XML document filename] ([SAX Driver Class])"); return; } // Load filename and SAX driver class String filename = args[0]; String saxDriverClass = null; if (args.length == 2) { saxDriverClass = args[1]; } // Create an instance of the tester and test try { SAXBuilder builder = null; if (saxDriverClass == null) { builder = new SAXBuilder(); } else { builder = new SAXBuilder(saxDriverClass); } Document doc = builder.build(filename); // Create an outputter XMLOutputter outputter = new XMLOutputter(); //outputter.setTrimText(true); //outputter.setExpandEmptyElements(true); outputter.output(doc, System.out); } catch (JDOMException e) { if (e.getRootCause() != null) { e.getRootCause().printStackTrace(); } else { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }}
TO 疾风摩郎 对,是参数化的,关于解决判断过多问题,我原来用的是反射,但ajoo给了我更好的方式,可以使用注册方式,请参看 http://www.csdn.net/Expert/TopicView1.asp?id=717381
TO 疾风摩郎 对,是参数化的,关于解决判断过多问题,我原来用的是反射,但ajoo给了我更好的方式,可以使用注册方式,请参看 http://www.csdn.net/Expert/TopicView1.asp?id=717381
to 微电: if (saxDriverClass == null) { builder = new SAXBuilder(); } else { builder = new SAXBuilder(saxDriverClass); }你看看,还是要用到if then嘛,我觉得理想中的程序应该是完全没有流控制语句的
理想中的程序应该是完全没有流控制语句的 He he. Utopia.if you have a boolean variable v, and code like if(v){}else{} you may be able to replace the "if-else" with a polymorphic function call.Conceptually, by doing that, you are creating state objects. before, you use "if-else" to deal with the two different states, now you use two state objects/classes. Neat, right? ( Actually, Small-talk is doing the same thing. if(v){}else{} is transformed to a OO couterpart like v:ifTrue [block1] :ifFalse [block2]. Java-like syntax would be v.ifTrue(new Callback(){/*block1*/}).ifFalse(new Callback(){/*block2*/}) )We can see, by using OO, we are not reducing the number of states. You still have to deal with all the branches. If we say the v.ifTrue(xx).ifFalse(xx) is neater than "if(v){xx}else{vv}", let's consider the case that we have 5 different boolean variables. How many states we have? 32. if we want to use OO way to represent all different states, we need to create 32 state objects/classes. And, which way do you think more natural? "if-else" or the 32 state objects/classes?
abstract factory means you choose the impl only when it matters to you. if it never matters to you, just randomly pick one at the top level (maybe the main() method)
如在命令行 直接赋予哪个子类!
一般实现都要传参,你可以通过构造函数的参数指定构造的Factory
但是切记返回的一定要是接口,而不是具体的实现类
看源码!
假如项目比较大,参数又多的话考虑可以采用xml来配置!
我想很多web容器的功能实现就是这么干的!
比如连接池!
D:\jdom\jdom-b7\samples\SAXBuilderDemo.java
采用类似底下的方法应该就可以实现!
public class SAXBuilderDemo {
/**
* <p>
* This provides a static entry point for creating a JDOM
* <code>{@link Document}</code> object using a SAX 2.0
* parser (an <code>XMLReader</code> implementation).
* </p>
*
* @param args <code>String[]</code>
* <ul>
* <li>First argument: filename of XML document to parse</li>
* <li>Second argument: optional name of SAX Driver class</li>
* </ul>
*/
public static void main(String[] args) {
if ((args.length != 1) && (args.length != 2)) {
System.out.println(
"Usage: java samples.SAXBuilderTest " +
"[XML document filename] ([SAX Driver Class])");
return;
} // Load filename and SAX driver class
String filename = args[0];
String saxDriverClass = null;
if (args.length == 2) {
saxDriverClass = args[1];
} // Create an instance of the tester and test
try {
SAXBuilder builder = null;
if (saxDriverClass == null) {
builder = new SAXBuilder();
} else {
builder = new SAXBuilder(saxDriverClass);
}
Document doc = builder.build(filename);
// Create an outputter
XMLOutputter outputter = new XMLOutputter();
//outputter.setTrimText(true);
//outputter.setExpandEmptyElements(true);
outputter.output(doc, System.out);
} catch (JDOMException e) {
if (e.getRootCause() != null) {
e.getRootCause().printStackTrace();
} else {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}}
对,是参数化的,关于解决判断过多问题,我原来用的是反射,但ajoo给了我更好的方式,可以使用注册方式,请参看
http://www.csdn.net/Expert/TopicView1.asp?id=717381
对,是参数化的,关于解决判断过多问题,我原来用的是反射,但ajoo给了我更好的方式,可以使用注册方式,请参看
http://www.csdn.net/Expert/TopicView1.asp?id=717381
builder = new SAXBuilder();
} else {
builder = new SAXBuilder(saxDriverClass);
}你看看,还是要用到if then嘛,我觉得理想中的程序应该是完全没有流控制语句的
He he. Utopia.if you have a boolean variable v, and code like
if(v){}else{}
you may be able to replace the "if-else" with a polymorphic function call.Conceptually, by doing that, you are creating state objects. before, you use "if-else" to deal with the two different states, now you use two state objects/classes. Neat, right?
(
Actually, Small-talk is doing the same thing. if(v){}else{} is transformed to a OO couterpart like v:ifTrue [block1] :ifFalse [block2]. Java-like syntax would be v.ifTrue(new Callback(){/*block1*/}).ifFalse(new Callback(){/*block2*/})
)We can see, by using OO, we are not reducing the number of states. You still have to deal with all the branches.
If we say the v.ifTrue(xx).ifFalse(xx) is neater than "if(v){xx}else{vv}", let's consider the case that we have 5 different boolean variables. How many states we have? 32.
if we want to use OO way to represent all different states, we need to create 32 state objects/classes. And, which way do you think more natural? "if-else" or the 32 state objects/classes?
if it never matters to you, just randomly pick one at the top level (maybe the main() method)