package factory method pattern; public interface Human { public void laugh(); public void cry(); public void talk(); } //**************************************************************************** package factory method pattern; public class WhiteHuman implements Human { public void laugh() { System.out.println("The caucasian laughed..."); } public void cry() { System.out.println("Caucasians cried..."); } public void talk() { System.out.println("The white people are talking..."); } } //**************************************************************************** package factory method pattern; public class BlackHuman implements Human { public void laugh() { System.out.println("Black people laughed..."); } public void cry() { System.out.println("Black people are crying..."); } public void talk() { System.out.println("Black people are talking..."); } } //**************************************************************************** package factory method pattern; public class YellowHuman implements Human { public void laugh() { System.out.println("The yellow race laughed..."); } public void cry() { System.out.println("The yellow race cried..."); } public void talk() { System.out.println("The yellow people are talking..."); } } //**************************************************************************** package factory method pattern; public class HumanFactory { public static Human createHuman(Class c) throws InstantiationException, IllegalAccessException, ClassNotFoundException{ Human human = null; human = (Human)Class.forName(c.getName()).newInstance(); return human; } } //**************************************************************************** package factory method pattern; public class NvWa { public static void main(String aegs[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException{ System.out.println("...The first batch of people created is like this: white people..."); Human whiteHuman = HumanFactory.createHuman(WhiteHuman.class); whiteHuman.cry(); whiteHuman.laugh(); whiteHuman.talk(); System.out.println("... The second batch of people created is like this: black..."); Human BlackHuman = HumanFactory.createHuman(BlackHuman.class); BlackHuman.cry(); BlackHuman.laugh(); BlackHuman.talk(); System.out.println("...The third batch of people created is like this: yellow race..."); Human YellowHuman = HumanFactory.createHuman(YellowHuman.class); YellowHuman.cry(); YellowHuman.laugh(); YellowHuman.talk(); } }
Factory Method Pattern A
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=327025872&siteId=291194637
Ranking