设计模式--抽象工厂方法模式
类图
源码
package skinMan; public class Client { public static void main(String[] agrs) throws Exception { HumanFactory maleHumanFactory = new MaleFactory(); HumanFactory femaleHumanFactory = new FemaleFactory(); Human maleYellowHuman = maleHumanFactory.createYellowHuman(); Human femaleYelloHuman = femaleHumanFactory.createYellowHuman(); maleYellowHuman.getColor(); maleYellowHuman.talk(); maleYellowHuman.getSex(); femaleYelloHuman.getColor(); femaleYelloHuman.talk(); femaleYelloHuman.getSex(); } } 2) Human.java package skinMan; public interface Human { public void getColor(); public void talk(); public void getSex(); } 3) HumanFactory.java package skinMan; public interface HumanFactory { public Human createYellowHuman(); public Human createBlackHuman(); public Human createWhiteHuman(); } 4) AbstractBlackHuman.java package skinMan; public abstract class AbstractBlackHuman implements Human { public void talk() { } public void getColor() { } } 5) AbstractWhiteHuman.java package skinMan; public abstract class AbstractWhiteHuman implements Human { public void talk() { System.out.println("the white people speak english!"); } public void getColor() { System.out.println("the white people skin is white!"); } } 6) AbstractYellowHuman.java package skinMan; public abstract class AbstractYellowHuman implements Human{ public void talk() { } public void getColor() { } } 7) MaleFactory.java package skinMan; public class MaleFactory implements HumanFactory{ public Human createYellowHuman() { return new MaleYellowHuman(); } public Human createBlackHuman() { return new MaleBlackHuman(); } public Human createWhiteHuman() { return new MaleWhiteHuman(); } } 8) MaleBlackHuman.java package skinMan; public class MaleBlackHuman implements Human { @Override public void getColor() { // TODO 自动生成的方法存根 System.out.println("黑色皮肤"); } @Override public void talk() { // TODO 自动生成的方法存根 } @Override public void getSex() { // TODO 自动生成的方法存根 System.out.println("男性"); } } 9) MaleWhiteHuman.java package skinMan; public class MaleWhiteHuman implements Human { @Override public void getColor() { // TODO 自动生成的方法存根 System.out.println("白色皮肤"); } @Override public void getSex() { // TODO 自动生成的方法存根 System.out.println("男性"); } @Override public void talk() { // TODO 自动生成的方法存根 } } 10) MaleYellowHuman.java package skinMan; public class MaleYellowHuman extends AbstractYellowHuman{ @Override public void getColor() { // TODO 自动生成的方法存根 System.out.println("黄色皮肤"); } @Override public void talk() { // TODO 自动生成的方法存根 } @Override public void getSex() { // TODO 自动生成的方法存根 System.out.println("男性"); } } 11) FemaleFactory.java package skinMan; public class FemaleFactory implements HumanFactory{ public Human createYellowHuman() { return new FemaleYellowHuman(); } public Human createBlackHuman() { return new FemaleBlackHuman(); } public Human createWhiteHuman() { return new FemaleWhiteHuman(); } } 12) FemaleBlackHuman.java package skinMan; public class FemaleBlackHuman implements Human { @Override public void getColor() { // TODO 自动生成的方法存根 System.out.println("黑色皮肤"); } @Override public void talk() { // TODO 自动生成的方法存根 } @Override public void getSex() { // TODO 自动生成的方法存根 System.out.println("女性"); } } 13) FemaleWhiteHuman.java package skinMan; public class FemaleWhiteHuman implements Human { @Override public void getColor() { // TODO 自动生成的方法存根 System.out.println("白色皮肤"); } @Override public void talk() { // TODO 自动生成的方法存根 } @Override public void getSex() { // TODO 自动生成的方法存根 System.out.println("女性"); } } 14) FemaleYellowHuman.java package skinMan; public class FemaleYellowHuman extends AbstractYellowHuman{ @Override public void getColor() { // TODO 自动生成的方法存根 System.out.println("黄色皮肤"); } @Override public void talk() { // TODO 自动生成的方法存根 } @Override public void getSex() { // TODO 自动生成的方法存根 System.out.println("女性"); } }
测试截图