?????????????????
????????????????????????????????Щ??????????????????????????????????????????????????????????????????????????????Щ???????????????????????????
??????????????UML?

????????????????
?????????????????????????????????????????????????????????????????????????????Human??????????
/**
* This is factory patter package
*/
package com.roc.factory;
/**
* ??????????  ????
* @author liaowp
*
*/
public interface Human {
public void say();
}
???????????????????????????????????
/**
* This is factory patter package
*/
package com.roc.factory;
/**
* man  ????
* @author liaowp
*
*/
public class Man implements Human {
/* say method
* @see com.roc.factory.Human#say()
*/
@Override
public void say() {
System.out.println("????");
}
}
/**
* This is factory patter package
*/
package com.roc.factory;
/**???
* @author liaowp
*
*/
public class Woman implements Human {
/* say method
* @see com.roc.factory.Human#say()
*/
@Override
public void say() {
System.out.println("???");
}
}
??????д??????????????????????????????????????????ж?????????
package com.roc.factory;
/**
* ??????
* @author liaowp
*
*/
public class SampleFactory {
public static Human makeHuman(String type){
if(type.equals("man")){
Human man = new Man();
return man;
}else if(type.equals("womman")){
Human woman = new Woman();
return woman;
}else{
System.out.println("??????????");
return null;
}
}
}
???????????????java?????????????????????????????á?
package com.roc.factory;
/**
* ?????????????
* @author liaowp
*
*/
public class SampleFactory1 {
public static Human makeHuman(Class c){
Human human = null;
try {
human = (Human) Class.forName(c.getName()).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
System.out.println("?????????????");
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("????????????????????ж???");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("??????");
e.printStackTrace();
}
return human;
}
}
????????????????
package com.roc.factory;
/**
* ??????????
* @author liaowp
*
*/
public class Client {
public static void main(String[] args) {
//        Human man = SampleFactory.makeHuman("man");
//        man.say();
//        Human womman = SampleFactory.makeHuman("womman");
//        womman.say();
//        Human test = SampleFactory.makeHuman("tttt");
Human man = SampleFactory1.makeHuman(Man.class);
man.say();
Human woman = SampleFactory1.makeHuman(Woman.class);
woman.say();
}
}
??????????????ó???
?????????????????????????.??????????????ж??????????????????????????????????????????????.?????ù????????????????????????????????ξ??????????????????????“????”???????????????????Щ????????δ?????????????????????????????????????????????????????????
?????????????????????????????????????Υ???????????η?????????????????????е?????????????У????????????????????????????????????????μ??????????乤????????????е?????????????????????????????????????????????????????????????????????ж??????????????ж??????????????????鸚???????????????????????????????