Comments on my Factory Pattern

Hi my name is Kaur, I have studied c++ for about 1.5 months. I just finished experimenting with factory pattern and this is the code i came up with.
Is it rational, or maybe i didn't get the concept of factory pattern right?


I got 2 abstract classes AbstractComputer and AbstractFactor just for the interface for concrete classes. There are 3 different types of computers PC MAC and SPARC and a factory for everyone of them. ComputerAssamble creates Computer objects through factories. MainClass does the input check and decides which factory to feed ComputerAssamble.
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
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <vector>


using namespace std;

class AbstractComputer{
      public:
        virtual string getCompleteDescription()=0;
        static AbstractComputer* object();
     
};

class AbstractFactory{
      public:
        virtual AbstractComputer* produce()=0;
             
      
};

class PC : public AbstractComputer{
      PC(){};
      public:               
          static AbstractComputer* object(){return new PC;}
          string getCompleteDescription(){ return "PC HAS BEEN PRODUCED\n";}
          
};

class MAC : public AbstractComputer{
      MAC(){};
      public:           
          static AbstractComputer* object(){return new MAC;}
          string getCompleteDescription(){ return "MAC HAS BEEN PRODUCED\n";}
      
};

class SPARC : public AbstractComputer{
      SPARC(){};
      public:      
          static AbstractComputer* object(){return new SPARC;}  
          string getCompleteDescription(){ return "SPARC HAS BEEN PRODUCED\n";}
      
};

class PCFactory : public AbstractFactory{
      public:
        AbstractComputer* produce(){ return PC::object();}
      
};

class MACFactory : public AbstractFactory{
      public:
        AbstractComputer* produce(){ return MAC::object();}
      
};

class SPARCFactory : public AbstractFactory{
      public:
        AbstractComputer* produce(){ return SPARC::object();}
      
};


class ComputerAssambler{
      
      public:
        static void AssambleComputer(AbstractFactory* factory){
               
               if(factory == NULL)
                   ;      
               else{
                 AbstractComputer* computer = factory->produce();
                 cout<<computer->getCompleteDescription();
                 }
              }
      
};


class MainClass{
      
      
      public:
             static void Main(string type){
                    
                    AbstractFactory* factory=NULL;
             
                    if(type.compare("PC") == 0)
                         factory = new PCFactory;  
                    else if(type.compare("MAC") == 0)
                         factory = new MACFactory;
                    else if(type.compare("SPARC") == 0)
                         factory = new SPARCFactory;  
                    else
                        cout<<"This pc is not in production"<<endl;
                        
                    ComputerAssambler::AssambleComputer(factory);
                 }
     
};


int main()
{

    
    cout<<"Type the name of the computer you would like produced: PC, MAC, SPARC"<<endl;
    char answer[4];
    while(1==1)
    {
               
    scanf("%s",answer);
    MainClass::Main(answer);
    
    }

    
    system("pause");
    return 0;


};


I won't make any comments at this point - I'm going to revise my notes on the abstract factory pattern - becasue this code seems to be off the mark.
Last edited on
Thx man, will wait for a reply!
The first thing wondering why you need a seperate factory for each type of computer - after all they inherit from AbstractComputer - so they can all be 'made' by the same factory.

Also the MainClass factory creates a specific factory such as SPARCFactory - but this specific factory does not actually create the product - it delegates it to a static function of the final product itself.

It just seems over-engineeered to me.
Have to agree with you. I took Design Patterns by GoF , didnt have the time to read it though, will do so tomorrow. I will post a new code if i manage to write something more rational.

Thx for discussing this with me.
I thought it looked rather javaesque

It just seems over-engineeered to me.


I have to agree. I have seen a lot of over-engineering with classes that wrap and wrap each other layer by layer until the main picture is lost due to that in my work-place. Sometimes I wonder, why do developers over-engineer? To me the appropriate level of engineering is good enough. We do not have to carry it to the extreme.

I guess GoF has some blame to share. They created a big hoo-ha in the IT circles and some adopters are so keen that they implement it in so many different ways when at the end of the day what business users want is a software that run fast and accurately for their daily operations.
Topic archived. No new replies allowed.