factory

Abstract Factory Design Pattern

Indent

  • Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

  • A hierarchy that encasuplates: many possible “plateforms”, and the construction of a suite of “products”

  • The new operator considerd harmful


#include <iostream>

class Shape
{
  public:
    Shape() { _id = _total++; }
    virtual void draw()=0;
  protected:
    int _id;
    static int _total;
};

int Shape::_total = 0;

class Circle : public Shape { public: void draw() { std::cout << "Circle" << _id << std::endl; }};
class Square : public Shape { public: void draw() { std::cout << "Square" << _id << std::endl; }};
class Eclipse: public Shape { public: void draw() { std::cout << "Eclipse"<< _id << std::endl; }};

class Factory
{
  public:
    virtual Shape* createCurvedInstance()=0;
    virtual Shape* createStraightInstance()=0;
};

class SimpleShapeFactory : public Factory
{
  public:
    Shape* createCurvedInstance() { return new Circle(); }
    Shape* createStraightInstance() { return new Square(); }
};

class RobustShapeFactory : public Factory
{
  public:
    Shape* createCurvedInstance() { return new Eclipse(); }
    Shape* createStraightInstance() { return new Square(); }
};

int main() 
{
  Factory* factory = new SimpleShapeFactory();
  Shape* shapes[3];

  shapes[0] = factory->createCurvedInstance();
  shapes[1] = factory->createStraightInstance();
  shapes[2] = factory->createStraightInstance();

  for(int i = 0; i < 3; ++i) 
  {
    shapes[i]->draw();
  }
  return 0;
}

Factory Method

Intent

  • Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Meathods lets a class defer instantiation to subclasses.

  • Define a “virtual” constructor.

  • The new operator considered harmful

Discussion

Factorty Metod is to create objects as Template methods.


#include <iostream>
#include <cstring>


/* Abstratct base class declared by framework */
class Document
{
  public:
    Document(char const*fn)
    {
      strcpy(name, fn);
    }

    virtual void Open() = 0;
    virtual void Close() = 0;

    char * GetName() { return name; }
  private:
    char name[20];
};

/* Concrete derived class defined by client */
class MyDocument: public Document
{
  public:
    MyDocument(char const *fn): Document(fn) {}
    void Open() { std::cout << " MyDocument: Open()" << std::endl; }
    void Close() { std::cout << " MyDocument: Close()" << std::endl; }
};

class Application
{
  public:
    Application():_index(0) { std::cout << "Application: ctor" << std::endl; }
    void NewDocument(char const* name)
    {
      std::cout<< "Application: NewDocument()" << std::endl;
      /* Framework calls the 'hole' reserved for client custmization */
      _docs[_index] = CreateDocument(name);
      _docs[_index++]->Open();
    }

    void OpenDocument();
    void ReportDocs() { std::cout<< _index + 1 << std::endl; };

    /* Framework uses Document's base class */
    virtual Document *CreateDocument(char const*)=0;

  private:
    int _index;
    Document *_docs[10];  
};

class MyApplication: public Application
{
  public:
    MyApplication() { std::cout << "MyApplication: ctor" << std::endl; }
    Document *CreateDocument(char const* fn)
    {
      std::cout << " MyApplication: CreateDocument()" << std::endl;
      return new MyDocument(fn);
    }
};

int main()
{
  MyApplication myApp;
  myApp.NewDocument("foo");
  myApp.NewDocument("bar");
  myApp.ReportDocs();
  return 0;    
}

把这个代码写一遍就知道factory method 和abstract factory的区别和联系了

Factory机制是UVM(Universal Verification Methodology)中的重要机制,在整个全局仿真中存在且唯一,所有被注册的类才能使用该机制,使用它的第一步是将类注册到工厂[^1]。 使用factory机制注册的component类,可以通过类名(字符串)进行实例化,并自动调用其main_phase。其功能的实现是因为UVM内部定义了一个参数化的类来实现此功能,本质上是对SystemVerilog(sv)中new函数的重载。相比于简单的new函数,factory机制经过改良提供了更多的功能[^2]。 使用factory机制注册的component类,在接收外部set的变量时,如果要传递的变量使用`uvm_field宏注册后,可以在此component类中省略get语句。用`uvm_field宏注册后可以使用诸如copy、print、compare、pack、unpack等方法,无需自己定义实现。经注册的类在用creat实例化后,使用set_type_override_by_type进行重载时会用子类代替父类[^2]。 此外,factory机制还有调试功能,如使用print_oerride_info(原类型)可列出原始类型和新类型,使用print_topology可显示整颗uvm树的树形结构[^3]。 ### 代码示例 以下是一个简单的使用UVM factory机制的示例代码: ```systemverilog import uvm_pkg::*; `include "uvm_macros.svh" // 定义一个基类 class base_comp extends uvm_component; `uvm_component_utils(base_comp) function new(string name, uvm_component parent); super.new(name, parent); endfunction virtual function void main_phase(uvm_phase phase); `uvm_info("BASE_COMP", "In main_phase of base_comp", UVM_LOW) endfunction endclass // 定义一个派生类 class derived_comp extends base_comp; `uvm_component_utils(derived_comp) function new(string name, uvm_component parent); super.new(name, parent); endfunction virtual function void main_phase(uvm_phase phase); `uvm_info("DERIVED_COMP", "In main_phase of derived_comp", UVM_LOW) endfunction endclass module tb; initial begin // 注册类型重载 uvm_factory factory = uvm_factory::get(); factory.set_type_override_by_type(base_comp::get_type(), derived_comp::get_type()); // 通过factory机制创建实例 base_comp my_comp; my_comp = base_comp::type_id::create("my_comp", null); // 运行仿真 run_test(); end endmodule ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值