CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Making a strong factory/prohibiting programmer use of NEW keyword

    Hi guys..

    I have a simple structure:

    ObjParent is the base class from which my children inherit. I want a factory class that manufactures appropriate children and i dont want the programmer making his own children..

    Dim x as ObjParent = FactoryClass.Manufacture("some influencing string")


    Manufacture returns one of a number of child types ObjChildA, ObjChildB etc. I dont want the programmer to be able to make a New ObjParent or any New ObjChildX (where X varies)


    how do i do this? I tried making the factory and the Parent/Children in the same namespace and making the constructors protected, but it doesnt work this way?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  2. #2
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Making a strong factory/prohibiting programmer use of NEW keyword

    Try this (I did not try this myself):
    Set the constructor of the instansiated objects to be Friend.
    Only classes in the same assembly will be able to instanciate those objects.

  3. #3
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: Making a strong factory/prohibiting programmer use of NEW keyword

    Can't you just set the New() sub to private instead of public?

    Private Sub New()

    That way you can't call new from outside the class itself, but has to go through your factory methods.

  4. #4
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Making a strong factory/prohibiting programmer use of NEW keyword

    If you set the constructor to be private, then the class itself must be the factory of itself (like in Singleton pattern - the Instance static property).
    You will not be able to instanciate the object in another factory class.
    That is why I suggested to use Friend modifier for the constructor - only classes from the same assembly can call the constructor.

  5. #5
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Making a strong factory/prohibiting programmer use of NEW keyword

    If you set the constructor to be private, then the class must be the factory of itself (like in Singleton pattern - the Instance static property).
    You will not be able to instanciate the object in another factory class.
    That is why I suggested to use Friend modifier for the constructor - only classes from the same assembly can call the constructor.

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