Java??????????????
???????????? ???????[ 2016/9/9 10:29:57 ] ??????????????????? Java
?????????????????
????????????????????????????????Щ??????????????????????????????????????????????????????????????????????????????Щ???????????????????????????
??????????????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();
}
}
??????????????ó???
?????????????????????????.??????????????ж??????????????????????????????????????????????.?????ù????????????????????????????????ξ??????????????????????“????”???????????????????Щ????????δ?????????????????????????????????????????????????????????
?????????????????????????????????????Υ???????????η?????????????????????е?????????????У????????????????????????????????????????μ??????????乤????????????е?????????????????????????????????????????????????????????????????????ж??????????????ж??????????????????鸚???????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11