不懂的请看注解,有故事背景的
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=value; } public String getValue(){ return value; } } package Factory; import dao.Human; /** * 工厂接口 * 这次定一个接口,应该要造不同性别的人,需要不同的生产线 * 那这个八卦炉必须可以制造男人和女人 * @author Mr.Zhang * 八卦炉 */ public interface HumanFactory { //造黄种人 public Human createYellowHuman(); //造黑人 public Human createBlackHuman(); //造白人 public Human createWhiteHuman(); } package Factory; import dao.Human; /** * 男性创建工厂,只创建男性 * @author Mr.Zhang * */ public class MaleHumanFactory extends AbstractHumanFactory { /** * 创建一个男性黄种人 */ @Override public Human createYellowHuman() { // TODO Auto-generated method stub return super.createHuman(HumanEnum.YellowMaleHuman); } /** * 创建一个男性黑人 */ @Override public Human createBlackHuman() { // TODO Auto-generated method stub return super.createHuman(HumanEnum.BlackMaleHuman); } /** * 创建一个男性白人 */ @Override public Human createWhiteHuman() { // TODO Auto-generated method stub return super.createHuman(HumanEnum.WhiteMaleHuman); } } package test; import Factory.FemaleHumanFactory; import Factory.HumanFactory; import Factory.MaleHumanFactory; import dao.Human; /** * 测试类(NvWa) * @author Mr.Zhang *女娲建立起了两条生产线,分别是: *男性生产线 *女性生产线 */ public class NvWa { public static void main(String[] args) { //第一条生产线,男性生产线 HumanFactory maleFoctory=new MaleHumanFactory(); //第二条生产线,女性生产线 HumanFactory femaleFactory = new FemaleHumanFactory(); //生产线建立完毕,开始生产男性黄种人了 Human maleYellowHuman=maleFoctory.createYellowHuman(); //开始生产女性黄种人了 Human femaleYellowHuman = femaleFactory.createYellowHuman(); maleYellowHuman.cry(); maleYellowHuman.laugh(); maleYellowHuman.sex(); System.out.println("-------------------"); femaleYellowHuman.cry(); femaleYellowHuman.laugh(); femaleYellowHuman.sex(); } }
原文地址:http://zhangzhipeng.blog.51cto.com/9115459/1581084