package com.pdw.pattern;
interface Me{
?public void sayHello();
?public void sayBay();
}
class SampleFacotry implements Me{
?public void sayHello() {
??// TODO Auto-generated method stub
??System.out.println("sampleFacotry->SayHello....");
?}
?public void sayBay() {
??// TODO Auto-generated method stub
??System.out.println("sampleFacotry->Bay....");
?}
?
}
class PPFacotry implements Me{
?public void sayHello() {
??// TODO Auto-generated method stub
??System.out.println("PPFacotry-->"+"Say Hello");
?}
?public void sayBay() {
??// TODO Auto-generated method stub
??System.out.println("PPFacotry-->"+"Say Bay.");
?}
?
}
? /**
?? * 以一般工廠方法構(gòu)造類
?? * @author Administrator
?? *
?? */
class CreateFacotry{
?public static Me createFacotry(String aa){
??if(aa.equalsIgnoreCase("Sample")){
???return new SampleFacotry();
??}else if(aa.equalsIgnoreCase("")){
???return new PPFacotry();
??}else{
???return null;
??}
?}
}
/**
?* 操象工廠的實(shí)現(xiàn)。
?* @author Administrator
?*
?*/
abstract class CFacory{
?public abstract SampleFacotry mecreator();
?public abstract PPFacotry kkmeicreator();
}
class CFacoryImple extends CFacory{
?@Override
?public SampleFacotry mecreator() {
??// TODO Auto-generated method stub
??return new SampleFacotry();
?}
?@Override
?public PPFacotry kkmeicreator() {
??// TODO Auto-generated method stub
??return new PPFacotry();
?}
?
}
public class Facotry {
?public static void main(String[] args){
??Me a=CreateFacotry.createFacotry("Sample");
??a.sayHello();
??CFacoryImple cfi=new CFacoryImple();
??Me me=cfi.mecreator();
??Me ppme=cfi.kkmeicreator();
??me.sayBay();
??ppme.sayBay();
?}
}
以上這個(gè)例程,說明了一般工廠以及操象工廠的實(shí)現(xiàn)。
工廠這個(gè)構(gòu)造模式在開發(fā)中會(huì)經(jīng)常用到
posted on 2006-06-28 22:54
有貓相伴的日子 閱讀(293)
評(píng)論(0) 編輯 收藏 所屬分類:
Patterns