码迷,mamicode.com
首页 > 其他好文 > 详细

抽象工厂模式

时间:2017-01-23 14:45:16      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:method   --   cep   person   raw   row   main   tor   中国   

抽象工厂模式:

1、抽象工厂模式是工厂方法模式的扩展【工厂方法模式:仅生产一种产品、抽象工厂模式:生产同一系列的多种产品】

//男孩接口
package com.wch.project;

public interface BoyInterface {

	public void drawBoy();
}


//女孩接口
package com.wch.project;

public interface GirlInterface {

	public void drawGirl();
}


//中国男孩类
package com.wch.project;

public class ChineseBoy implements BoyInterface {

	@Override
	public void drawBoy() {
		// TODO Auto-generated method stub
         System.out.println("----------------chinese boy----------------");
	}

}

//中国女孩类
package com.wch.project;

public class ChineseGirl implements GirlInterface {

	@Override
	public void drawGirl() {
		// TODO Auto-generated method stub
		 System.out.println("----------------chinese girl----------------");
	}

}



//中国工厂
package com.wch.project;

public class ChineseFactory implements PersonFactory {

	@Override
	public BoyInterface getBoy() {
		// TODO Auto-generated method stub
        return new ChineseBoy();
	}

	@Override
	public GirlInterface getGirl() {
		// TODO Auto-generated method stub
		return new ChineseGirl();
	}

}


//美国男孩类
package com.wch.project;

public class AmericanBoy implements BoyInterface {

	@Override
	public void drawBoy() {
		// TODO Auto-generated method stub
		  System.out.println("----------------American boy----------------");
	}

}


//美国女孩类
package com.wch.project;

public class AmericanGirl implements GirlInterface {

	@Override
	public void drawGirl() {
		// TODO Auto-generated method stub
		  System.out.println("----------------American girl----------------");
	}

}


//美国工厂
package com.wch.project;

public class AmericanFactory implements PersonFactory {

	@Override
	public BoyInterface getBoy() {
		// TODO Auto-generated method stub
        return new AmericanBoy();
	}

	@Override
	public GirlInterface getGirl() {
		// TODO Auto-generated method stub
		return new AmericanGirl();
	}

}


//客户端调用
package com.wch.project;

public class WchTest {

	public static void main(String[] args) throws Exception{
		
		/*PersonFactory personFactory = new ChineseFactory();
		BoyInterface boy = personFactory.getBoy();
		boy.drawBoy();*/
		
		
		PersonFactory personFactory = new AmericanFactory();
		GirlInterface girl = personFactory.getGirl();
		girl.drawGirl();
		
	}
	
}

  

抽象工厂模式

标签:method   --   cep   person   raw   row   main   tor   中国   

原文地址:http://www.cnblogs.com/chenweichu/p/6343705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!