建站系统做网站,类似58同城分类信息网站开发,广州网站设计报价,网站开发中系统部署与维护要求一、模板方法
模板方法(Template Method)设计模式是一种行为设计模式#xff0c; 它在父类中定义了一个功能的框架#xff0c; 允许子类在不修改结构的情况下重写功能的特定步骤。也就是模板方法定义了一组有序执行的操作#xff0c;将一些步骤的实现留给子类#xff0c;同…一、模板方法
模板方法(Template Method)设计模式是一种行为设计模式 它在父类中定义了一个功能的框架 允许子类在不修改结构的情况下重写功能的特定步骤。也就是模板方法定义了一组有序执行的操作将一些步骤的实现留给子类同时保持整体功能结构。该技术通常也用于为主要操作提供预处理和后处理的钩子(hook)。
UVM库用了很多模板方法比如uvm_sequence里的pre_body()和post_body()就是body()方法的钩子在分别允许用户在body()执行之前和之后做一些其它处理。
模板方法设计模式主要包括以下几个组件
抽象类 AbstractClass会声明完成一个功能所需各个步骤的方法 以及依次调用它们实际步骤。 功能步骤可以被声明为抽象类型 也可以提供一些默认实现。另外也可以提供一些放在主要功能步骤之前或之后的可选步骤方法(钩子)这些方法为子类提供额外的功能扩展点。具体类 ConcreteClass可以重写所有步骤实现 但不能重写模板方法自身执行各个步骤方法的顺序。
我们以UVM中的monitor来举个模板方法应用的例子利用模板方法可以扩展monitor主要功能而且不容易误破坏monitor主功能。在base monitor组件中定义了非virtual的collect_transactions()模板方法并提供了空的pre_collect()和post_ collect ()钩子方法。在继承的子monitor中通过实现pre_ collect ()和post_ collect ()的具体内容来提供了特定项目需求的操作。然后使用UVM factory方法将子monitor的对象去替换base monitor的对象。
下图为模板方法设计模式在monitor中应用的UML类图。 二、参考代码
monitor的模板方法设计模式参考代码如下
class base_monitor extends uvm_monitor;uvm_component_utils (base_monitor)function new(string name base_monitor , uvm_component parentnull);super.new(name, parent);endfunction : newtask collect_transactions();pre_collect();collect();post_collect();endtask : collect_transactionsvirtual task pre_collect();uvm_info(PRE_COLLECT, EMPTY method, UVM_LOW)endtask : pre_collecttask collect();uvm_info(COLLECT, collect begin, UVM_LOW)uvm_info(COLLECT, collect end, UVM_LOW)endtask : collectvirtual task post_collect();uvm_info(POST_COLLECT, EMPTY method, UVM_LOW)endtask : post_collectendclass : base_monitor
模板方法设计模式-具有空钩子的base monitor class son_monitor extends base_monitor;uvm_component_utils (son_monitor)function new(string name son_monitor , uvm_component parentnull);super.new(name, parent);endfunction : newvirtual task pre_collect();uvm_info(PRE_COLLECT, PRE: collect item, UVM_LOW)endtask : pre_collectvirtual task post_collect();uvm_info(POST_COLLECT, POST: collect item, UVM_LOW)endtask : post_collectendclass : son_monitor
模板方法设计模式-带有实现钩子的son monitor 模拟测试代码如下
// Use UVM factory overrde in the uvm_env
set_type_override_by_type(base_monitor::get_type(), son_monitor::get_type(), b0); 输出仿真日志如下 | # UVM_INFO 0.000ns: uvm_test_top.env.agent.mon_h [PRE_COLLECT] EMPTY method| # UVM_INFO 0.000ns: uvm_test_top.env.agent.mon_h [COLLECT] collect begin| # UVM_INFO 0.000ns: uvm_test_top.env.agent.mon_h [COLLECT] collect end| # UVM_INFO 0.000ns: uvm_test_top.env.agent.mon_h [POST_COLLECT] EMPTY method
模板方法设计模式-带有空钩子的base monitor的输出结果 | # UVM_INFO 0.000ns: uvm_test_top.env.agent.mon_h [PRE_COLLECT] PRE: collect item| # UVM_INFO 0.000ns: uvm_test_top.env.agent.mon_h [COLLECT] collect begin| # UVM_INFO 0.000ns: uvm_test_top.env.agent.mon_h [COLLECT] collect end| # UVM_INFO 0.000ns: uvm_test_top.env.agent.mon_h [POST_COLLECT] POST: collect item
模板方法设计模式-带有实现钩子的子monitor的输出结果 输出仿真文件显示了模板方法模式的效果。在前者中消息由具有空pre_collect()和post_collect()钩子的base monitor生成。在后一种情况下将使用子monitor的实例并使用已实现的钩子这些钩子可用于特定于项目的处理且无需修改base monitor的代码。子monitor主要的collect()方法继承自base monitor因此两种情况下保持一致。模板方法collect_transactions()确保钩子会在主函数collect()之前和之后合适的地方被调用了。
模板方法的通常使用方式就是定义一个基本的抽象类并且指定哪些抽象方法需要再子类中实现。模板方法的主要缺点就是如果父类和子类都实现了复杂的功能调试起来将非常麻烦。