简单工厂

@import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); 


接口类:

package com.jerry.design.simpleFactory.imp;


/**

 * 

 * @author jerry

 *

 */

public interface interfaceTest {

public void test(String name);


}


实现类A:

package com.jerry.design.simpleFactory.impl;


import com.jerry.design.simpleFactory.imp.interfaceTest;


/**

 * 

 * @author jerry

 *

 */

public class ImplA implements interfaceTest {

public void test(String name){

System.out.println(" my ImplA name is:"+name);

}


}


实现类B:

package com.jerry.design.simpleFactory.impl;


import com.jerry.design.simpleFactory.imp.interfaceTest;


/**

 * 

 * @author jerry

 *

 */

public class ImplB implements interfaceTest {

public void test(String name){

System.out.println(" my ImplB name is:"+name);

}


}


工厂类:

package com.jerry.design.simpleFactory.factory;


import com.jerry.design.simpleFactory.imp.interfaceTest;

import com.jerry.design.simpleFactory.impl.ImplA;

import com.jerry.design.simpleFactory.impl.ImplB;


public class Factory {

public static interfaceTest createImp(int type){

interfaceTest test = null;

if(type==1){

test  = (interfaceTest) new ImplA();

}else if(type==2){

test = (interfaceTest) new ImplB();

}

return test;

}


}


测试类:

package com.jerry.design.simpleFactory.factory;


import com.jerry.design.simpleFactory.imp.interfaceTest;


public class Test {


public static void main(String[] args) {

interfaceTest test = Factory.createImp(1);

test.test(" i am jerry!");// my ImplA name is: i am jerry!

test = Factory.createImp(2);

test.test(" i am jerry!");// my ImplB name is: i am jerry!

}

}



总结:简单工厂其实就是要选择实现的类而已。

 

 

 

 

posted on 2012-02-14 22:19 疯狂的蜗牛 阅读(52) 评论(0)  编辑  收藏 所属分类: 设计模式


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

公告

@import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

常用链接

留言簿

随笔档案

文章分类

文章档案

搜索

最新评论