Improve Your SystemVerilog OOP Skills

Similar documents
Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Using Design Patterns in Java Application Development

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Design Patterns. Gunnar Gotshalks A4-1

SDC Design patterns GoF

Summary of the course lectures

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Topics in Object-Oriented Design Patterns

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

The Design Patterns Matrix From Analysis to Implementation

What is Design Patterns?

Design patterns. Jef De Smedt Beta VZW

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Design Patterns. An introduction

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

3 Product Management Anti-Patterns by Thomas Schranz

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

A Reconnaissance on Design Patterns

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

An Introduction to Patterns

Tuesday, October 4. Announcements

Introduction to Software Engineering: Object Design I Reuse & Patterns

Object-Oriented Design

CPSC 310 Software Engineering. Lecture 11. Design Patterns

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

DESIGN PATTERN - INTERVIEW QUESTIONS

Application Architectures, Design Patterns

Object Oriented Paradigm

1 Software Architecture

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

Applying the Observer Design Pattern

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

What is Design Patterns?

CSE 70 Final Exam Fall 2009

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Object-Oriented Oriented Programming

Information systems modelling UML and service description languages

Design patterns. OOD Lecture 6

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

CS/CE 2336 Computer Science II

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

Agile Architecture. The Why, the What and the How

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Software Development Project. Kazi Masudul Alam

Applying Design Patterns to SCA Implementations

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

Applying the Decorator Design Pattern

Software Design COSC 4353/6353 D R. R A J S I N G H

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

Writing your own Java I/O Decorator p. 102 Tools for your Design Toolbox p. 105 Exercise Solutions p. 106 The Factory Pattern Baking with OO

A Primer on Design Patterns

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

Nat. Thomas Software Engineering Telephonics Inc. Long Island IEEE Lecture Series

Inheritance. EEC 521: Software Engineering. Dealing with Change. Polymorphism. Software Design. Changing requirements Code needs to be flexible

What is Design Patterns?

Design Patterns. (and anti-patterns)

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

6.3 Patterns. Definition: Design Patterns

Object-oriented Software Design Patterns

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Design Pattern and Software Architecture: IV. Design Pattern

Applying the Factory Method Design Pattern

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Lecture 4: Observer Pattern, Event Library and Componentization

SYLLABUS CHAPTER - 1 [SOFTWARE REUSE SUCCESS FACTORS] Reuse Driven Software Engineering is a Business

Object-Oriented Design

Design Patterns: Structural and Behavioural

Introduction to Object-Oriented Programming

Patterns of learning

SOFTWARE ENGINEERING SOFTWARE DESIGN. Saulius Ragaišis.

CS251 Software Engineering Lectures 18: Intro to DP

Design Patterns. Hausi A. Müller University of Victoria. Software Architecture Course Spring 2000

Introduction and History

Design Patterns 2. Page 1. Software Requirements and Design CITS 4401 Lecture 10. Proxy Pattern: Motivation. Proxy Pattern.

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

Lecture 13: Design Patterns

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

Development and Implementation of Workshop Management System Application to Explore Combing Multiple Design Patterns

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

An Introduction to Patterns

Exam in TDDB84: Design Patterns,

Brief Note on Design Pattern

Design Patterns Reid Holmes

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

TDDB84: Lecture 09. SOLID, Language design, Summary. fredag 11 oktober 13

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

SOLID DESIGN PATTERNS FOR MERE MORTALS

Designing and Writing a Program. Divide and Conquer! The Design-Code-Debug Cycle. Documentation is Code. Pair Programming 3/8/2012

Transcription:

1 Improve Your SystemVerilog OOP Skills By Learning Principles and Patterns Jason Sprott Verilab Email: jason.sprott@verilab.com www.verilab.com

Experts in SV Learn Design Patterns 2 Look, you don t need to know how things work, only how to use them. But I don t want to be stuck just writing tests forever Bob Big Dog Coder Smart Engineer

