The Ultimate Hitchhiker's Guide to Verification

Dumping ground of useful links/articles/tips/tricks on System Verilog/VMM/OVM as and when I stumble upon them and some of my views on it :)

What is Factory Pattern

If you are working in System Verilog/VMM environment, it is high likely that you will be bombarded with word/expression from OOP world like "Factory Pattern", "Facade Pattern", "Observer Pattern" and so on. There is a finite possibility that you will seat down and start thinking what does those word mean and why they were used too frequently. Let me try to explain the idea/concept behind such word in the simplest way possible.

In SW engineering, a design pattern (or simply pattern) means a general repeatable solution to a commonly occurring problem. Most of these patterns have similarity to something or other in human society and hence they are known by that name.

Factory pattern as the name suggest, is aimed at solving the issue of creation of object. (Factory pattern is not the only pattern to deal with creation of objects, there are a bunch of more patterns for handling different kind of cases, and collectively they are known a creational patterns)

Let me give an example of case where we might need to use creational pattern and how to do so it in SV. Suppose you want to create a "Toy Factory" class which needs to create multiple types of toys (say toy aeroplane, toy tank, toy bus) depending upon the string input to it.

To create these different types of toys we need to have class defined for them. And there will be common method and data interface for these classes, hence it make sense to put all the common data member/task/functions in a class called toy class and then extend it.

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

Now we are done with the bothering about the objects to be created. The next problem that we need to solve is to write the toy factory class itself. For simplicity, let's consider the case where we will want to pass 1 to get an instance of tank class and 2 for getting an instance of bus class from the factory. Now the factory class will look like this.
class TOY_factory;
Toy my_toy

// Common methods
function toy get_toy(int type);
if(type == 1) this.my_toy = new TOY_Tank();
if(type == 2) this.my_toy = new TOY_Bus();
return this.my_toy;
endfunction : get_toy
endclass : TOY_factory

Note that we are using virtual function for bringing polymorphism in action and save us from having an individual instance of the toy type in the factory class.

Reference

2 comments:

Hamed July 29, 2013 at 8:26 AM  

should't

this.string be this.type instead?

Unknown May 22, 2014 at 4:15 PM  

this post starts off really good to describe the Factory, then it falls short and describes inheritance without really concluding what the purpose of a factory is. In short, a factory is a methodology that is used to return an object. Instead of using new() directly, the factory is usually the method used to create and get a handle to the object. If new() is used to construct the object, then the specifics of the constructor is used and is hardcoded throughout the code. With the factory, it is basically a lookup table - in systemverilog/UVM, the type_id::create() factory method is used to create the object. If a class or subclass is "registered" with the factory, then create() indirectly calls its constructor. If you register and use a different sub-class in place later, the call to type_id::create() will call a different constructor. This allows for "overriding" component and data-type classes via the factory. Anyway, this is basically more detail. Would be nice to see some examples in the post to really show the idea of a factory.

Post a Comment

About Me

My photo
I am from Sambalpur, Orissa, India. Have done Btech and Mtech in ECE from IIT Kharagpur. Currently working as Lead Member Technical Staff at Mentor Graphics Noida

My Feedburner

Followers

Search This Blog

My Shelfari Bookshelf

Shelfari: Book reviews on your book blog

 

Traffic Summary