SlideShare a Scribd company logo
1 of 77
Download to read offline
Compiler Case Study -
Design Patterns in C#
Ganesh Samarthyam
ganesh@codeops.tech
Design Patterns - Introduction
“Applying design principles is the key to creating
high-quality software!”
Architectural principles:
Axis, symmetry, rhythm, datum, hierarchy, transformation
How to Apply Principles in Practice?
Design principles
Code
How to bridge
the gap?
What are Smells?
“Smells'are'certain'structures'
in'the'code'that'suggest'
(some4mes'they'scream'for)'
the'possibility'of'refactoring.”''
What is Refactoring?
Refactoring (noun): a change
made to the internal structure of
software to make it easier to
understand and cheaper to
modify without changing its
observable behavior
Refactor (verb): to restructure
software by applying a series
of refactorings without
changing its observable
behavior
What are Design Patterns?
recurrent solutions
to common
design problems
Pattern Name
Problem
Solution
Consequences
Principles to Patterns
Expr expr = Addition.Make(
Multiplication.Make(
Constant.Make(10),
Constant.Make(20)),
Constant.Make(10));
Expr expr = new ExprBuilder().Const(10).Mult(20).Plus(30).Build();
Encapsulate object creation
Builder pattern
Why Care About Patterns?
❖ Patterns capture expert
knowledge in the form of
proven reusable solutions
❖ Better to reuse proven
solutions than to “re-invent”
the wheel
❖ When used correctly, patterns
positively influence software
quality
❖ Creates maintainable,
extensible, and reusable code
Design Patterns - Catalog
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Design Patterns - Catalog
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Creational
Deals with controlled
object creation
Factory method, for
example
Structural
Deals with
composition of classes
or objects
Composite, for example
Behavioral
Deals with interaction
between objects/
classes and
distribution of
responsibility
Strategy, for example
3 Principles Behind Patterns
Design'principles'
behind'pa0erns'
Program'to'an'
interface,'not'to'an'
implementa7on''
Favor'object'
composi7on''
over'inheritance'
Encapsulate'what'
varies'
Compiler Case Study
Case Study Overview
❖ Smelly code segments (specially if-else based on types
and switch statements) used first
❖ They are “refactored” using design patterns
❖ The case study shows code-snippets; compilable, self-
contained source code files shared with you
What You Need to Know
❖ Understand essential object oriented constructs such as runtime
polymorphism (virtual functions), and principles (such as
encapsulation)
❖ Know essential C# language features - classes, inheritance, etc
❖ Data structures and algorithms (especially binary trees, tree
traversals, and stacks)
❖ NO background in compilers needed
❖ Extremely simplified compiler code example; purposefully
done to make best use of this case study
Simple Expression Tree Example
+
* 30
10 20
Expression: ((10 * 20) + 30)
Post-order Traversal: Simulating Code Gen
10 20 * 30 +
post-order
traversal
result
+
* 30
10 20
Front-end
Intermediate code (CIL, Java bytecodes, …)
Interpreter (Virtual
Machine)
Using Stack for Evaluation
Initial
empty
10
push 10
10
push 20
20
200
perform
10 * 20
push
200
200
push 30
30
230
perform
200 +
30
push
230
End
result
= 230
10 20 * 30 +
Mnemonic Names in Java and .NET
10 20 * 30 +
push 10
push 20
multiply
push 30
add
.NET CIL Instructions Java Bytecode Instructions
ldc.i4.s 0x0a
ldc.i4.s 0x14
mul
ldc.i4.s 0x1e
add
(ldc => load constant ;
i4 => integer of 32 bits;
s => signed)
bipush 10
bipush 20
imul
bipush 30
iadd
(bipush stands for “byte integer push”)
Hands-on Exercise
Use the ildasm tool to disassemble the Expr.cs code
// Expr.cs
using System;
class Test {
    public static void Main() {
        int a = 10, b = 20, c = 30;
        int r = ((a * b) + c);
        Console.WriteLine(r);
    }
}
((10	*	20)	+	30)Source Code
C#
Compiler
.NET CLR
(Common
Language
Runtime)
ldc.i4.s 0x0a
ldc.i4.s 0x14
mul
ldc.i4.s 0x1e
add
+
* 30
10 20
Facade Pattern
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a
higher-level interface that makes the subsystem easier to use.
Facade Pattern in Compilers
“The Compiler class acts as a facade: It offers clients a single,
simple interface to the compiler subsystem.”
Facade Pattern in Compilers
public virtual void Compile(StreamReader input, BytecodeStream output) {
Scanner scanner = new Scanner(input);
var builder = new ProgramNodeBuilder();
Parser parser = new Parser();
parser.Parse(scanner, builder);
RISCCodeGenerator generator = new RISCCodeGenerator(output);
ProgramNode parseTree = builder.GetRootNode();
parseTree.Traverse(generator);
}
Procedural (conditional) Code
How to get rid of
conditional statements?
public void GenCode()
{
if (_left == null && _right == null)
{
Console.WriteLine("ldc.i4.s " + _value);
}
else
{
// its an intermediate node
_left.GenCode();
_right.GenCode();
switch (_value)
{
case "+":
Console.WriteLine("add");
break;
case "-":
Console.WriteLine("sub");
break;
case "*":
Console.WriteLine("mul");
break;
case "/":
Console.WriteLine("div");
break;
default:
Console.WriteLine("Not implemented yet!");
break;
}
}
}
Hands-on Exercise
Refactor the “../Composite/Before/Compiler.cs” to
get rid of conditional statements and use runtime
polymorphism instead
Hands-on Exercise
internal abstract class Expr
{
public abstract void GenCode();
}
internal class Constant : Expr
{
private readonly int _val;
public Constant(int val)
{
_val = val;
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
internal class BinaryExpr : Expr
{
private readonly Expr _left;
private readonly Expr _right;
public BinaryExpr(Expr arg1, Expr arg2)
{
_left = arg1;
_right = arg2;
}
public override void GenCode()
{
_left.GenCode();
_right.GenCode();
}
}
internal class Plus : BinaryExpr
{
public Plus(Expr arg1, Expr arg2) : base(arg1, arg2)
{
}
public override void GenCode()
{
base.GenCode();
Console.WriteLine("add");
}
}
Composite Pattern
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Composite Pattern: Discussion
❖ There are many situations where
a group of components form
larger components
❖ Simplistic approach: Make
container component contain
primitive ones
❖ Problem: Code has to treat
container and primitive
components differently
Compose objects into tree structures to represent part-whole hierarchies. Composite
lets client treat individual objects and compositions of objects uniformly.
❖ Perform recursive
composition of
components
❖ Clients don’t have to
treat container and
primitive components
differently
BTW, Did You Observe?
// ((10 * 20) + 30)
var expr = new Expr(
new Expr(
new Expr(null, "10", null), "*", new Expr(null, "20", null)),
"+",
new Expr(null, "30", null));
expr.GenCode();
// ((10 * 20) + 10)
Expr expr = new Plus(
new Multiply(
new Constant(10),
new Constant(20)),
new Constant(30));
expr.GenCode();
Hands-on Exercise
function interpret(node: binary expression tree)
| if node is a leaf then
return (node.value)
| if node is binary operator op then
return interpret(node.left) op interpret(node.right)
Rewrite the code to “evaluate” the expression instead
of generating the code - here is the “pseudo-code”
Solution
internal class Constant : Expr
{
private readonly int _val;
public Constant(int arg)
{
_val = arg;
}
public override int Interpret()
{
return _val;
}
}
internal class Plus : Expr
{
private readonly Expr _left;
private readonly Expr _right;
public Plus(Expr arg1, Expr arg2)
{
_left = arg1;
_right = arg2;
}
public override int Interpret()
{
return _left.Interpret() + _right.Interpret();
}
}
Register-Based Instructions (x86)
movl $10, -8(%rbp)
movl $20, -12(%rbp)
movl $30, -16(%rbp)
movl -8(%rbp), %eax
imull -12(%rbp), %eax
addl -16(%rbp), %eax
popq %rbp
retq
int main() {
int a = 10;
int b = 20;
int c = 30;
return ((a * b) + c);
}
cc -S expr.c
Register-Based Instructions (Android)
((a * b) + c);
javac -source 1.7 -target 1.7 expr.java
dx --dex --output=expr.dex expr.class
dexdump -d expr.dex
const/16 v1, #int 10 // #a
const/16 v2, #int 20 // #14
const/16 v3, #int 30 // #1e
mul-int v0, v1, v2
add-int/2addr v0, v3
return v0
Hands-on Exercise
Generate register-based instructions for the given
expression ((10 * 20) + 30)
You can generate for an imaginary machine (i.e., a
virtual machine like Dalvik/Android) or a real-
machine (like x86-64)
Solution
internal class Register
{
private readonly int _index;
private Register(int index)
{
_index = index;
}
public static Register GetRegister(int index)
{
return new Register(index);
}
// FreeRegister to be implemented
// not implemented for now for the sake of simplicity
public int GetIndex()
{
return _index;
}
}
Solution
abstract class Expr {
public abstract Register GenCode();
}
class Constant : Expr {
int val;
public Constant(int arg) {
val = arg;
}
public override Register GenCode() {
Register targetRegister = RegisterAllocator.GetNextRegister();
Console.WriteLine("const/16 v{0}, #int {1}", targetRegister.GetIndex(), val);
return targetRegister;
}
}
class Plus : Expr {
private Expr left, right;
public Plus(Expr arg1, Expr arg2) {
left = arg1;
right = arg2;
}
public override Register GenCode() {
Register firstRegister = left.GenCode();
Register secondRegister = right.GenCode();
Register targetRegister = RegisterAllocator.GetNextRegister();
Console.WriteLine("add-int v{0}, v{1}, v{2}", targetRegister.GetIndex(), firstRegister.GetIndex(),
secondRegister.GetIndex());
return targetRegister;
}
}
Solution
((10 + 20) + 30);
Assuming infinite registers from v0,
with target register as the first argument,
here is the mnemonic code generated for our
imaginary (i.e., virtual) machine
const/16 v0, #int 10
const/16 v1, #int 20
add-int v2, v0, v1
const/16 v3, #int 30
add-int v4, v2, v3
How to Improve the Tree Creation?
var expr = Addition.Make(
Multiplication.Make(
Constant.Make(10),
Constant.Make(20)),
Constant.Make(10));
Expr expr = new Plus(
new Multiply(
new Constant(10),
new Constant(20)),
new Constant(30));
Hands-on Exercise: Solution
internal class Constant : Expr
{
private readonly int _val;
private Constant(int val)
{
_val = val;
}
public static Constant Make(int arg)
{
return new Constant(arg);
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
internal class Constant : Expr
{
private readonly int _val;
public Constant(int val)
{
_val = val;
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
Factory Method Pattern: Structure
Factory Method Pattern: Discussion
❖ A class cannot anticipate the
class of objects it must create
❖ A class wants its subclasses to
specify the objects it creates
Define an interface for creating an object, but let subclasses decide which class to
instantiate. Factory method lets a class defer instantiation to subclasses.
❖ Delegate the responsibility
to one of the several helper
subclasses
❖ Also, localize the
knowledge of which
subclass is the delegate
Scenario
internal class Constant : Expr
{
private readonly int _val;
private Constant(int val)
{
_val = val;
}
public static Constant Make(int arg)
{
return new Constant(arg);
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
For expressions like ((10 * 20)
+ 10), why should we create
different Constant objects?
Can we reuse them?
Hands-on Exercise
Improve this code to reuse immutable objects (for the
ones with the same values) instead of creating
duplicate objects.
public static Constant Make(int arg)
{
return new Constant(arg);
}
Hands-on Exercise: Solution
internal class Constant : Expr
{
private static readonly
Dictionary<int, Constant> Pool = new Dictionary<int, Constant>();
private readonly int _val;
private Constant(int val)
{
_val = val;
}
public static Constant Make(int arg)
{
if (!Pool.ContainsKey(arg)) Pool[arg] = new Constant(arg);
return Pool[arg];
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
Flyweight Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Flyweight Pattern: Discussion
❖ When an application uses a
large number of small objects,
it can be expensive
❖ How to share objects at
granular level without
prohibitive cost?
Use sharing to support large numbers of fine-grained objects efficiently
❖ When it is possible to share
objects (i.e., objects don't
depend on identity)
❖ When object’s value
remain the same
irrespective of the contexts
- they can be shared
❖ Share the commonly used
objects in a pool
Scenario
// ((10 * 20) + 10)
var expr = Addition.Make(
Multiplication.Make(
Constant.Make(10),
Constant.Make(20)),
Constant.Make(10));
expr.GenCode();
This code still sucks - if the
expression becomes more
complex, can you really read
it? Can we simplify this?
Hands-on Exercise
Improve this code to use “builder pattern”.
// ((10 * 20) + 10)
var expr = Addition.Make(
Multiplication.Make(
Constant.Make(10),
Constant.Make(20)),
Constant.Make(10));
expr.GenCode();
var expr = new ExprBuilder().Const(10).Mult(20).Plus(30).Build();
Hands-on Exercise: Solution
internal class ExprBuilder
{
private Expr _expr;
public ExprBuilder Const(int arg)
{
_expr = Constant.Make(arg);
return this;
}
public ExprBuilder Plus(int arg)
{
_expr = new Addition(_expr, Constant.Make(arg));
return this;
}
public ExprBuilder Mult(int arg)
{
_expr = new Multiplication(_expr, Constant.Make(arg));
return this;
}
public Expr Build()
{
return _expr;
}
}
Builder Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Builder Pattern: Discussion
❖ Creating or assembling a
complex object can be tedious
Separate the construction of a complex object from its representation so that the same
construction process can create different representations.
❖ Make the algorithm for
creating a complex object
independent of parts that
make up the object and
how they are assembled
❖ The construction process
allows different
representations for the
object that is constructed
Scenario
Provide the ability to traverse
the aggregate structure
without exposing
implementation details (also
use iteration instead of
recursion)
internal class Constant : Expr
{
private readonly int _val;
public Constant(int val)
{
_val = val;
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
internal class Plus : BinaryExpr
{
public Plus(Expr arg1, Expr arg2) : base(arg1, arg2)
{
}
public override void GenCode()
{
base.GenCode();
Console.WriteLine("add");
}
}
Hands-on Exercise
Traverse the expression tree using “external” iteration
public override void GenCode()
{
base.GenCode();
Console.WriteLine("add");
}
Hands-on Exercise: Solution
internal class ExprIterator
{
public static IEnumerable<Expr> Traverse(Expr node)
{
if (node != null)
{
foreach (var left in Traverse(node.GetLeft())) yield return left;
foreach (var right in Traverse(node.GetRight())) yield return right;
yield return node;
}
}
}
foreach (var node in ExprIterator.Traverse(expr))
node.GenCode();
Iterator Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Iterator Pattern: Discussion
❖ How to provide a way to
traverse an aggregate structure
in different ways, but without
exposing its implementation
details?
Provide a way to access the elements of an aggregate object sequentially without
exposing its underlying representation.
❖ Take the responsibility for
access and traversal out of
the list object and put it
into an iterator object
❖ Let the iterator keep track
of the element visited,
traversal order, etc
Scenario
How to separate:
a) code generation logic
from node types?
b) how to support different
target types?
class Plus extends Expr {
private Expr left, right;
public Plus(Expr arg1, Expr arg2) {
left = arg1;
right = arg2;
}
public void genCode() {
left.genCode();
right.genCode();
if(t == Target.JVM) {
System.out.println("iadd");
}
else { // DOTNET
System.out.println("add");
}
}
}
Hands-on Exercise
Refactor this code to remove the if-else condition check
(explicit type-checking code) and use runtime
polymorphism instead
if(t == Target.JVM) {
System.out.println("iadd");
}
else { // DOTNET
System.out.println("add");
}
Hands-on Exercise
internal abstract class Target
{
public abstract void GenCode(Constant constant);
public abstract void GenCode(Plus plus);
public abstract void GenCode(Mult mult);
}
internal class JvmTarget : Target
{
public override void GenCode(Constant constant)
{
Console.WriteLine("bipush " + constant.GetValue());
}
public override void GenCode(Plus plus)
{
Console.WriteLine("iadd");
}
public override void GenCode(Mult mult)
{
Console.WriteLine("imul");
}
}
Strategy Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Strategy Pattern: Discussion
❖ Useful when there is a set of
related algorithms and a client
object needs to be able to
dynamically pick and choose
an algorithm that suits its
current need
Defines a family of algorithms, encapsulates each one, and makes
them interchangeable. Strategy lets the algorithm vary
independently from clients that use it
❖ The implementation of each of the
algorithms is kept in a separate
class referred to as a strategy.
❖ An object that uses a Strategy
object is referred to as a context
object.
❖ Changing the behavior of a
Context object is a matter of
changing its Strategy object to the
one that implements the required
algorithm
Solution Using Visitor pattern
class Plus extends Expr {
private Expr left, right;
public Plus(Expr arg1, Expr arg2) {
left = arg1;
right = arg2;
}
public Expr getLeft() {
return left;
}
public Expr getRight() {
return right;
}
public void accept(Visitor v) {
v.visit(this);
}
}
class DOTNETVisitor extends Visitor {
public void visit(Constant arg) {
System.out.println("ldarg " + arg.getVal());
}
public void visit(Plus plus) {
genCode(plus.getLeft());
genCode(plus.getRight());
System.out.println("add");
}
public void visit(Sub sub) {
genCode(sub.getLeft());
genCode(sub.getRight());
System.out.println("sub");
}
public void genCode(Expr expr) {
expr.accept(this);
}
}
Visitor Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Visitor Pattern: Call Sequence
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Visitor Pattern: Discussion
❖ Many distinct and unrelated
operations need to be
performed on objects in an
object structure, and you want
to avoid “polluting” their
classes with these operations
Represent an operation to be performed on the elements of an object structure.
Visitor lets you define a new operation without changing the classes of the
elements on which it operations
❖ Create two class
hierarchies:
❖ One for the elements
being operated on
❖ One for the visitors that
define operations on the
elements
Procedural (conditional) Code
How to get rid of
conditional statements?
internal class Interpreter
{
private static readonly Stack<int> ExecutionStack = new Stack<int>();
private static int Interpret(byte[] byteCodes)
{
var pc = 0;
while (pc < byteCodes.Length)
switch (byteCodes[pc++])
{
case (byte) BYTECODE.ADD:
ExecutionStack.Push(ExecutionStack.Pop() + ExecutionStack.Pop());
break;
case (byte) BYTECODE.LDCI4S:
ExecutionStack.Push(byteCodes[pc++]);
break;
}
return ExecutionStack.Pop();
}
// rest of the code elided ...
}
Hands-on Exercise
Refactor this code to remove the switch statements and
use runtime polymorphism instead
switch (byteCodes[pc++])
{
case (byte) BYTECODE.ADD:
ExecutionStack.Push(ExecutionStack.Pop() + ExecutionStack.Pop());
break;
case (byte) BYTECODE.LDCI4S:
ExecutionStack.Push(byteCodes[pc++]);
break;
}
Command Pattern: Solution
internal class Interpreter
{
private readonly Stack<int> _evalStack = new Stack<int>();
public int Interpret(ByteCode[] byteCodes)
{
foreach (var byteCode in byteCodes) byteCode.Exec(_evalStack);
return _evalStack.Pop();
}
}
internal abstract class ByteCode
{
public abstract void Exec(Stack<int> evalStack);
}
internal class ADD : ByteCode
{
public override void Exec(Stack<int> evalStack)
{
var lval = evalStack.Pop();
var rval = evalStack.Pop();
evalStack.Push(rval + lval);
}
}
Command Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Command Pattern: Discussion
❖ Want to issue requests or
execute commands without
knowing anything about the
operation being requested or
the receiver of the request
Encapsulate a request as an object, thereby letting you parameterize clients with
different requests, queue or log requests, and support undoable operations.
❖ Parameterize objects by an
action to perform, with a
common method such as
Execute
❖ Can be useful to specify,
queue, and execute
requests at different times;
also to support undo or
logging
Patterns Discussed in this Case-Study
✓ Facade
✓ Composite
✓ Factory method
✓ Flyweight
✓ Builder
✓ Iterator pattern
✓ Strategy
✓ Visitor
✓ Command
BTW, What Architecture Style is it?
Lexical Analysis
Parsing
Semantic Analysis
Program file
Intermediate Code
Generation
Optimization
Native Code
Generation
Native assembly/machine code
Token stream
AST (Abstract Syntax Tree)
Attributed tree/
augmented syntax tree
Intermediate code
Optimized
intermediate code
Its “pipe-and-filter” Style!
sediment
pre-
carbon
ultra-filter
post-
carbon
Filtered
water
Real-world “pipes and filters”
Pattern Oriented Software Architecture (POSA)
ganesh@codeops.tech @GSamarthyam
www.codeops.tech slideshare.net/sgganesh
+91 98801 64463 bit.ly/sgganesh

More Related Content

What's hot

Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 
Software re engineering
Software re engineeringSoftware re engineering
Software re engineeringdeshpandeamrut
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design PatternSanae BEKKAR
 
Garbage collection
Garbage collectionGarbage collection
Garbage collectionSomya Bagai
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsAnton Keks
 
User Interface Analysis and Design
User Interface Analysis and DesignUser Interface Analysis and Design
User Interface Analysis and Design Saqib Raza
 
Design Patterns Presentation - Chetan Gole
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan GoleChetan Gole
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns pptAman Jain
 
Java Networking
Java NetworkingJava Networking
Java NetworkingSunil OS
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
Design Pattern in Software Engineering
Design Pattern in Software EngineeringDesign Pattern in Software Engineering
Design Pattern in Software EngineeringManish Kumar
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for javamaheshm1206
 

What's hot (20)

Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Software re engineering
Software re engineeringSoftware re engineering
Software re engineering
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design Pattern
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
OOP java
OOP javaOOP java
OOP java
 
User Interface Analysis and Design
User Interface Analysis and DesignUser Interface Analysis and Design
User Interface Analysis and Design
 
Design Patterns Presentation - Chetan Gole
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan Gole
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Design Pattern in Software Engineering
Design Pattern in Software EngineeringDesign Pattern in Software Engineering
Design Pattern in Software Engineering
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for java
 

Similar to Design Patterns - Compiler Case Study - Hands-on Examples

Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#CodeOps Technologies LLP
 
Applied Design Patterns - A Compiler Case Study
Applied Design Patterns - A Compiler Case Study Applied Design Patterns - A Compiler Case Study
Applied Design Patterns - A Compiler Case Study CodeOps Technologies LLP
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and MonoidsHugo Gävert
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Platonov Sergey
 
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
Roslyn: el futuro de C# y VB.NET by Rodolfo FinochiettiRoslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti.NET Conf UY
 
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8tdc-globalcode
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and typesimtiazalijoono
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.pptssuserd64918
 
PVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications developmentPVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications developmentOOO "Program Verification Systems"
 

Similar to Design Patterns - Compiler Case Study - Hands-on Examples (20)

Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#
 
Applied Design Patterns - A Compiler Case Study
Applied Design Patterns - A Compiler Case Study Applied Design Patterns - A Compiler Case Study
Applied Design Patterns - A Compiler Case Study
 
Effective Object Oriented Design in Cpp
Effective Object Oriented Design in CppEffective Object Oriented Design in Cpp
Effective Object Oriented Design in Cpp
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
Novidades do c# 7 e 8
Novidades do c# 7 e 8Novidades do c# 7 e 8
Novidades do c# 7 e 8
 
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
Roslyn: el futuro de C# y VB.NET by Rodolfo FinochiettiRoslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
 
Roslyn: el futuro de C#
Roslyn: el futuro de C#Roslyn: el futuro de C#
Roslyn: el futuro de C#
 
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.ppt
 
PVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications developmentPVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications development
 
Interpreter Case Study - Design Patterns
Interpreter Case Study - Design PatternsInterpreter Case Study - Design Patterns
Interpreter Case Study - Design Patterns
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 

More from Ganesh Samarthyam

Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeGanesh Samarthyam
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”Ganesh Samarthyam
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGanesh Samarthyam
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionGanesh Samarthyam
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationGanesh Samarthyam
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterGanesh Samarthyam
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Ganesh Samarthyam
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckGanesh Samarthyam
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageGanesh Samarthyam
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Ganesh Samarthyam
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz QuestionsGanesh Samarthyam
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz QuestionsGanesh Samarthyam
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizGanesh Samarthyam
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesGanesh Samarthyam
 

More from Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 

Recently uploaded

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 

Recently uploaded (20)

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 

Design Patterns - Compiler Case Study - Hands-on Examples

  • 1. Compiler Case Study - Design Patterns in C# Ganesh Samarthyam ganesh@codeops.tech
  • 2. Design Patterns - Introduction
  • 3. “Applying design principles is the key to creating high-quality software!” Architectural principles: Axis, symmetry, rhythm, datum, hierarchy, transformation
  • 4. How to Apply Principles in Practice? Design principles Code How to bridge the gap?
  • 6. What is Refactoring? Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior Refactor (verb): to restructure software by applying a series of refactorings without changing its observable behavior
  • 7. What are Design Patterns? recurrent solutions to common design problems Pattern Name Problem Solution Consequences
  • 8. Principles to Patterns Expr expr = Addition.Make( Multiplication.Make( Constant.Make(10), Constant.Make(20)), Constant.Make(10)); Expr expr = new ExprBuilder().Const(10).Mult(20).Plus(30).Build(); Encapsulate object creation Builder pattern
  • 9. Why Care About Patterns? ❖ Patterns capture expert knowledge in the form of proven reusable solutions ❖ Better to reuse proven solutions than to “re-invent” the wheel ❖ When used correctly, patterns positively influence software quality ❖ Creates maintainable, extensible, and reusable code
  • 10. Design Patterns - Catalog Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 11. Design Patterns - Catalog Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994 Creational Deals with controlled object creation Factory method, for example Structural Deals with composition of classes or objects Composite, for example Behavioral Deals with interaction between objects/ classes and distribution of responsibility Strategy, for example
  • 12. 3 Principles Behind Patterns Design'principles' behind'pa0erns' Program'to'an' interface,'not'to'an' implementa7on'' Favor'object' composi7on'' over'inheritance' Encapsulate'what' varies'
  • 14. Case Study Overview ❖ Smelly code segments (specially if-else based on types and switch statements) used first ❖ They are “refactored” using design patterns ❖ The case study shows code-snippets; compilable, self- contained source code files shared with you
  • 15. What You Need to Know ❖ Understand essential object oriented constructs such as runtime polymorphism (virtual functions), and principles (such as encapsulation) ❖ Know essential C# language features - classes, inheritance, etc ❖ Data structures and algorithms (especially binary trees, tree traversals, and stacks) ❖ NO background in compilers needed ❖ Extremely simplified compiler code example; purposefully done to make best use of this case study
  • 16. Simple Expression Tree Example + * 30 10 20 Expression: ((10 * 20) + 30)
  • 17. Post-order Traversal: Simulating Code Gen 10 20 * 30 + post-order traversal result + * 30 10 20
  • 18. Front-end Intermediate code (CIL, Java bytecodes, …) Interpreter (Virtual Machine)
  • 19. Using Stack for Evaluation Initial empty 10 push 10 10 push 20 20 200 perform 10 * 20 push 200 200 push 30 30 230 perform 200 + 30 push 230 End result = 230 10 20 * 30 +
  • 20. Mnemonic Names in Java and .NET 10 20 * 30 + push 10 push 20 multiply push 30 add .NET CIL Instructions Java Bytecode Instructions ldc.i4.s 0x0a ldc.i4.s 0x14 mul ldc.i4.s 0x1e add (ldc => load constant ; i4 => integer of 32 bits; s => signed) bipush 10 bipush 20 imul bipush 30 iadd (bipush stands for “byte integer push”)
  • 21. Hands-on Exercise Use the ildasm tool to disassemble the Expr.cs code // Expr.cs using System; class Test {     public static void Main() {         int a = 10, b = 20, c = 30;         int r = ((a * b) + c);         Console.WriteLine(r);     } }
  • 22. ((10 * 20) + 30)Source Code C# Compiler .NET CLR (Common Language Runtime) ldc.i4.s 0x0a ldc.i4.s 0x14 mul ldc.i4.s 0x1e add + * 30 10 20
  • 23. Facade Pattern Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
  • 24. Facade Pattern in Compilers “The Compiler class acts as a facade: It offers clients a single, simple interface to the compiler subsystem.”
  • 25. Facade Pattern in Compilers public virtual void Compile(StreamReader input, BytecodeStream output) { Scanner scanner = new Scanner(input); var builder = new ProgramNodeBuilder(); Parser parser = new Parser(); parser.Parse(scanner, builder); RISCCodeGenerator generator = new RISCCodeGenerator(output); ProgramNode parseTree = builder.GetRootNode(); parseTree.Traverse(generator); }
  • 26. Procedural (conditional) Code How to get rid of conditional statements? public void GenCode() { if (_left == null && _right == null) { Console.WriteLine("ldc.i4.s " + _value); } else { // its an intermediate node _left.GenCode(); _right.GenCode(); switch (_value) { case "+": Console.WriteLine("add"); break; case "-": Console.WriteLine("sub"); break; case "*": Console.WriteLine("mul"); break; case "/": Console.WriteLine("div"); break; default: Console.WriteLine("Not implemented yet!"); break; } } }
  • 27. Hands-on Exercise Refactor the “../Composite/Before/Compiler.cs” to get rid of conditional statements and use runtime polymorphism instead
  • 28. Hands-on Exercise internal abstract class Expr { public abstract void GenCode(); } internal class Constant : Expr { private readonly int _val; public Constant(int val) { _val = val; } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } } internal class BinaryExpr : Expr { private readonly Expr _left; private readonly Expr _right; public BinaryExpr(Expr arg1, Expr arg2) { _left = arg1; _right = arg2; } public override void GenCode() { _left.GenCode(); _right.GenCode(); } } internal class Plus : BinaryExpr { public Plus(Expr arg1, Expr arg2) : base(arg1, arg2) { } public override void GenCode() { base.GenCode(); Console.WriteLine("add"); } }
  • 29. Composite Pattern Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 30. Composite Pattern: Discussion ❖ There are many situations where a group of components form larger components ❖ Simplistic approach: Make container component contain primitive ones ❖ Problem: Code has to treat container and primitive components differently Compose objects into tree structures to represent part-whole hierarchies. Composite lets client treat individual objects and compositions of objects uniformly. ❖ Perform recursive composition of components ❖ Clients don’t have to treat container and primitive components differently
  • 31. BTW, Did You Observe? // ((10 * 20) + 30) var expr = new Expr( new Expr( new Expr(null, "10", null), "*", new Expr(null, "20", null)), "+", new Expr(null, "30", null)); expr.GenCode(); // ((10 * 20) + 10) Expr expr = new Plus( new Multiply( new Constant(10), new Constant(20)), new Constant(30)); expr.GenCode();
  • 32. Hands-on Exercise function interpret(node: binary expression tree) | if node is a leaf then return (node.value) | if node is binary operator op then return interpret(node.left) op interpret(node.right) Rewrite the code to “evaluate” the expression instead of generating the code - here is the “pseudo-code”
  • 33. Solution internal class Constant : Expr { private readonly int _val; public Constant(int arg) { _val = arg; } public override int Interpret() { return _val; } } internal class Plus : Expr { private readonly Expr _left; private readonly Expr _right; public Plus(Expr arg1, Expr arg2) { _left = arg1; _right = arg2; } public override int Interpret() { return _left.Interpret() + _right.Interpret(); } }
  • 34. Register-Based Instructions (x86) movl $10, -8(%rbp) movl $20, -12(%rbp) movl $30, -16(%rbp) movl -8(%rbp), %eax imull -12(%rbp), %eax addl -16(%rbp), %eax popq %rbp retq int main() { int a = 10; int b = 20; int c = 30; return ((a * b) + c); } cc -S expr.c
  • 35. Register-Based Instructions (Android) ((a * b) + c); javac -source 1.7 -target 1.7 expr.java dx --dex --output=expr.dex expr.class dexdump -d expr.dex const/16 v1, #int 10 // #a const/16 v2, #int 20 // #14 const/16 v3, #int 30 // #1e mul-int v0, v1, v2 add-int/2addr v0, v3 return v0
  • 36. Hands-on Exercise Generate register-based instructions for the given expression ((10 * 20) + 30) You can generate for an imaginary machine (i.e., a virtual machine like Dalvik/Android) or a real- machine (like x86-64)
  • 37. Solution internal class Register { private readonly int _index; private Register(int index) { _index = index; } public static Register GetRegister(int index) { return new Register(index); } // FreeRegister to be implemented // not implemented for now for the sake of simplicity public int GetIndex() { return _index; } }
  • 38. Solution abstract class Expr { public abstract Register GenCode(); } class Constant : Expr { int val; public Constant(int arg) { val = arg; } public override Register GenCode() { Register targetRegister = RegisterAllocator.GetNextRegister(); Console.WriteLine("const/16 v{0}, #int {1}", targetRegister.GetIndex(), val); return targetRegister; } } class Plus : Expr { private Expr left, right; public Plus(Expr arg1, Expr arg2) { left = arg1; right = arg2; } public override Register GenCode() { Register firstRegister = left.GenCode(); Register secondRegister = right.GenCode(); Register targetRegister = RegisterAllocator.GetNextRegister(); Console.WriteLine("add-int v{0}, v{1}, v{2}", targetRegister.GetIndex(), firstRegister.GetIndex(), secondRegister.GetIndex()); return targetRegister; } }
  • 39. Solution ((10 + 20) + 30); Assuming infinite registers from v0, with target register as the first argument, here is the mnemonic code generated for our imaginary (i.e., virtual) machine const/16 v0, #int 10 const/16 v1, #int 20 add-int v2, v0, v1 const/16 v3, #int 30 add-int v4, v2, v3
  • 40. How to Improve the Tree Creation? var expr = Addition.Make( Multiplication.Make( Constant.Make(10), Constant.Make(20)), Constant.Make(10)); Expr expr = new Plus( new Multiply( new Constant(10), new Constant(20)), new Constant(30));
  • 41. Hands-on Exercise: Solution internal class Constant : Expr { private readonly int _val; private Constant(int val) { _val = val; } public static Constant Make(int arg) { return new Constant(arg); } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } } internal class Constant : Expr { private readonly int _val; public Constant(int val) { _val = val; } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } }
  • 43. Factory Method Pattern: Discussion ❖ A class cannot anticipate the class of objects it must create ❖ A class wants its subclasses to specify the objects it creates Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses. ❖ Delegate the responsibility to one of the several helper subclasses ❖ Also, localize the knowledge of which subclass is the delegate
  • 44. Scenario internal class Constant : Expr { private readonly int _val; private Constant(int val) { _val = val; } public static Constant Make(int arg) { return new Constant(arg); } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } } For expressions like ((10 * 20) + 10), why should we create different Constant objects? Can we reuse them?
  • 45. Hands-on Exercise Improve this code to reuse immutable objects (for the ones with the same values) instead of creating duplicate objects. public static Constant Make(int arg) { return new Constant(arg); }
  • 46. Hands-on Exercise: Solution internal class Constant : Expr { private static readonly Dictionary<int, Constant> Pool = new Dictionary<int, Constant>(); private readonly int _val; private Constant(int val) { _val = val; } public static Constant Make(int arg) { if (!Pool.ContainsKey(arg)) Pool[arg] = new Constant(arg); return Pool[arg]; } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } }
  • 47. Flyweight Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 48. Flyweight Pattern: Discussion ❖ When an application uses a large number of small objects, it can be expensive ❖ How to share objects at granular level without prohibitive cost? Use sharing to support large numbers of fine-grained objects efficiently ❖ When it is possible to share objects (i.e., objects don't depend on identity) ❖ When object’s value remain the same irrespective of the contexts - they can be shared ❖ Share the commonly used objects in a pool
  • 49. Scenario // ((10 * 20) + 10) var expr = Addition.Make( Multiplication.Make( Constant.Make(10), Constant.Make(20)), Constant.Make(10)); expr.GenCode(); This code still sucks - if the expression becomes more complex, can you really read it? Can we simplify this?
  • 50. Hands-on Exercise Improve this code to use “builder pattern”. // ((10 * 20) + 10) var expr = Addition.Make( Multiplication.Make( Constant.Make(10), Constant.Make(20)), Constant.Make(10)); expr.GenCode(); var expr = new ExprBuilder().Const(10).Mult(20).Plus(30).Build();
  • 51. Hands-on Exercise: Solution internal class ExprBuilder { private Expr _expr; public ExprBuilder Const(int arg) { _expr = Constant.Make(arg); return this; } public ExprBuilder Plus(int arg) { _expr = new Addition(_expr, Constant.Make(arg)); return this; } public ExprBuilder Mult(int arg) { _expr = new Multiplication(_expr, Constant.Make(arg)); return this; } public Expr Build() { return _expr; } }
  • 52. Builder Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 53. Builder Pattern: Discussion ❖ Creating or assembling a complex object can be tedious Separate the construction of a complex object from its representation so that the same construction process can create different representations. ❖ Make the algorithm for creating a complex object independent of parts that make up the object and how they are assembled ❖ The construction process allows different representations for the object that is constructed
  • 54. Scenario Provide the ability to traverse the aggregate structure without exposing implementation details (also use iteration instead of recursion) internal class Constant : Expr { private readonly int _val; public Constant(int val) { _val = val; } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } } internal class Plus : BinaryExpr { public Plus(Expr arg1, Expr arg2) : base(arg1, arg2) { } public override void GenCode() { base.GenCode(); Console.WriteLine("add"); } }
  • 55. Hands-on Exercise Traverse the expression tree using “external” iteration public override void GenCode() { base.GenCode(); Console.WriteLine("add"); }
  • 56. Hands-on Exercise: Solution internal class ExprIterator { public static IEnumerable<Expr> Traverse(Expr node) { if (node != null) { foreach (var left in Traverse(node.GetLeft())) yield return left; foreach (var right in Traverse(node.GetRight())) yield return right; yield return node; } } } foreach (var node in ExprIterator.Traverse(expr)) node.GenCode();
  • 57. Iterator Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 58. Iterator Pattern: Discussion ❖ How to provide a way to traverse an aggregate structure in different ways, but without exposing its implementation details? Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. ❖ Take the responsibility for access and traversal out of the list object and put it into an iterator object ❖ Let the iterator keep track of the element visited, traversal order, etc
  • 59. Scenario How to separate: a) code generation logic from node types? b) how to support different target types? class Plus extends Expr { private Expr left, right; public Plus(Expr arg1, Expr arg2) { left = arg1; right = arg2; } public void genCode() { left.genCode(); right.genCode(); if(t == Target.JVM) { System.out.println("iadd"); } else { // DOTNET System.out.println("add"); } } }
  • 60. Hands-on Exercise Refactor this code to remove the if-else condition check (explicit type-checking code) and use runtime polymorphism instead if(t == Target.JVM) { System.out.println("iadd"); } else { // DOTNET System.out.println("add"); }
  • 61. Hands-on Exercise internal abstract class Target { public abstract void GenCode(Constant constant); public abstract void GenCode(Plus plus); public abstract void GenCode(Mult mult); } internal class JvmTarget : Target { public override void GenCode(Constant constant) { Console.WriteLine("bipush " + constant.GetValue()); } public override void GenCode(Plus plus) { Console.WriteLine("iadd"); } public override void GenCode(Mult mult) { Console.WriteLine("imul"); } }
  • 62. Strategy Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 63. Strategy Pattern: Discussion ❖ Useful when there is a set of related algorithms and a client object needs to be able to dynamically pick and choose an algorithm that suits its current need Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it ❖ The implementation of each of the algorithms is kept in a separate class referred to as a strategy. ❖ An object that uses a Strategy object is referred to as a context object. ❖ Changing the behavior of a Context object is a matter of changing its Strategy object to the one that implements the required algorithm
  • 64. Solution Using Visitor pattern class Plus extends Expr { private Expr left, right; public Plus(Expr arg1, Expr arg2) { left = arg1; right = arg2; } public Expr getLeft() { return left; } public Expr getRight() { return right; } public void accept(Visitor v) { v.visit(this); } } class DOTNETVisitor extends Visitor { public void visit(Constant arg) { System.out.println("ldarg " + arg.getVal()); } public void visit(Plus plus) { genCode(plus.getLeft()); genCode(plus.getRight()); System.out.println("add"); } public void visit(Sub sub) { genCode(sub.getLeft()); genCode(sub.getRight()); System.out.println("sub"); } public void genCode(Expr expr) { expr.accept(this); } }
  • 65. Visitor Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 66. Visitor Pattern: Call Sequence Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 67. Visitor Pattern: Discussion ❖ Many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid “polluting” their classes with these operations Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operations ❖ Create two class hierarchies: ❖ One for the elements being operated on ❖ One for the visitors that define operations on the elements
  • 68. Procedural (conditional) Code How to get rid of conditional statements? internal class Interpreter { private static readonly Stack<int> ExecutionStack = new Stack<int>(); private static int Interpret(byte[] byteCodes) { var pc = 0; while (pc < byteCodes.Length) switch (byteCodes[pc++]) { case (byte) BYTECODE.ADD: ExecutionStack.Push(ExecutionStack.Pop() + ExecutionStack.Pop()); break; case (byte) BYTECODE.LDCI4S: ExecutionStack.Push(byteCodes[pc++]); break; } return ExecutionStack.Pop(); } // rest of the code elided ... }
  • 69. Hands-on Exercise Refactor this code to remove the switch statements and use runtime polymorphism instead switch (byteCodes[pc++]) { case (byte) BYTECODE.ADD: ExecutionStack.Push(ExecutionStack.Pop() + ExecutionStack.Pop()); break; case (byte) BYTECODE.LDCI4S: ExecutionStack.Push(byteCodes[pc++]); break; }
  • 70. Command Pattern: Solution internal class Interpreter { private readonly Stack<int> _evalStack = new Stack<int>(); public int Interpret(ByteCode[] byteCodes) { foreach (var byteCode in byteCodes) byteCode.Exec(_evalStack); return _evalStack.Pop(); } } internal abstract class ByteCode { public abstract void Exec(Stack<int> evalStack); } internal class ADD : ByteCode { public override void Exec(Stack<int> evalStack) { var lval = evalStack.Pop(); var rval = evalStack.Pop(); evalStack.Push(rval + lval); } }
  • 71. Command Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 72. Command Pattern: Discussion ❖ Want to issue requests or execute commands without knowing anything about the operation being requested or the receiver of the request Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. ❖ Parameterize objects by an action to perform, with a common method such as Execute ❖ Can be useful to specify, queue, and execute requests at different times; also to support undo or logging
  • 73. Patterns Discussed in this Case-Study ✓ Facade ✓ Composite ✓ Factory method ✓ Flyweight ✓ Builder ✓ Iterator pattern ✓ Strategy ✓ Visitor ✓ Command
  • 74. BTW, What Architecture Style is it? Lexical Analysis Parsing Semantic Analysis Program file Intermediate Code Generation Optimization Native Code Generation Native assembly/machine code Token stream AST (Abstract Syntax Tree) Attributed tree/ augmented syntax tree Intermediate code Optimized intermediate code
  • 76. Pattern Oriented Software Architecture (POSA)