Agenda 3 The OO Basics Are Not Enough OO Principles Abstract Factory Pattern example Strategy Pattern example Composite Pattern example

The OO Basics Are Not Enough 4 OO Basics Abstraction Encapsulation Polymorphism Inheritance Essential to learning, but... Doesn t tell us how to build good OO programs We need to know more

Guiding Principles of OOP 5 OO Principles Classes should be open for extension but closed for modification Subclasses should be substitutable for their base classes Depend on abstractions. Do not depend on concrete classes Encapsulate what varies Favour composition over inheritance Loosely coupled designs between interacting objects... Guide us in how to use our OO basics Aim to be principles of good programming Apply to SystemVerilog testbench design

Guiding Principles of OOP 6 OO Principles Classes should be open for extension but closed for modification Subclasses should be substitutable for their base classes Depend on abstractions. Do not depend on concrete classes Encapsulate what varies Favour composition over inheritance Loosely coupled designs between interacting objects... AKA: Open-closed Principle (OCP) Maybe the most important of all Change what modules do, without modifying them

Guiding Principles of OOP 7 OO Principles Classes should be open for extension but closed for modification Subclasses should be substitutable for their base classes Depend on abstractions. Do not depend on concrete classes Encapsulate what varies Favour composition over inheritance Loosely coupled designs between interacting objects... AKA: Liskov s Substitution Principle (LSP) Contracts must be honoured Surprisingly often violated Violations can lie dormant for some time

Guiding Principles of OOP 8 OO Principles Classes should be open for extension but closed for modification Subclasses should be substitutable for their base classes Depend on abstractions. Do not depend on concrete classes By not passing b to the Encapsulate what varies super class, existing code Favour composition might not work over the same inheritance anymore. Loosely coupled designs between interacting objects... class BaseClass; Channel a, b; function new(channel a, Channel b); this.a = a; this.b = b; end function virtual function Channel getcha()... virtual function Channel getchb()... endclass class SubClass extends BaseClass; function new(channel a, Channel b); super.new(a, null); end function endclass if(s.getchb == ChY)... // broken now

Guiding Principles of OOP 9 OO Principles Classes should be open for extension but closed for modification Subclasses should be substitutable for their base classes Depend on abstractions. Do not depend on concrete classes Encapsulate what varies Favour composition over inheritance Loosely coupled designs between interacting objects... AKA: Dependency Inversion Principle (DIP) Dependencies should target an abstract interface Concrete things change a lot, interfaces less so When you see new we are talking concrete

Guiding Principles of OOP 10 OO Principles Classes should be open for extension but closed for modification Subclasses should be substitutable for their base classes Depend on abstractions. Do not depend on concrete classes Encapsulate what varies Favour composition over inheritance Loosely coupled designs between interacting objects... Looks for behaviours that (may) change, e.g. different algorithms Encapsulate what changes in a class Aims to allow changes to be made without affecting dependent code

Design Patterns What Are They? 11 Each pattern describes a problems which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use the solution again a million times over, without ever doing it the same twice Christopher Alexander (Architect), A Pattern Language: Towns, Buildings, Construction, 1977

Design Patterns 12 Patterns tell us how to structure classes and objects to solve certain problems. We need to fit that to our application and programming language Embody OO Principles Show you how to write code with good OO design qualities Most patterns address change

Design Patterns And Types 13 Creational Structural Behavioural Singleton Factory Method Abstract Factory Builder Prototype Facade Adapter Bridge Decorator Composite Flyweight Proxy Has someone already got a solution to your OO problem? Iterator Command Strategy Chain of responsibility Mediator and more... (11 patterns)

Abstract Factory Pattern (Creational) 14 Problem: I want to instantiate different types of checkers, depending on the type of bus being used. I don t want my environment change when new bus types are added. Abstract Factory Provides and interface for creating families of related or dependent objects without specifying their concrete classes

Abstract Factory Pattern 15 Depends on all protocol checker objects, because it s creating them directly CheckerEnv AxiMstChk OcpMstChk AxiSlvChk OcpSlvChk Any changes to the concrete implementation of these could affect CheckerEnv Every new kind of checker added creates another dependency for CheckerEnv

