story · 2020年05月19日

SystemVerilog​概念浅析之Factory Pattern

如果粗略的了解过UVM,很可能一开始会被UVM方法学的一些表述弄得雨里雾里,比如Factory机制

在软件工程中,**设计模式(design pattern)**是指对常见问题的通用解决方案。

Factory Pattern旨在解决对象创建的问题。

举一个例子,假设你要创建多种类型的玩具(例如toy tank,toy bus),需要先定义一个通用的基类,其拥有通用的**方法(method )数据(data)**接口。

class TOY;
// Common data memeber
string type;

// Common methods
 virtual function string get_type();
endclass : TOY

然后对该基类进行扩展。

class TOY_Tank extends TOY;
function new();
   this.string = "Toy Tank";
endfunction : new

string function string get_type();
   return this.string;
endfunction : get_type
endclass : TOY_Tank class TOY_Bus extends TOY;
function new();
   this.string = "Toy Bus";
endfunction : new

string function string get_type();
   return this.string;
endfunction : get_type
endclass : TOY_Bus

到这里,我们完成了相关对象的创建工作。

下一个需要解决的问题是编写toy factory 类。为简单起见,考虑传递“1”以获得toy  tank 的实例,传递“2”来从factory 获取toy  bus 实例。

class TOY_factory;
Toy my_toy
// Common methods
function toy get_toy(int type);
  if(type == 1) this.my_toy = newTOY_Tank();
  if(type == 2) this.my_toy = newTOY_Bus();
  return this.my_toy;
endfunction : get_toy

endclass : TOY_factory



本文转载自公众号:芯片数字实验室
原文链接:
https://mp.weixin.qq.com/s/DCQNVa3sxMSeToBCQrqnzQ
未经作者同意,请勿转载!

推荐阅读

想了解更多内容,欢迎关注芯片数字实验室专栏
推荐阅读
关注数
12276
内容数
201
前瞻性的眼光,和持之以恒的学习~
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息