02. Factory Method模式代码

    2. Factory Method 本质:延迟到子类来选择实现
      2.1 EXPORT OPERATE FACTORY
        public interface ExportFileApi {
            public boolean export(String data);
        }
        public class ExportDb implements ExportFileApi{
            public boolean export(String data) {
                System.out.println("Export data '" + data + "' to DB.");
                return true;
            }
        }
        public class ExportTxtFile implements ExportFileApi{
            public boolean export(String data) {
                System.out.println("Export data '" + data + "' to a TXT file.");
                return true;
            }
        }
        public abstract class ExportOperateFactory {
            public boolean export(String data) {
                ExportFileApi api = factoryMethod();
                return api.export(data);
            }
            protected abstract ExportFileApi factoryMethod();
        }
        public class ExportDbOperateFactory extends ExportOperateFactory {
            protected ExportFileApi factoryMethod() {
                return new ExportDb();
            }
        }
        public class ExportTxtFileOperateFactory extends ExportOperateFactory {
            protected ExportFileApi factoryMethod() {
                return new ExportTxtFile();
            }
        }
        public class FactoryMethodClient {
            public static void main(String[] args) {
                ExportOperateFactory operateFactory = new ExportDbOperateFactory();
                operateFactory.export("data for testing");
            }
        }
      2.2 HUMAN FACTORY
        public interface Human {
            //每个人种都有相应的颜色
            public void getColor();
            //人类会说话
            public void talk();
        }
        public class WhiteHuman implements Human {
            //白色人种的颜色是白色的
            public void getColor(){
                System.out.println("白色人种的皮肤颜色是白色的!");
            }
            //白色人种讲话
            public void talk() {
                System.out.println("白色人种会说话,一般都是但是单字节。");
            }
        }
        public class BlackHuman implements Human {
            public void getColor(){
                System.out.println("黑色人种的皮肤颜色是黑色的!");
            }
            public void talk() {
                System.out.println("黑人会说话,一般人听不懂。");
            }
        }
        public class YellowHuman implements Human {
            public void getColor(){
                System.out.println("黄色人种的皮肤颜色是黄色的!");
            }
            public void talk() {
                System.out.println("黄色人种会说话,一般说的都是双字节。");
            }
        }
        public abstract class AbstractHumanFactory {
            public abstract <T extends Human> T createHuman(Class<T> c);
        }
        public class HumanFactory extends AbstractHumanFactory {
            public <T extends Human> T createHuman(Class<T> c){
                //定义一个生产的人种
                Human human=null;
                try {
                    //产生一个人种
                    human = (Human)Class.forName(c.getName()).newInstance();
                } catch (Exception e) {
                    System.out.println("人种生成错误!");
                }
                return (T)human;
            }
        }
        public class NvWa {
            public static void main(String[] args) {
                //声明阴阳八卦炉
                AbstractHumanFactory YinYangLu = new HumanFactory();
                //女娲第一次造人
                System.out.println("--造出的第三批人是白色人种--");
                Human whiteHuman = YinYangLu.createHuman(WhiteHuman.class);
                whiteHuman.getColor();
                whiteHuman.talk();
                //女娲第二次造人
                System.out.println("\n--造出的第三批人是黑色人种--");
                Human blackHuman = YinYangLu.createHuman(BlackHuman.class);
                blackHuman.getColor();
                blackHuman.talk();
                //第三次造人
                System.out.println("\n--造出的第三批人是黄色人种--");
                Human yellowHuman = YinYangLu.createHuman(YellowHuman.class);
                yellowHuman.getColor();
                yellowHuman.talk();
            }
        }


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章