473,804 Members | 3,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help me in implementing Factory design pattern

39 New Member
i am using vc++
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3. class esteem;
  4. class zen;
  5. class maruti
  6. {
  7.     char name[10];
  8. public:
  9.     maruti(){}
  10.     void getdata()
  11.     {
  12.         cout<<"get name\n";
  13.         cin>>name;
  14.     }
  15.     maruti* getobject()
  16.     {
  17.         if (name == "esteem")
  18.             return esteem.Instance();
  19.         else 
  20.             return new zen();
  21.     }
  22.     virtual void print() {}
  23. };
  24. class esteem:public maruti
  25. {
  26.      static esteem* pinstance; // initialize pointer
  27. public:
  28.     esteem(){}
  29.      static esteem *Instance () 
  30.     {
  31.     if (pinstance == 0)  // is it the first call?
  32.     {  
  33.       pinstance = new esteem(); // create sole instance
  34.     }
  35.     return pinstance; // address of sole instance
  36.     }
  37.     void print()
  38.     {
  39.         cout<<"in esteem\n";
  40.     }
  41.  
  42. };
  43. class zen:public maruti
  44. {
  45. public:
  46.     zen(){}
  47.     void print()
  48.     {
  49.         cout<<"in zen\n";
  50.     }
  51.  
  52. };
  53.  
  54. int main()
  55. {
  56.     maruti *m,*k,j;
  57.     m=&j;
  58.     m->getdata();
  59.     k = m->getobject();
  60.     k->print();
  61.     return 0;
  62. }
  63.  
  64.  
  65.  
  66.  
this is the whole code
i am getting errors
error C2512: 'zen' : no appropriate default constructor available
this is the main error
so tried another way to create the object of esteem class
but even this is showing erros
please help me
Sep 21 '07 #1
1 1249
weaknessforcats
9,208 Recognized Expert Moderator Expert
I am assuming maruti is your factory and esteem and zen are classes for which you want the factory to create an objects.

First then, the classes you want objects of do not derive from the factory. That is, the object produced by the factory is not a kind of factory.

Your factory maruti::getobje ct() needs to return either an esteem* or a zen* and it can't do both. And you can't derive esteem and zen from maruti.

You have very many things not correct here.
1) your static esteem::pInstan ce variable was never created
2) you cannot define a static method inside the class declaration. You can do that only with member funcuons
3) esteem and zen to not derive from maruti
4) Your main() should look like:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     maruti* theFactory = new maruti;
  4. esteem* obj = CreateEsteem(theFactory);
  5.  
  6.     return 0;
  7. }
  8.  
That is, you create the factory and then pass the factorry to a create function to crate the required object:
Expand|Select|Wrap|Line Numbers
  1.  esteem* CreateEsteem(maruti* arg)
  2. {
  3.      return arg->getesteemobject();
  4. }
  5.  
The maruti::geteste emobject() returns an esteem*:
Expand|Select|Wrap|Line Numbers
  1. esteem* maruti::getesteemobject()
  2.     {
  3.         return esteem::Instance();
  4.  
  5.     }
  6.  
This is all based on the fact that esteem and zen are independent classes.

If they are not, then they need to derive from a base class and the factory method creates a derived object and returns a base class pointer. This would be a parameterized factory:
Expand|Select|Wrap|Line Numbers
  1. quality* maruti::getobject(marutitype arg)
  2.     {
  3.          if (arg == ESTEEM)
  4.          {
  5.            return esteem::Instance();
  6.          }
  7.          if (arg == ZEN)
  8.          {
  9.            return new zen;         }
  10.  
  11.     }
  12.  
where:

class maruti
{
public:
enum marutitype {ESTEEM, ZEN);
etc...
[/code]
Sep 21 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

17
6647
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products. So I shall have an Abstract Base called 'Car' implemented by Toyota, Ford, and Buick. Further I'd like to enable to client to say Car *factory;
1
1307
by: Julia | last post by:
Hi I have a domain model and I am looking for the correct design patterns to use, The following is my domain model Server-> the is the thread boundaries,the server create a thread the Thread create the Manager ,the Manager create the MessagingService and
11
4312
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns. They are still online at http://www.fluffycat.com/java/patterns.html Since September 2003 I've mainly been using PHP, and now that PHP 5 is becoming more available I am going to try the same thing I did in Java with PHP.
3
1821
by: Christer | last post by:
Hi all! We're creating an administration for a web application. This includes severeal aspx pages. Our key entity in the web application is "Profile", which can have a certain type. Based on the profile type, the administration pages will have some small changes, such as displaying a few extra tabs, or maybe change the available choices in a dropdown.
2
1586
by: max | last post by:
Hello, I analyze this design pattern for a long time but I do not understand how this pattern work and what the purpose is? (I looked a this site http://www.dofactory.com/Patterns/PatternAbstract.aspx). Could anybody try to explain me in his own words how this pattern work and what the purpose is? thanks in advance
2
1397
by: Chris | last post by:
Hi, I have been stuck trying to come up with a design for days. I am working on a small project regarding barcode and I want to implement a factory design. I am now confused. I decided factory pattern since I am dealing with EAN13, UPCA12 UPC8 etc. Can someone provide a small sample on how to start. I have looked at the factory implementation online but still can't figure it out. Can someone provide a sample jsut to start with.
1
2345
by: orel | last post by:
Please, As i tried hundreds different implementation to make it work and actually didn't succeed, can someone here help me to understand and use the implementation of the Object Factory design pattern. I'm using gcc 3.4.2 on Mingw. The factory is the same as the one in Modern C++ design (without error checking) , like this :
10
2355
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. #include<iostream>
22
1930
by: ajos | last post by:
Hi friends, I am trying to implement a factory design pattern.I'm quite confused on how to put it down here.Let me try.I have certain html option containing values like example---> <html:select property="branch" size="1"> <html:option value="California"></html:option> <html:option value="New York"></html:option> and so on.... the option value here is lage.so ive been told to implement the factory pattern.I've been...
4
5020
by: Pallav singh | last post by:
Hi , when should i select Factory Method / Prototype Design Pattern during my design phase ?? as both look similar to me Thanks in Advance Thanks Pallav
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.