You are on page 1of 16

Design Patterns

Factory Method
 A creational pattern
 Used to deal with the problem of creating
objects without having to specify the exact
class of the object that will be created.

2
class Polygon {
protected:
int width, height;
public:
Polygon (int a, int b) : width(a), height(b) {}
virtual int area (void) =0;
void printarea()
{ cout << this->area() << '\n'; }
};
3
class Rectangle: public Polygon {
public:
Rectangle(int a,int b) : Polygon(a,b) {}
int area()
{ return width*height; }
};

class Triangle: public Polygon {


public:
Triangle(int a,int b) : Polygon(a,b) {}
int area()
{ return width*height/2; }
};

4
int main () {
Polygon * ppoly1 = new Rectangle (4,5);
Polygon * ppoly2 = new Triangle (4,5);
ppoly1->printarea();
ppoly2->printarea();
delete ppoly1;
delete ppoly2;
return 0;
}
5
int main () {
// Polygon * ppoly1 = new Rectangle (4,5);
// Polygon * ppoly2 = new Triangle (4,5);
Rectangle * ppoly1 = new Rectangle (4,5)
Triangle * ppoly2 = new Triangle (4,5);
ppoly1->printarea();
ppoly2->printarea();
delete ppoly1;
delete ppoly2;
return 0;
}
6
class Employee
{
public:
virtual void raiseSalary()
{ /* common raise salary code */ }

virtual void promote()


{ /* common promote code */ }
};

7
class Manager: public Employee {
virtual void raiseSalary()
{ /* Manager specific raise salary code, may contain
increment of manager specific incentives*/ }

virtual void promote()


{ /* Manager specific promote */ }
};

// Similarly, there may be other types of employees

8
// increment salary of all employees
void globalRaiseSalary(Employee *emp[], int n)
{
for (int i = 0; i < n; i++)
emp[i]->raiseSalary();
// Polymorphic Call: Calls raiseSalary()
}

9
 How does that happen – the magic behind
polymorphism

10
How does compiler do this magic?
 Compiler maintains two things:
vtable: A table of function pointers. It is maintained per class.
vptr: A pointer to vtable. It is maintained per object.
 Compiler adds additional code at two places to maintain and use
vptr.
– Code in every constructor. This code sets vptr of the object being
created. This code sets vptr to point to vtable of the class.
– Code with polymorphic function call.
 Wherever a polymorphic call is made, compiler inserts code to first
look for vptr.
 Once vptr is fetched, vtable of derived class can be accessed.
Using vtable, address of derived.

11
12
Object creation and polymorphism

13
Object creation is not
// client program polymorphic
int main () {
Polygon * ppoly[10];
for (int i = 0; i < 10; i++) {
// get type, width, and height of the object
if (t == “R”)
ppoly[i] = new Rectangle(w, h);
else if (t == “T”)
ppoly[i] = new Triangle(w, h);
}
for (int i = 0; i < 10; i++)
ppoly[i]->printarea();
// rest of the code
return 0; What happens when a
} new polygon type is
14
added to the system?
Object creation and polymorphism – Factory Method

Polygon * createPolygon(char t, int w, int h) {


if (t == ‘R’)
return new Rectangle(w, h);
else if (t == ‘T’)
return new Triangle(w, h);
}

15
int main () {
Polygon * ppoly[10];
for (int i = 0; i < 10; i++) {
// get type, width, and height of the object
// to be created
ppoly[i] = createPolygon(t, w, h);
// pseudo polymorphic behavior
}
for (int i = 0; i < 10; i++)
ppoly[i]->printarea();
// rest of the code
return 0;
}
16

You might also like