抽象工厂模式
抽象工厂模式:
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(); } }
感谢您的阅读,您的支持是我写博客动力。