zoukankan      html  css  js  c++  java
  • 工厂设计模式 Factory

    Factory 主要用来实例化有共同接口的类,工厂模式可以动态决定应该实例化那一个类。

    例如:汽车销售商场

    该模式将创建对象的过程放在了一个静态方法中来实现.在实际编程中,如果需要大量的创建对象,该模式是比较理想的.

    public class Demo1 {
        public static void main(String[] args) {
            System.out.println("买宝马");
            Car bmw = CarFactory("BMW");
            bmw.run();
            System.out.println("买大奔");
            Car benz = CarFactory("Benz");
            benz.run();
        }
        public static Car CarFactory(String carName) {
            if ("BMW".equals(carName)) {
                return new BMW();
            } else if ("Benz".equals(carName)) {
                return new Benz();
            } else {
                throw new RuntimeException("车型有误");
            }
        }
    }
    abstract class Car {
        public abstract void run();
    }
    class BMW extends Car {
        @Override
        public void run() {
            System.out.println("BMW跑跑");
        }
    }
    class Benz extends Car {
        @Override
        public void run() {
            System.out.println("Benz跑跑");
        }
    }

    模拟spring工厂:

    person类:

    public class Person {
        private int id;
    
        String name;
    
        public Person(int id, String name) {
            this.id = id;
            this.name = name;
        }
    
        public Person() {
        }
    
        public void eat(int num) {
            System.out.println(name + "吃很" + num + "斤饭");
        }
    
        private static void sleep(int num) {
            System.out.println("明天睡上" + num + "小时");
        }
    
        public static void sum(int[] arr) {
            System.out.println("长度是:" + arr.length);
        }
    
        @Override
        public String toString() {
            return " 编号:" + this.id + " 姓名:" + this.name;
        }
    }

    info.txt

    com.it.reflect.Person
    id=110
    name=旺财
    /*
    工厂设计模式就是用于产生对象 的。
    */
    class Car{}
    class BMW extends Car{}
    class BSJ extends Car{}
    
    public class Demo1 {
        
        public static void main(String[] args) throws Exception {
            Person p = (Person) getInstance();
            System.out.println(p);
        }
        
        //需求: 编写一个工厂方法根据配置文件返回对应的对象。
        public static Object getInstance() throws Exception{
            //读取配置文件
            BufferedReader bufferedReader = new BufferedReader(new FileReader("info.txt"));
            //读取第一行 : 读取类文件的信息
            String className = bufferedReader.readLine();
            //通过完整类名获取对应 的Class对象
            Class clazz = Class.forName(className);
            //获取到对应的构造方法
            Constructor constructor = clazz.getDeclaredConstructor(null);
            constructor.setAccessible(true);
            Object o  = constructor.newInstance(null);
            //给对象设置对应的属性值
            String line = null;
            while((line = bufferedReader.readLine())!=null){
                String[] datas = line.split("=");
                Field field =clazz.getDeclaredField(datas[0]);
                //设置可以访问
                field.setAccessible(true);
                if(field.getType()==int.class){
                    field.set(o, Integer.parseInt(datas[1]));
                }else{
                    field.set(o, datas[1]);
                }
            }
            return o;
        }
    }
  • 相关阅读:
    [leetcode]43. Multiply Strings
    [leetcode]387. First Unique Character in a String
    Penetration Test
    Penetration Test
    Penetration Test
    Penetration Test
    Penetration Test
    Penetration Test
    Penetration Test
    Penetration Test
  • 原文地址:https://www.cnblogs.com/loveincode/p/5344349.html
Copyright © 2011-2022 走看看