Springのファクトリモードと反射メカニズム

4299 ワード

反射メカニズムの役割
1、運転時にいずれかの対象が属するクラスを判断する.
2、実行時にクラスのオブジェクトを取得する.
3、実行時にjavaオブジェクトのプロパティ、メソッド、構築メソッドなどにアクセスします.
まず、なぜ反射メカニズムを使うのかを明らかにしなければなりません.オブジェクトを直接作成すればいいのではないでしょうか.これは動的と静的の概念に関連しています.
静的コンパイル:コンパイル時にタイプを決定し、オブジェクトをバインドします.
動的コンパイル:実行時にタイプを決定し、オブジェクトをバインドします.動的コンパイルはJavaの柔軟性を最大限に発揮し、多態の応用を体現し、クラス間のレンコンの整合性を低下させる.
反射と工場モード実現IOC
SpringにおけるIoCの実現原理は工場モードの反射機構である.まず、反射メカニズムを使用しない場合の工場モードを見てみましょう.
interface fruit{  
    public abstract void eat();  
}   
class Apple implements fruit{  
     public void eat(){  
         System.out.println("Apple");  
     }  
}   
class Orange implements fruit{  
     public void eat(){  
         System.out.println("Orange");  
     }  
}  
//       
//                                  
class Factory{  
     public static fruit getInstance(String fruitName){  
         fruit f=null;  
         if("Apple".equals(fruitName)){  
             f=new Apple();  
         }  
         if("Orange".equals(fruitName)){  
             f=new Orange();  
         }  
         return f;  
     }  
}  
class hello{  
     public static void main(String[] a){  
         fruit f=Factory.getInstance("Orange");  
         f.eat();  
     }  
}  

上の書き方の欠点は、サブクラスを追加するときに、工場クラスを修正する必要があることです.サブクラスを追加しすぎると、変更が多くなります.次に、反射メカニズムを使用して工場モードを実現します.
interface fruit{  
     public abstract void eat();  
}  
class Apple implements fruit{  
public void eat(){  
         System.out.println("Apple");  
     }  
}  
class Orange implements fruit{  
public void eat(){  
        System.out.println("Orange");  
    }  
}  
class Factory{  
    public static fruit getInstance(String ClassName){  
        fruit f=null;  
        try{  
            f=(fruit)Class.forName(ClassName).newInstance();  
        }catch (Exception e) {  
            e.printStackTrace();  
        }  
        return f;  
    }  
}  
class hello{  
    public static void main(String[] a){  
        fruit f=Factory.getInstance("Reflect.Apple");  
        if(f!=null){  
            f.eat();  
        }  
    }  
}  

これで、任意の複数のサブクラスを追加しても、ファクトリクラスは変更する必要はありません.反射メカニズムを用いて実現されるファクトリモードは、反射によってインタフェースのインスタンスを取得することができるが、完全なパケットとクラス名を入力する必要がある.また、ユーザーは、1つのインタフェースに使用可能なサブクラスがいくつあるかを知ることができません.そのため、プロパティファイルの形式で必要なサブクラスを構成します.
次に、反射メカニズムを使用し、プロパティ・ファイルと組み合わせたファクトリ・モード(IoC)を記述します.まずfruit.propertiesのリソース・ファイルを作成します.
apple=Reflect.Apple  
orange=Reflect.Orange  

次に、プライマリクラスコードを記述します.
interface fruit{  
    public abstract void eat();  
}  
class Apple implements fruit{  
    public void eat(){  
        System.out.println("Apple");  
    }  
}  
class Orange implements fruit{  
    public void eat(){  
        System.out.println("Orange");  
    }  
}  
//         
class init{  
    public static Properties getPro() throws FileNotFoundException, IOException{  
        Properties pro=new Properties();  
        File f=new File("fruit.properties");  
        if(f.exists()){  
            pro.load(new FileInputStream(f));  
        }else{  
            pro.setProperty("apple", "Reflect.Apple");  
            pro.setProperty("orange", "Reflect.Orange");  
            pro.store(new FileOutputStream(f), "FRUIT CLASS");  
        }  
        return pro;  
    }  
}  
class Factory{  
    public static fruit getInstance(String ClassName){  
        fruit f=null;  
        try{  
            f=(fruit)Class.forName(ClassName).newInstance();  
        }catch (Exception e) {  
            e.printStackTrace();  
        }  
        return f;  
    }  
}  
class hello{  
    public static void main(String[] a) throws FileNotFoundException, IOException{  
        Properties pro=init.getPro();  
        fruit f=Factory.getInstance(pro.getProperty("apple"));  
        if(f!=null){  
            f.eat();  
        }  
    }  
}  

私たちは
IOCコンテナの動作パターンを工場パターンの昇華と見なし、IOCコンテナを工場と見なすことができ、この工場で生産する対象はすべてプロファイルに定義され、プログラミング言語で提供される反射メカニズムを利用して、プロファイルに与えられたクラス名に基づいて対応するオブジェクトを生成する
.実現を見ると、IOCは以前工場メソッドに書かれていた死んだオブジェクト生成コードを、コンフィギュレーションファイルに変更して定義し、つまり工場とオブジェクト生成の両方を独立して区切って、柔軟性とメンテナンス性を高めることを目的としています.