25.2.4 Factoryクラスのインプリメント

これでインプリメンテーションクラスを生成したので,valuetypeのFactoryをインプリメントします。

次に示すサンプルでは,生成されたPoint_initクラスにはIDLで宣言されたcreateメソッドがあります。このクラスはCORBA::ValueFactoryBase(C++)またはorg.omg.CORBA.portable.ValueFactory(Java)を継承します。コードサンプル25-3および25-4で示すようにPointDefaultFactoryクラスはPointValueFactoryをインプリメントします。

コードサンプル25-3 Factoryクラスのインプリメント(C++)

class PointFactory: public CORBA::ValueFactoryBase {
  public:
     PointFactory(){}
     virtual ~PointFactory(){}
     CORBA::ValueBase* create_for_unmarshal() {
        return new PointImpl();
     }
};

C++の場合,Point_initには,パブリックメソッドであるcreate_for_unmarshalがあり,これはMap_c.hhの純仮想メソッドとして出力されます。ユーザはPoint_initからクラスを派生させ,create_for_unmarshalメソッドをインプリメントしてFactoryクラスを生成する必要があります。IDLファイルをコンパイルする場合,IDLファイル用のスケルトンクラスを生成しません。

コードサンプル25-4 Factoryクラスのインプリメント(Java)

public class PointDefaultFactory implements
                                      PointValueFactory {
  public java.io.Serializable read_value (
                 org.omg.CORBA.portable.InputStream is) {
     java.io.Serializable val =
         new PointImpl(); // Called the implementation class
     // create and initialize value
     val = ((org.omg.CORBA_2_3.portable.InputStream)is).
                                         read_value(val);
     return val;
  }
  // It is up to the user to implement the valuetype
  // however they want:
  public Point create (int x,
        int y,
        java.lang.String z) {
     //IMPLEMENT:
     return null;
  }
}

新しいvaluetypeを作成するためにPointImpl()が呼び出されます。新しいvaluetypeは,read_valueによってInputStreamから読み込まれます。

注(Javaの場合)
ユーザはread_valueを呼び出す必要があります。これを呼び出さないと,ファクトリが動作しないで,ほかのどのメソッドも呼び出せません。