『学習ノート』のJAVA設計モデル--単純工場モデル


          《        》      ,        :
               ,           。                    ,       

に の を っています.
1.  (Creator)           ,                。            ,     

オブジェクト.
2.  (Product)                   ,                 。
3.    (Concrete Product)                  。


     (Apple),  (Banana)  Class,       get()  。
public class Apple{
    /*
     *   
     */
     public void get(){
        System.out.println("    ");
    }
}

public class Banana{
    /*
     *   
     */
    public void get(){
        System.out.println("    ");
    }
}

       MainClass ,      Apple,Banana         get()  。
public class MainClass {
    public static void main(String[] args){
 
        //     Apple
        Apple apple = newApple();
        //     Banana
        Banana banana = newBanana();
        apple.get();
        banana.get();
    }
}

    Apple Banana     ,     get()  ,                      ,  ,  

インタフェースFruitを します.
public interface Fruit {
    /*
     *   
     */
    public void get();
}
 Apple Banana    Fruit  。
public class Apple implements Fruit{
    /*
     *   
     */
     public void get(){
        System.out.println("    ");
    }
}

public class Banana implements Fruit{
    /*
     *   
     */
     public void get(){
        System.out.println("    ");
    }
}

          , MainClass             。

  
  
  
  
public class MainClass {
    public static void main(String[] args){
 
      //     Apple,     
        Fruit apple = newApple();
        Fruit banana = newBanana();
        apple.get();
        banana.get();
    }
}

                     Apple Banana     ,            FruitFactory。

  
  
  
  
public class FruitFactory {
    /*
     *   Apple    
     */
    public static  Fruit getApple(){
        returnnewApple();
    }
     
    /*
     *   Banana   
     */
    public static Fruit getBanana(){
        returnnewBanana();
    }
}

 FruitFactory getXXX()      static  ,         FruitFactory      ,         

static.
 
  FruitFactory  Apple Banana    。

  
  
  
  
public class MainClass {
    public static void main(String[] args){
 
     //     Apple
        Fruit apple = FruitFactory.getApple();
        Fruit banana = FruitFactory.getBanana();
        apple.get();
        banana.get();
    }
}

      ,                getXXX()  ,                  FruitFactory 

しており、 の も ではなく、「 」の に していないため、 しています.

  
  
  
  
public class FruitFactory {
    /*
     * getFruit  ,        
     */
     public static Fruit getFruit(String type) throws InstantiationException, IllegalAccessException{
        if(type.equalsIgnoreCase("apple")) {
            returnApple.class.newInstance();
        } elseif(type.equalsIgnoreCase("banana")) {
            returnBanana.class.newInstance();
        } else{
            System.out.println("          ");
            returnnull;
        }
    }
}

MainClass  FruitFactory  Apple Banana    。
public class MainClass {
    public static void main(String[] args){
        Fruit apple = FruitFactory.getFruit("apple");
        Fruit banana = FruitFactory.getFruit("banana");
        apple.get();
        banana.get();
    }
}
      ,             。          ,           ,           
ボリュームクラスのオブジェクト.ユーザーは、これらのオブジェクトがどのように され、どのように されているかを することなく、 クラスに づいて なインスタンスを できます.ソフトウェアアーキテクチャ の に ちます. なファクトリモデルの もそのファクトリクラスに れており、ファクトリクラスはすべてのインスタンスの ロジックを しているため、「 」の ではよくないことがわかります.また,システム の な が するにつれて, にも の が される があり, はあまりよくない.たとえばPear( )クラスを する 、Fruit FactoryはgetFruit()メソッドにtype==「Pear」の を する があります.この 、 に しません.
FruitFactory                 ,    。
public class FruitFactory{
    /*
     * getFruit  ,        
     */
     public static Fruit getFruit(String type)throws ClassNotFoundException, InstantiationException,IllegalAccessException {
        Class fruit = Class.forName(type);
        return(Fruit) fruit.newInstance();
    }
}

      
      
      
      
MainClass  FruitFactory  Apple Banana    ,  getFruit()               ,     
ClassNotFoundException  ,    ,              。

   
   
   
   
public class MainClass {
    public static void main(String[] args){
 
     Fruit apple = FruitFactory.getFruit("Apple");
        Fruit banana = FruitFactory.getFruit("Banana");
        apple.get();
        banana.get();
    }
}