zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然JAVA反射机制学习笔记:工厂设计模式

    package org.lxh.demo15.factorydemo01 ;
    interface Fruit{
        public void eat() ;    // 吃水果
    }
    class Apple implements Fruit{
        public void eat(){            // 覆写eat()方法
            System.out.println("** 吃苹果");
        }
    };
    class Orange implements Fruit{
        public void eat(){
            System.out.println("** 吃橘子") ;
        }
    };
    class Factory{
        public static Fruit getInstance(String className){
            Fruit fruit = null ;
            try{
                fruit = (Fruit)Class.forName(className).newInstance() ;
            }catch(Exception e){
                e.printStackTrace() ;
            }
            return fruit ;
        }
    };
    public class FactoryDemo01{
        public static void main(String args[]){
            Fruit f = Factory.getInstance("org.lxh.demo15.factorydemo01.Apple") ;
            if(f!=null){
                f.eat() ;
            }
        }
    };
    package org.lxh.demo15.factorydemo02 ;
    import java.util.Properties ;
    import java.io.File ;
    import java.io.FileOutputStream ;
    import java.io.FileInputStream ;
    interface Fruit{
        public void eat() ;    // 吃水果
    }
    class Apple implements Fruit{
        public void eat(){            // 覆写eat()方法
            System.out.println("** 吃苹果");
        }
    };
    class Orange implements Fruit{
        public void eat(){
            System.out.println("** 吃橘子") ;
        }
    };
    class Init{
        public static Properties getPro(){
            Properties pro = new Properties() ;
            File f = new File("d:\fruit.properties") ;    // 找到属性文件
            try{
                if(f.exists()){    // 文件存在
                    pro.load(new FileInputStream(f)) ;    // 读取属性
                }else{
                    pro.setProperty("apple","org.lxh.demo15.factorydemo02.Apple") ;
                    pro.setProperty("orange","org.lxh.demo15.factorydemo02.Orange") ;
                    pro.store(new FileOutputStream(f),"FRUIT CLASS") ;
                }
            }catch(Exception e){}
            return pro ;
        }
    };
    class Factory{
        public static Fruit getInstance(String className){
            Fruit fruit = null ;
            try{
                fruit = (Fruit)Class.forName(className).newInstance() ;
            }catch(Exception e){
                e.printStackTrace() ;
            }
            return fruit ;
        }
    };
    public class FactoryDemo02{
        public static void main(String args[]){
            Properties pro = Init.getPro() ;
            Fruit f = Factory.getInstance(pro.getProperty("apple")) ;
            if(f!=null){
                f.eat() ;
            }
        }
    };
  • 相关阅读:
    html iframe 滚动条
    Angular-Ant Desigin 开篇
    端口访问不了的原因
    swift 加载 本地html 和 网络路径
    xcode9.4 报错 error:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
    viewDidLoad, viewWillDisappear, viewWillAppear等区别及各自的加载顺序
    JavaScript设计模式之一Interface接口
    ES6原生Class
    react portals
    react-native-pushy 热更新
  • 原文地址:https://www.cnblogs.com/tszr/p/12416999.html
Copyright © 2011-2022 走看看