首页 > 代码库 > 抽象工厂模式
抽象工厂模式
不懂的请看注解,有故事背景的
package dao; /** * 定义一个人类的统称 * @author Mr.Zhang * */ public interface Human { /** * 首先定义什么事人类 */ //会笑的 public void laugh(); //会哭的 public void cry(); //会说话的 public void talk(); //性别 public void sex(); } package abstractBase; import dao.Human; /** * 黑人 * @author Mr.Zhang * */ public abstract class AbstractBlackHuman implements Human { @Override public void laugh() { System.out.println("黑人会笑"); } @Override public void cry() { System.out.println("黑人会哭"); } @Override public void talk() { System.out.println("黑人会说话"); } } package abstractBase; import dao.Human; /** * 白人 * @author Mr.Zhang * */ public abstract class AbstractWhiteHuman implements Human { @Override public void laugh() { System.out.println("白人会笑"); } @Override public void cry() { System.out.println("白人会哭"); } @Override public void talk() { System.out.println("白人会说话"); } } package abstractBase; import dao.Human; /** * 黄种人 * @author Mr.Zhang * */ public abstract class AbstractYellowHuman implements Human { @Override public void laugh() { System.out.println("黄种人会笑"); } @Override public void cry() { System.out.println("黄种人会哭"); } @Override public void talk() { System.out.println("黄种人会说话"); } } package daoimpl; import abstractBase.AbstractBlackHuman; public class BlackFemaleHuman extends AbstractBlackHuman { @Override public void sex() { System.out.println("该黑人性别为女!"); } } package daoimpl; import abstractBase.AbstractBlackHuman; public class BlackMaleHuman extends AbstractBlackHuman { @Override public void sex() { System.out.println("该黑人性别为男!"); } } package daoimpl; import abstractBase.AbstractWhiteHuman; public class WhiteFemaleHuman extends AbstractWhiteHuman { @Override public void sex() { System.out.println("该白人性别为女!"); } } package daoimpl; import abstractBase.AbstractWhiteHuman; public class WhiteMaleHuman extends AbstractWhiteHuman { @Override public void sex() { System.out.println("该白人性别为男!"); } } package daoimpl; import abstractBase.AbstractYellowHuman; public class YellowFemaleHuman extends AbstractYellowHuman { @Override public void sex() { System.out.println("该黄种人的性别为女!"); } } package daoimpl; import abstractBase.AbstractYellowHuman; public class YellowMaleHuman extends AbstractYellowHuman { @Override public void sex() { System.out.println("该黄种人的性别为男!"); } } package Factory; import dao.Human; /** * 编写一个抽象类,根据enum创建一个人类出来 * @author Mr.Zhang * */ public abstract class AbstractHumanFactory implements HumanFactory { protected Human createHuman(HumanEnum humanEnum){ Human human = null; if(!humanEnum.getValue().equals("")){ try { //直接产生一个实例 human=(Human) Class.forName (humanEnum.getValue()).newInstance(); } catch (Exception e) { //因为使用了enum,这个种异常情况不会产 生了,除非你的enum有问题; e.printStackTrace(); } } return human; } } package Factory; import dao.Human; /** * 女性创建工厂,只创建女性 * @author Mr.Zhang * */ public class FemaleHumanFactory extends AbstractHumanFactory { /** * 创建一个女性黄种人 */ @Override public Human createYellowHuman() { // TODO Auto-generated method stub return super.createHuman(HumanEnum.YellowFemaleHuman); } /** * 创建一个女性黑人 */ @Override public Human createBlackHuman() { // TODO Auto-generated method stub return super.createHuman(HumanEnum.BlackFemaleHuman); } /** * 创建一个女性白人 */ @Override public Human createWhiteHuman() { // TODO Auto-generated method stub return super.createHuman(HumanEnum.WhiteFemaleHuman); } } package Factory; /** * 世界上有哪些类型的人,列出来 * @author Mr.Zhang *(jdk1.5↑才开始引入的类型) */ public enum HumanEnum { BlackFemaleHuman("daoimpl.BlackFemaleHuman"), BlackMaleHuman("daoimpl.BlackMaleHuman"), WhiteFemaleHuman("daoimpl.WhiteFemaleHuman"), WhiteMaleHuman("daoimpl.WhiteMaleHuman"), YellowFemaleHuman("daoimpl.YellowFemaleHuman"), YellowMaleHuman("daoimpl.YellowMaleHuman"); private String value=""; private HumanEnum(String value){ this.value=http://www.mamicode.com/value;"-------------------"); femaleYellowHuman.cry(); femaleYellowHuman.laugh(); femaleYellowHuman.sex(); } }
抽象工厂模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。