CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 32
  1. #16
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Abstract Factory Design Patterns Advice

    I don´t get it. All your account types are unrelated, they´re neither derived from any other class nor are they base class of any other class. Why do you need a separate factory class for each of your account types? Don´t make things more complicated than they really are, just use an if-else clause like
    D_Drmmr already suggested. You can move the account creation into a factory, so you have one factory object that creates an account object depending on the login type. Specifying an interface for the factory allows extension and adding other factories later.

    There are two possible scenarios that come into my mind:

    1) A factory that creates instances of a fixed set of accounts:

    Code:
    class AbstractAccountFactory
    {
    public:
       virtual ~AbstractAccountFactory();
    
       UserAccount    create_user_account() virtual = 0;
       AdminAccount  create_admin_account() virtual = 0;
       StaffAccount    create_staff_account() virtual = 0;
    };
    
    class Win32AccountFactory : public AbstractFactory
    {
    ...
    };
    
    class UnixAccountFactory : public AbstractFactory
    {
    ...
    };
    I don´t know if the example above makes any sense, but it demonstrates how to use the abstract factory pattern to create instances of a fixed set of object types.

    The other scenario is a hierachy where all accounts are derived from a base account class and the factory decides which derived class to instantiate depending on the login type. This fits the case you are describing:

    Code:
    class AccountFactory
    {
    public:
       AbstractAccount* create_account( std::string& login )
       {
          if( false == login.empty() )
          {
             switch( login[0] )
             {
                case 'H' : return create_user_account();
                case 'A' : return create_admin_account();
                case 'S' : return create_staff_account();
             }
          }
          // return 0 or throw an exception
          return 0;
       }
    
    private:
       AbstractAccount* create_user_account()
       {
          ...
       }
    
       AbstractAccount* create_admin_account()
       {
          ...
       }
    
       AbstractAccount* create_staff_account()
       {
          ...
       }
    };
    You can even create an InvalidAccount account type and return it instead of returning 0 or throwing an exception. The InvalidAccount simply fails on all operations made on it.
    - Guido

  2. #17
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Abstract Factory Design Patterns Advice

    Actually, my purpose is to learn abstract factory, factory method and OOP. Your second approach is not bad but need if statement.

    I have a solution like this My solution is like this. Basically, there are two hierarchy which are ObjecFactory which have three derived classes which are AdminFactory, HRFactory and StaffFactory. Another class hierarchy is wraps login session inside usernameType which have three derived classes also same as above.

    I was thinking the difficulties in creating concrete usernameType then forward to concrete objectFactory to create the object. Now, i have solution which is using factory method in usernameType hierarchy. Is this possible ?

    Thanks.

    Please comment on this approach rather spoon feed me with some code and explain why this approach doesn't works and other approach is better.

    Thanks.
    Last edited by Peter_APIIT; June 25th, 2009 at 05:16 AM.
    Thanks for your help.

  3. #18
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Abstract Factory Design Patterns Advice

    Let me summarize:

    1) You have got a username string whose first character specifies which account type to create
    2) You don´t want to use if-else statements to find the appropriate account type
    3) You want to use a separate factory object to create an account depending on the username information

    Some other people and me commented your approach and all of them told you NOT to use different objects to create account objects or use an if-else/switch statement respectively.
    You, on the other side, ignore all these comments and insist on your approach, no matter how unsuitable it is.
    No matter what and no matter how you do it, at some point you have to look at the first character of the username, there´s simply no other way except for magic.
    Furthermore, your account types are unrelated, they have no common base class, so the abstract factory pattern might not be applied at all. If the sole purpose of this thread is to get an understanding of how to use the abstract factory pattern then you better choose and example where it can be applied.
    Please explain how your design fits together with the abstract factory pattern
    Last edited by GNiewerth; June 25th, 2009 at 06:46 AM.
    - Guido

  4. #19
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Abstract Factory Design Patterns Advice

    OK. I have refer to this website and i find that what you said is quite true.

    http://www.c-sharpcorner.com/UploadF...ctFactory.aspx

    My class hierarchy is like this.

    Code:
                 objectFactory
                      |
    AdminFactory  HRFactory StaffFactory  
    
    
           usernameType  (Contain reference to login)
                      |
    AdminType  HRType  Stafftype  (Forward call to concrete factory)
    
    class login; 
    
                Human
                    |
    Admin      Hr     Staff
    Yes, this class hierarchy does not exist a abstract product or related/family product. Please comment on this,

    Actually, i not ignore your solution but just want clear the problem.

    I bag your pardon if i seems stupid.


    Thanks.
    Thanks for your help.

  5. #20
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Thumbs down Re: Abstract Factory Design Patterns Advice

    What do you think about this:

    Code:
    struct AbstractAccount
    {
       virtual ~AbstractAccount();
    }
    
    struct AdminAccount : AbstractAccount
    {
    }
    
    struct HRAccount : AbstractAccount
    {
    }
    
    struct StaffAccount : AbstractAccount
    {
    }
    
    
    struct AbstractAccountFactory
    {
       virtual ~AbstractAccountFactory();
    
      // corrected
       virtual AbstractAccount* create_account() = 0;
    };
    
    struct AdminAccountFactory : AbstractAccountFactory
    {
       AbstractAccount* create_account()
       {
          return new AdminAccount();
       }
    };
    
    struct StaffAccountFactory : AbstractAccountFactory
    {
       AbstractAccount* create_account()
       {
          return new StaffAccount();
       }
    };
    
    struct HRAccountFactory : AbstractAccountFactory
    {
       AbstractAccount* create_account()
       {
          return new HRAccount();
       }
    };
    
    // Scenario 1: strAccount is a std::string
    AbstractAccount* create_account( const std::string& strAccount )
    {
       AbstractAccountFactory *Factory = 0;
       if( false == strAccout.empty() )
       {
          switch( strAccount[0] )
          {
             case 'H' : Factory = new HRAccountFactory();
                            break;
    
             case 'A' : Factory = new AdminAccountFactory();
                            break;
    
            case 'S' : Factory = new StaffAccountFactory();
                           break;
          }
       }
       if( Factory )
       {
          return Factory->create_account();
          delete Factory;
       }
    }
    
    Scenario 2: usertypeName is a class
    
    template<typename UserType>
    AbstractAccount* create_account();
    
    template<>
    AbstractAccount* create_account<AdminType >()
    {
       AdminAccountFactory Factory;
       
       return Factory.create_account();
    
       // even simpler:
       return new AdminAccount();
    }
    
    template<>
    AbstractAccount* create_account<HRType >()
    {
       HRAccountFactory Factory;
       
       return Factory.create_account();
    
       // even simpler:
       return new HRAccount();
    }
    
    template<>
    AbstractAccount* create_account<StaffType >()
    {
       StaffAccountFactory Factory;
       
       return Factory.create_account();
    
       // even simpler:
       return new StaffAccount();
    }
    Last edited by GNiewerth; June 26th, 2009 at 04:37 PM. Reason: AbstractFactory::create_account made pure virtual
    - Guido

  6. #21
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Abstract Factory Design Patterns Advice

    GNiewerth's code example is presumably only meant to illustrate the idea, but I note that AbstractAccountFactory::create_account() was not declared pure virtual, probably by accident, and this omission would affect the idea to be illustrated. (AbstractAccount also is not actually an abstract base class, but that can be pardoned, methinks )

    Also, if you do want to convert the example to something compilable, perhaps this would be a better way of implementing the create_account free function:
    Code:
    // Scenario 1: strAccount is a std::string
    AbstractAccount* create_account(const std::string& strAccount)
    {
        std::auto_ptr<AbstractAccountFactory> Factory;
    
        if (!strAccount.empty())
        {
            switch (strAccount[0])
            {
            case 'H':
                Factory.reset(new HRAccountFactory());
                break;
            case 'A':
                Factory.reset(new AdminAccountFactory());
                break;
            case 'S':
                Factory.reset(new StaffAccountFactory());
                break;
            }
        }
    
        return Factory.get() ? Factory->create_account() : 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #22
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Abstract Factory Design Patterns Advice

    My last question is does AbstractAccount corresponds to my usernameType. If there is no abstractProduct, What is the return type of createInstance() in factory class.

    Does my situation suitable for abstract factory ?

    Thanks.
    Last edited by Peter_APIIT; June 26th, 2009 at 07:08 PM.
    Thanks for your help.

  8. #23
    Join Date
    Nov 2008
    Location
    OK, US
    Posts
    63

    Re: Abstract Factory Design Patterns Advice

    Here you go (see file attached).

    Objects are only created when that user type login. You can add more user types without touching the base factory class. Just copy 2 files (like Admin.h and Admin.cpp), then rename the files, replace "Admin" with something else in the content, add to the solution, compile and voila.

    Let me know if this is what you want.
    Attached Files Attached Files
    Last edited by zerocool2k; June 27th, 2009 at 02:31 AM.

  9. #24
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Abstract Factory Design Patterns Advice

    Thanks, i have coded this myself with two two design patterns which are abstract factory and factory method. Although, the solution is unnecessary bu learn two DP.

    Thanks.
    Thanks for your help.

  10. #25
    Join Date
    Nov 2008
    Location
    OK, US
    Posts
    63

    Re: Abstract Factory Design Patterns Advice

    I just notice the destructors in my example were never called. Anyone knows why?

    I can explicitly call "delete ob" in main() and should not have "delete pAdmin" in its destructor. But how to make this more hidden? (not having to delete in main()? ) Why weren't the destructors called when the program terminates?
    Last edited by zerocool2k; June 27th, 2009 at 04:48 PM.

  11. #26
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Abstract Factory Design Patterns Advice

    My abstract factory and factory method pattern has memory leak problem too. I don't know where to delete it. In main or destructor.

    Please help us.
    Thanks for your help.

  12. #27
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Abstract Factory Design Patterns Advice

    Quote Originally Posted by zerocool2k
    I just notice the destructors in my example were never called. Anyone knows why?
    You implement your classes to use the RAII idiom, but then you directly use pointers to dynamically allocated objects, thus you did not actually use the RAII idiom.

    Quote Originally Posted by zerocool2k
    I can explicitly call "delete ob" in main() and should not have "delete pAdmin" in its destructor. But how to make this more hidden? (not having to delete in main()? ) Why weren't the destructors called when the program terminates?
    Use smart pointers. std::tr1::shared_ptr is probably applicable here.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #28
    Join Date
    Nov 2008
    Location
    OK, US
    Posts
    63

    Re: Abstract Factory Design Patterns Advice

    Thanks. I guess I need to separate the base class and factory so that I can create object and utilize the RAII idiom.

  14. #29
    Join Date
    Jun 2009
    Posts
    1

    Re: Abstract Factory Design Patterns Advice

    Have a look at this tutorial on Abstract Factory Patter specifically for C++:

    http://sourceforge.net/project/downl...pdf&a=93175800

    The relevant source code can be downloaded from here :

    http://sourceforge.net/project/showf...ease_id=686238

  15. #30
    Join Date
    Nov 2008
    Location
    OK, US
    Posts
    63

    Re: Abstract Factory Design Patterns Advice

    Here's a cleaner version from my Example above. Add more user types or Singleton if you want.
    Attached Files Attached Files

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured