In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要介绍"singleton class是什么",在日常操作中,相信很多人在singleton class是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"singleton class是什么"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
SystemVerilog中Singleton class是指只有一个对象(object)的类。这个对象只创建一次,为所需的全局变量提供一个namespaces。
Singleton class定义全局的行为,例如printing。
Singleton class 在compile-time创建一个对象,然后在run-time操作其中的成员,不需要用户在仿真时在建立一个新的实例。
program top;class singleton; int unsigned var1;
// Need to declare it as static, as it is accessed by static method 'create' static singleton single;
// Declared as 'protected', so user can't directly create instance of this class protected functionnew(); endfunction:new
static functionsingleton create(); if (single ==null) begin $display("Object single is null, so creating new object"); single =new(); end return single; endfunction:create endclass:singleton singleton s1, s2; initial begin s1 = singleton ::create(); $display (" 1 : s1.var1 = %0d", s1.var1); s1.var1 =10; $display (" 2 : s1.var1 = %0d", s1.var1);
s2 = singleton ::create(); $display (" 3 : s2.var1 = %0d", s2.var1);
s2.var1 =20; $display (" 4 : s2.var1 = %0d", s2.var1); $display (" 5 : s1.var1 = %0d", s1.var1); end endprogram
Output:
// 1 : s1.var1 = 0 // 2 : s1.var1 = 10 // 3 : s2.var1 = 10 // 4 : s2.var1 = 20 // 5 : s1.var1 = 20
A singleton object is a globally accessiblestatic object providing customizable service methods.
Synopsys UVM1.2 Workshop
在下面的例子中:
class service_class; protected static service_class me =get(); static function service_class get(); if(me = null) me = new() ; return me ; endfunctionextern virtual function void error (string msg) ;endclass
error method需要用户自行定义所需要的行为。
在UVM的workshop中使用了一个名称为proxy_class的singleton class很好地诠释了其在factory机制的应用。
class proxy_class#(type T =base) ; type proxy_class#(T) this_type ; //just for coding convenience protected function new();endfunction
static function this_type get(); if(me== null) me = new() ; return me;endfunctionstatic function T create(); create=new() ;functionendcase
class driver xtends base; typedef proxy_class(driver) proxy ;endclass
class monitor xtends base; typedef proxy_class(monitor) proxy ;endclass
class environment ; driver drv ; monitor mon; function new; drv = driver::proxy::create() ; mon = monitor::proxy::create() ; endfunctionendclass
这个proxy class提供了一个创建不同的singleton bject的机制。
为了使proxy class更有意义,我们需要一个virtual base class和factory机制。proxy class根据不同的factory注册和base class生成特定的proxy object。
到此,关于"singleton class是什么"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.