Abstract Factory Pattern DIP 16 CheckerEnv CheckerEnv now depends only on an abstraction AxiMstChk Checker OcpMstChk Concrete classes now depend on the abstraction AxiSlvChk OcpSlvChk Dependency Inversion Principle Depend on abstractions not concrete classes

Abstract Factory Pattern 17 CheckerEnv Open-closed Principle <<uses>> <<uses>> createmstchk() Factory DIP MstChk do_check() <<extends>> <<extends>> ConcreteAxiFactory createmstchk() <<uses>> AxiMstChk do_check() Factory Method

Abstract Factory Pattern 18 CheckerEnv <<uses>> <<uses>> createmstchk() Factory MstChk do_check() <<extends>> <<extends>> ConcreteAxiFactory createmstchk() <<uses>> AxiMstChk do_check() ConcreteOcpFactory createmstchk() <<uses>> OcpMstChk do_check()

Abstract Factory Pattern 19 class ConcreteAxiFactory extends Factory;... function MstChk createmstchk(); AxiMstChk p; p = new(<axi specific args>); return(p);... endclass class ConcreteOcpFactory extends Factory;... function MstChk createmstchk(); OcpMstChk p; p = new(<ocp specific args>); return(p);... endclass The factory knows how to create its own concrete products. If the way to do this changes, the client is protected from that

Abstract Factory Pattern 20 class CheckerEnv; Factory checker_factory; function new(factory f); checker_factory = f; function start_checking _stuff(); MstChk my_chk; my_chk = checker_factory.createmstchk(); my_chk.do_check(); // do something endclass Whoever uses the CheckerEnv class... CheckerEnv my_env; ConcreteOcpFactory ocp_factory = new; my_env = new(ocp_factory); my_env.start_checking_stuff(); CheckerEnv uses abstract types it is generic code. Remember DIP When using CheckerEnv we just pass it the type of factory we want it to use We can create new factory types whenever we like

Strategy Pattern (Behavioural) 21 Problem: I have code that uses an algorithm that can change or new ones can be added. I want to allow the algorithm to change without breaking my code. Strategy Defines a family of algorithms, encapsulates each one and makes them interchangeable. Strategy lets the algorithm vary independently from the client using it.

Strategy Pattern 22 Client Client controls which algorithm used Encapsulate what varies Context ContextInterface() <<uses>> OCP Strategy AlgorithmInterface() <<extends>> ConcereteStrategyA AlgorithmInterface() ConcereteStrategyB AlgorithmInterface() New Algorithms easily added without affecting Context code ConcereteStrategyC AlgorithmInterface()

Strategy Pattern 23 Crypto Crypto controls which algorithm used Encapsulate what varies CryptoContext do_encryption() <<uses>> CryptoStrategy encrypt() <<extends>> CryptoAlgorithmA encrypt() CryptoAlgorithmB encrypt() New Algorithms easily added without affecting Context code

Strategy Pattern 24 virtual class CryptoStrategy; pure virtual function MyData encrypt(mydata d); endclass class CryptoAlgorithmA extends CryptoStrategy; virtual function MyData encrypt(mydata d); // some algorithm endclass class CryptoContext; CryptoStrategy my_algorithm; function new(cryptostrategy s); my_algorithm = s; function MyData do_encryption(mydata d); return (my_algorithm.encrypt(d)); endclass Abstract interface capturing behaviour that varies Concrete implementation of the algorithm. Generic code that can handle any algorithm. Only depends on the abstract interface In this case algorithm to use is passed on constructor Executes the algorithm

