uvm_primer ch9 factory pattern factory pattern

uvm_primer ch9 factory pattern factory pattern

ch9 factory pattern

Programming tricks are called design patterns;
factory pattern is a design pattern
factory pattern is a static method

Factory pattern example

If you don’t understand the advantages of the factory pattern, you still have to change make_animalthe funciton to use the factory pattern ; if you want to construct a dog object

  • Supplement: If you want to create a new species, you really have to change the make_animal method of the factory, but this method can be called directly anywhere
  • For more details, you can see the relevant explanation of the factory model in "Large Talk Design Patterns"
class animal_factory;

 // 定义成static,则这个函数在任何地方可以直接访问
 //animal 为返回对象的类型
   static function animal make_animal(string species, 
                                      int age, string name); 
      chicken chicken;
      lion lion;
      case (species)
        "lion" : begin
           lion = new(age, name);
           return lion;  //构造了一个狮子 ,然后把它赋给animal 类型的变量中;
        end

        "chicken" : begin
           chicken = new(age, name);
           return chicken;
        end

        default : 
          $fatal (1, {
    
    "No such animal: ", species});
        
      endcase // case (species)
      
   endfunction : make_animal
   
endclass : animal_factor
....
      animal animal_h;
      
      animal_h = animal_factory::make_animal("lion", 15, "Mustafa")//使用工厂模式  

Forced type conversion

thorn_in_pawThis is not defined in the animal class, it animal_h.thorn_in_pawwill report an error if you use it directly
cast_ok = $cast(lion_h, animal_h);


cast_ok = $cast(lion_h, animal_h);
      if ( ! cast_ok) 
        $fatal(1, "Failed to cast animal_h to lion_h");
       //thorn_in_paw这个在animal类中没有定义,直接使用animal_h.thorn_in_paw 会报错
      if (lion_h.thorn_in_paw) $display("He looks angry!"); 
      animal_cage#(lion)::cage_animal(lion_h);

Guess you like

Origin blog.csdn.net/weixin_39060517/article/details/112755954