Factory Design Pattern Example - Review

Hi ,

I have written the below program to demonstrate factory design pattern out of my understanding from the study.

my Doubt is almost all the examples on internet are using interface create function as static, can this be a proper example for factory design pattern.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

#include <iostream>

using namespace std;

enum ProductType { ProductType1 = 0, ProductType2 };

//Interface
class InterfaceClass
{
    public:
        virtual void nameOfProduct() = 0;
        void createProduct(ProductType type);
};

//product 1
class Product1Class : public InterfaceClass
{
    public:
        virtual void nameOfProduct() {
            cout<<"Creating Product 1" <<endl;
        }
};

//product 2
class Product2Class : public InterfaceClass
{
    public:
        virtual void nameOfProduct() {
            cout<<"Creating Product 2" <<endl;
        }
};

#if 0
/*
client class implements the products
if library class (Interface class ) changes by adding a new product , 
we will have to add one more if-else to the new product, 
so for complex class or products this will be not desired

*/
class ClientClass{
    private:
        InterfaceClass *Prod;
    public:
    ClientClass(ProductType type){
        if(type == ProductType1 )
            Prod = new Product1Class;
        else if(type == ProductType2)
            Prod =new Product2Class;
    }
    
    ~ClientClass(){
        if(Prod)
            delete Prod;
        Prod = NULL;
    }
    InterfaceClass *getProduct() {
        return Prod;
    }
        
};

#endif

/*
    Better design   
*/

InterfaceClass * createProduct(ProductType type)
{
    InterfaceClass * Prod = NULL;
    
     if(type == ProductType1 )
        Prod = new Product1Class;
        else if(type == ProductType2)
        Prod =new Product2Class;   
        
        return Prod;
}

class ClientClass{
    private:
        InterfaceClass *Prod;
    public:
    ClientClass(ProductType type){
        Prod  = createProduct(type);
    }
    ~ClientClass(){
        if(Prod)
            delete Prod;
        Prod = NULL;
    }
    InterfaceClass *getProduct() {
        return Prod;
    }
        
};


int main() {

    ClientClass *Cp1 = new ClientClass(ProductType1);
    Cp1->getProduct()->nameOfProduct();
    
    ClientClass *Cp2 = new ClientClass(ProductType2);
    Cp2->getProduct()->nameOfProduct();
    
    return 0;
}


Last edited on
A proper factory should be static. This is because it should technically not require access based on individual objects. All it should do is return an object of that class's type, which could be a subclass/derived type.

std::make_shared is a popular example of a factory function in the standard library
Try not to put obvious information in the names of entities. It is obvious from the declaration
class ClientClass
That ClientClass is a class type.

Similarly, it is obvious from the declaration
int CounterVariable
That CounterVariable is a variable.

Either use the space to make the code clearer, or make the code shorter, and eliminate the obvious information.
Last edited on
A factory could also be a class if it needs resources.
Imagine a factory that creates sprites.
Sprites might need bitmaps, so the factory need a way to know about the bitmaps and store them.
A factory can also be abstract so that you can choose between different factories at runtime.
Topic archived. No new replies allowed.