关闭

抽象工厂(一)

227人阅读 评论(0) 收藏 举报
分类:
public interface Human {
	void laugh();
	void cry();
	void talk();
	void sex();
}


public abstract class AbstractYellowMan implements Human {
    public static String TAG = AbstractYellowMan.class.getSimpleName();
	@Override
	public void laugh() { 
		System.out.println(TAG +" laugh");
	}

	@Override
	public void cry() { 
		System.out.println(TAG +" cry");
	}

	@Override
	public void talk() { 
		System.out.println(TAG +" talk");
	} 
}


public abstract class AbstractWhiteMan implements Human {
    public static String TAG = AbstractWhiteMan.class.getSimpleName();
	@Override
	public void laugh() { 
		System.out.println(TAG +" laugh");
	}

	@Override
	public void cry() { 
		System.out.println(TAG +" cry");
	}

	@Override
	public void talk() { 
		System.out.println(TAG +" talk");
	} 
}

public abstract class AbstractBlackMan implements Human {
    public static String TAG = AbstractBlackMan.class.getSimpleName();
	@Override
	public void laugh() { 
		System.out.println(TAG +" laugh");
	}

	@Override
	public void cry() { 
		System.out.println(TAG +" cry");
	}

	@Override
	public void talk() { 
		System.out.println(TAG +" talk");
	} 
}


public class ImplMaleYellowMman extends AbstractYellowMan { 
	@Override
	public void sex() {
		 System.out.println("该黄种人性别是  **男**");

	} 
}

public class ImplMaleWhiteMman extends AbstractYellowMan { 
	@Override
	public void sex() {
		 System.out.println("该白种人性别是  **男**"); 
	} 
}

public class ImplMaleBlackMman extends AbstractYellowMan { 
	@Override
	public void sex() {
		 System.out.println("该黑种人性别是  **男**"); 
	} 
}

public class ImplFemaleYellowMman extends AbstractYellowMan { 
	@Override
	public void sex() {
		 System.out.println("该黄种人性别是  **女**"); 
	} 
}

public class ImplFemaleWhiteMman extends AbstractYellowMan { 
	@Override
	public void sex() {
		 System.out.println("该白种人性别是  **女**"); 
	} 
}

public class ImplFemaleBlackMman extends AbstractYellowMan { 
	@Override
	public void sex() {
		 System.out.println("该黑种人性别是  **女**"); 
	} 
}

public enum HumanEnum { 
     YellowMaleMan("设计模式之禅.ImplMaleYellowMman"),
     YellowFemaleMan("设计模式之禅.ImplFemaleYellowMman"),
     BlackMaleMan("设计模式之禅.ImplMaleBlackMman"),
     BlackFemaleMan("设计模式之禅.ImplFemaleBlackMman"),
     WhiteMaleMan("设计模式之禅.ImplMaleWhiteMman"),
     WhiteFemalMan("设计模式之禅.ImplFemaleWhiteMman"); 
     private String value = "";
	 private HumanEnum(String value){
		 this.value = value;
	 }
	 public String getValue(){
		 return this.value;
	 }
}

public interface HumanFactory {
     public Human createYellowMan();
     public Human createWhiteMan();
     public Human createBlackMan();
}

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) { 
				e.printStackTrace();
			}
    	  }
		return human;
      } 
}

public class MaleHumanFactory extends AbstractHumanFactory { 
	@Override
	public Human createYellowMan() { 
		return super.createHuman(HumanEnum.YellowMaleMan);
	} 
	@Override
	public Human createWhiteMan() {
		 return super.createHuman(HumanEnum.WhiteMaleMan);
	} 
	@Override
	public Human createBlackMan() { 
		return super.createHuman(HumanEnum.BlackMaleMan);
	} 
}

public class FemaleHumanFactory extends AbstractHumanFactory { 
	@Override
	public Human createYellowMan() { 
		return super.createHuman(HumanEnum.YellowFemaleMan);
	} 
	@Override
	public Human createWhiteMan() { 
		return super.createHuman(HumanEnum.WhiteFemalMan);
	} 
	@Override
	public Human createBlackMan() { 
		return super.createHuman(HumanEnum.BlackFemaleMan);
	} 
}

public class Client { 
	public static void main(String[] args) {
		 HumanFactory malefactory = new MaleHumanFactory();
		 HumanFactory femalFactory = new FemaleHumanFactory();
		 Human human = malefactory.createBlackMan();
		 human.cry();
		 human.sex();
		 human.talk();
		 human.laugh(); 
	} 
}


















0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:42239次
    • 积分:637
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:121篇
    • 译文:0篇
    • 评论:0条