(BASIC) Factory Example Not Working

Hi,

I’m trying to recreate a very basic example of Factory from Yin-Yang whitepaper to understand the concept, but getting unexpected results. Can anyone point to a mistake, please


virtual class Object;
  pure virtual function Object create();
endclass


/******************************/
class Factory;
  Object obj;
  virtual function Object create();
    create = obj.create();
  endfunction
endclass


/******************************/
class Transaction extends Object;

  virtual function void display_property();
    $display("Transaction");
  endfunction

  virtual function Object create();
    Transaction trn_h = new();
    return trn_h;
  endfunction
endclass


/******************************/
class Transaction1 extends Transaction;

  virtual function void display_property();
    $display("Transaction 1");
  endfunction

  virtual function Object create();
    Transaction trn_h = new();
    return trn_h;
  endfunction
endclass


/******************************/
class Transaction2 extends Transaction;


  virtual function void display_property();
    $display("Transaction 2");
  endfunction

  virtual function Object create();
    Transaction trn_h = new();
    return trn_h;
  endfunction
endclass


/******************************/
class Driver;
  task run(Factory fac_h);
    forever begin
      Transaction trn_h;
      $cast(trn_h,fac_h.create());
      trn_h.display_property();
      #10;
    end
  endtask
endclass


/******************************/
module tb();

  initial begin
    Driver drv_h = new();
    Factory fac_h = new();
    Transaction trn_h = new();
    Transaction1 trn1_h = new();
    Transaction2 trn2_h = new();

    fork
      drv_h.run(fac_h);
    join_none

    fac_h.obj = trn_h;
    #30;
    fac_h.obj = trn1_h;
    #30;
    fac_h.obj = trn2_h;
    #30;
    $finish;
  end


endmodule


I’m expecting to get something like:


Transaction
Transaction
Transacation 1
Transacation 1
Transacation 2
Transacation 2

but instead the output is :


Transaction
Transaction
Transaction 
...

What am I missing in the code?
Regards

In reply to Andrew_85:

The create() function of each Transaction class returns the base Transaction instead of the same Transaction type of the class.

You want:


/******************************/
class Transaction2 extends Transaction;
 
 
  virtual function void display_property();
    $display("Transaction 2");
  endfunction
 
  virtual function Object create();
    Transaction2 trn_h = new();   // Create appropriate type
    return trn_h;
  endfunction
endclass

In reply to cgales:

Thanks.
With this modification the code works as expected. I suppose there are copy-paste mistakes in the mentioned whitepaper.