Strategy Pattern 25 virtual class CryptoStrategy; class pure CryptoAlgorithmA; virtual function MyData encrypt(mydata d); endclass class virtual CryptoContext; function MyData encrypt(mydata d); // some algorithm // CryptoStrategy my_algorithm; *** Client code *** new(cryptostrategy s); endclass // instance my_algorithm of crypto = s; container CryptoContext my_crypto; function MyData do_encryption(mydata d); // create return desired (my_algorithm.encrypt(d)); CryptoAlgorithmA cryptoa = new; endclass // pass in the algorithm to be // used as an argument (or similar) my_crypto = new(cryptoa); // Do the encryption foo = my_crypto.do_encryption(data); Instance of the algorithm we want to use Program the generic context code to use it Call to do encryption just does the right thing

Composite Pattern (Structural) 26 Problem: I want to build a tree structure, where objects can be leafs or other nodes to further hierarchy. Composite Allows you to compose objects into tree structures to represent part-whole hierarchies. Composite lets client treat individual objects or compositions of objects uniformly

Composite Pattern 27 Client Component add() remove() getchild() operation() Tree Structure Composite: contains other hierarchy <<extends>> operation() Leaf Leaf Composite list_of_components[ ] add() remove() getchild() operation()

Composite Pattern 28 virtual class BusComponent; virtual function void add(buscomponent c); // default behaviour(error?) virtual function void remove(buscomponent c); // default behaviour (error?) pure virtual function void enable(buscomponent c); pure virtual function void disable(buscomponent c); Interface for node or leaf. Some methods not applicable in each case. We could add default behaviour. Operations interface endclass

Composite Pattern 29 virtual class BusComponent; class virtual BusNode function extends void BusComponent; add(buscomponent c); protected // default BusComponent behaviour(error?) node_list[buscomponent]; virtual function void add(buscomponent c); virtual node_list[c] function void = c; remove(buscomponent // use ref as key c); // default behaviour (error?) virtual function void remove(buscomponent c);... node_list.delete(c); endclass virtual function void enable(); // iterate sub nodes and enable virtual function void disable(); // iterate sub nodes and disable... endclass Implementation to manage hierarchy for this node The user doesn t need to care if this is a node or leaf. The interface is the same

Composite Pattern 30 virtual class BusComponent; class virtual BusNode; function void add(buscomponent c); // default behaviour(error?) class virtual BusLeafNode function extends void add(buscomponent BusComponent; c); virtual virtual node_list[c] function function = void void c; // remove(buscomponent enable(); use ref as key c); virtual // // default enables function behaviour a leaf node void remove(buscomponent (error?) master or slave c); node_list.delete(c);... virtual function void disable(); endclass // disables a leaf node master or slave virtual function void enable();... // ignore for non leaf node? endclass Functionality for leaf nodes.

Composite Pattern 31 TOP BUS2 class BusControl; function void disable(buscomponent c); c.disable(); endclass... BusControl bus_ctrl = new; S1 S2 // start with instance TOP (BusNode) bus_ctrl.disable(top); // start with instance S5 (BusLeafNode) bus_ctrl.disable(s5); S3 S4 S5 BusControl can treat nodes and leafs the same

Summary 32 Learning Principles and Patterns improves your OOP skills Patterns give us a useful common language when talking about solutions Someone might already have solved the OO problem you are working on elegantly

Bibliography 33 Design Patterns: Elements of Reusable Object-Oriented Software By Gamma, Helm, Johnson, Vlissides Head First Design Patterns By Eric Freeman & Elizabeth Freeman Design Patterns Explained Second Edition By Alan Shalloway; James R. Trott Design Patterns For Dummies By Steve Holzner Article: Design Principles and Design Patterns, Robert C. Martin http://www.objectmentor.com/resources/articles/principles_and_patterns.pdf The Object-Oriented Thought Process, Third Edition By Matt Weisfeld SystemVerilog for Verification Second Edition By Chris Spear OVM Class Reference Version 2.0 Paper: Verification Patterns in Addition to RVM By Cavanagh, Sine Warner (Sun Microsystems), SNUG2008 San Jose

34 Improve Your SystemVerilog OOP Skills By Learning Principles and Patterns Jason Sprott Verilab Email: jason.sprott@verilab.com www.verilab.com