By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,275 Members | 947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,275 IT Pros & Developers. It's quick & easy.

Factory Class Question

P: n/a
Is there a way to prevent classes from being instantiated by methods other
than my factory pattern?

I have a couple of classes and want to force the factory class to be used as
the "entry point".

Any help is appreciated,

Jason MacKenzie
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Jason,
The "easiest" way is to make the constructor private which will only allow
the class itself to create an instance.

This unfortunately will not prevent creating an instance via reflection &
Activator.CreateInstance... Using Activator.CreateInstance to create an
instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm**********************@formet.com> wrote in message
news:e$**************@tk2msftngp13.phx.gbl...
Is there a way to prevent classes from being instantiated by methods other
than my factory pattern?

I have a couple of classes and want to force the factory class to be used
as the "entry point".

Any help is appreciated,

Jason MacKenzie

Nov 21 '05 #2

P: n/a
Jason,
The "easiest" way is to make the constructor private which will only allow
the class itself to create an instance.

This unfortunately will not prevent creating an instance via reflection &
Activator.CreateInstance... Using Activator.CreateInstance to create an
instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm**********************@formet.com> wrote in message
news:e$**************@tk2msftngp13.phx.gbl...
Is there a way to prevent classes from being instantiated by methods other
than my factory pattern?

I have a couple of classes and want to force the factory class to be used
as the "entry point".

Any help is appreciated,

Jason MacKenzie

Nov 21 '05 #3

P: n/a
Jay - thanks for the response.

Here is what I have right now:

I have a base class called Employee

I have 2 derived classes based on Employee called Facilty1_Employee and
Facilty2_Employee

I have a factory class that has a function that returns Employee and will
instatiate Facility1 or 2 depending on a setting in the registry.

Facility2_Employee has an overloaded constructor which means I can't make it
private (as far as I know).

Facilty1_employee takes one argument in its constructor as well which means
I can't make that private.

Should I be setting properties instead of using passing arguments to the
constructors to accomplish what I'm trying to do?

Thanks a million,

Jason MacKenzie
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
Jason,
The "easiest" way is to make the constructor private which will only allow
the class itself to create an instance.

This unfortunately will not prevent creating an instance via reflection &
Activator.CreateInstance... Using Activator.CreateInstance to create an
instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm**********************@formet.com> wrote in message
news:e$**************@tk2msftngp13.phx.gbl...
Is there a way to prevent classes from being instantiated by methods
other than my factory pattern?

I have a couple of classes and want to force the factory class to be used
as the "entry point".

Any help is appreciated,

Jason MacKenzie


Nov 21 '05 #4

P: n/a
Jason,
Seeing as you have parameterized constructors, the "easiest" way may be to
move EmployeeFactory, Employee, Facilty1Employee, and Facilty2Employee all
to their own class library, then make the constructors of Employee,
Facilty1Employee, and Facilty2Employee as Friend. Which means that only that
assembly will be able to instantiate the classes. I would make
EmployeeFactory a not inheritable class with a private constructor, which
prevents others from inheriting from it or creating an instance of it.

I normally do not create both a EmployeeFactory class and Employee class,
instead I put the factory method as a shared member of Empoyee.

Hope this helps
Jay

"Jason MacKenzie" <jm**********************@formet.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Jay - thanks for the response.

Here is what I have right now:

I have a base class called Employee

I have 2 derived classes based on Employee called Facilty1_Employee and
Facilty2_Employee

I have a factory class that has a function that returns Employee and will
instatiate Facility1 or 2 depending on a setting in the registry.

Facility2_Employee has an overloaded constructor which means I can't make
it private (as far as I know).

Facilty1_employee takes one argument in its constructor as well which
means I can't make that private.

Should I be setting properties instead of using passing arguments to the
constructors to accomplish what I'm trying to do?

Thanks a million,

Jason MacKenzie
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
Jason,
The "easiest" way is to make the constructor private which will only
allow the class itself to create an instance.

This unfortunately will not prevent creating an instance via reflection &
Activator.CreateInstance... Using Activator.CreateInstance to create an
instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm**********************@formet.com> wrote in message
news:e$**************@tk2msftngp13.phx.gbl...
Is there a way to prevent classes from being instantiated by methods
other than my factory pattern?

I have a couple of classes and want to force the factory class to be
used as the "entry point".

Any help is appreciated,

Jason MacKenzie



Nov 21 '05 #5

P: n/a
Jay - thanks for the response.

Here is what I have right now:

I have a base class called Employee

I have 2 derived classes based on Employee called Facilty1_Employee and
Facilty2_Employee

I have a factory class that has a function that returns Employee and will
instatiate Facility1 or 2 depending on a setting in the registry.

Facility2_Employee has an overloaded constructor which means I can't make it
private (as far as I know).

Facilty1_employee takes one argument in its constructor as well which means
I can't make that private.

Should I be setting properties instead of using passing arguments to the
constructors to accomplish what I'm trying to do?

Thanks a million,

Jason MacKenzie
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
Jason,
The "easiest" way is to make the constructor private which will only allow
the class itself to create an instance.

This unfortunately will not prevent creating an instance via reflection &
Activator.CreateInstance... Using Activator.CreateInstance to create an
instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm**********************@formet.com> wrote in message
news:e$**************@tk2msftngp13.phx.gbl...
Is there a way to prevent classes from being instantiated by methods
other than my factory pattern?

I have a couple of classes and want to force the factory class to be used
as the "entry point".

Any help is appreciated,

Jason MacKenzie


Nov 21 '05 #6

P: n/a
Jason,
Seeing as you have parameterized constructors, the "easiest" way may be to
move EmployeeFactory, Employee, Facilty1Employee, and Facilty2Employee all
to their own class library, then make the constructors of Employee,
Facilty1Employee, and Facilty2Employee as Friend. Which means that only that
assembly will be able to instantiate the classes. I would make
EmployeeFactory a not inheritable class with a private constructor, which
prevents others from inheriting from it or creating an instance of it.

I normally do not create both a EmployeeFactory class and Employee class,
instead I put the factory method as a shared member of Empoyee.

Hope this helps
Jay

"Jason MacKenzie" <jm**********************@formet.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Jay - thanks for the response.

Here is what I have right now:

I have a base class called Employee

I have 2 derived classes based on Employee called Facilty1_Employee and
Facilty2_Employee

I have a factory class that has a function that returns Employee and will
instatiate Facility1 or 2 depending on a setting in the registry.

Facility2_Employee has an overloaded constructor which means I can't make
it private (as far as I know).

Facilty1_employee takes one argument in its constructor as well which
means I can't make that private.

Should I be setting properties instead of using passing arguments to the
constructors to accomplish what I'm trying to do?

Thanks a million,

Jason MacKenzie
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
Jason,
The "easiest" way is to make the constructor private which will only
allow the class itself to create an instance.

This unfortunately will not prevent creating an instance via reflection &
Activator.CreateInstance... Using Activator.CreateInstance to create an
instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm**********************@formet.com> wrote in message
news:e$**************@tk2msftngp13.phx.gbl...
Is there a way to prevent classes from being instantiated by methods
other than my factory pattern?

I have a couple of classes and want to force the factory class to be
used as the "entry point".

Any help is appreciated,

Jason MacKenzie



Nov 21 '05 #7

P: n/a
That helps out immensely. Thank you very much.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O6**************@TK2MSFTNGP15.phx.gbl...
Jason,
Seeing as you have parameterized constructors, the "easiest" way may be to
move EmployeeFactory, Employee, Facilty1Employee, and Facilty2Employee all
to their own class library, then make the constructors of Employee,
Facilty1Employee, and Facilty2Employee as Friend. Which means that only
that assembly will be able to instantiate the classes. I would make
EmployeeFactory a not inheritable class with a private constructor, which
prevents others from inheriting from it or creating an instance of it.

I normally do not create both a EmployeeFactory class and Employee class,
instead I put the factory method as a shared member of Empoyee.

Hope this helps
Jay

"Jason MacKenzie" <jm**********************@formet.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Jay - thanks for the response.

Here is what I have right now:

I have a base class called Employee

I have 2 derived classes based on Employee called Facilty1_Employee and
Facilty2_Employee

I have a factory class that has a function that returns Employee and will
instatiate Facility1 or 2 depending on a setting in the registry.

Facility2_Employee has an overloaded constructor which means I can't make
it private (as far as I know).

Facilty1_employee takes one argument in its constructor as well which
means I can't make that private.

Should I be setting properties instead of using passing arguments to the
constructors to accomplish what I'm trying to do?

Thanks a million,

Jason MacKenzie
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
Jason,
The "easiest" way is to make the constructor private which will only
allow the class itself to create an instance.

This unfortunately will not prevent creating an instance via reflection
& Activator.CreateInstance... Using Activator.CreateInstance to create
an instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm**********************@formet.com> wrote in message
news:e$**************@tk2msftngp13.phx.gbl...
Is there a way to prevent classes from being instantiated by methods
other than my factory pattern?

I have a couple of classes and want to force the factory class to be
used as the "entry point".

Any help is appreciated,

Jason MacKenzie



Nov 21 '05 #8

P: n/a
That helps out immensely. Thank you very much.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O6**************@TK2MSFTNGP15.phx.gbl...
Jason,
Seeing as you have parameterized constructors, the "easiest" way may be to
move EmployeeFactory, Employee, Facilty1Employee, and Facilty2Employee all
to their own class library, then make the constructors of Employee,
Facilty1Employee, and Facilty2Employee as Friend. Which means that only
that assembly will be able to instantiate the classes. I would make
EmployeeFactory a not inheritable class with a private constructor, which
prevents others from inheriting from it or creating an instance of it.

I normally do not create both a EmployeeFactory class and Employee class,
instead I put the factory method as a shared member of Empoyee.

Hope this helps
Jay

"Jason MacKenzie" <jm**********************@formet.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Jay - thanks for the response.

Here is what I have right now:

I have a base class called Employee

I have 2 derived classes based on Employee called Facilty1_Employee and
Facilty2_Employee

I have a factory class that has a function that returns Employee and will
instatiate Facility1 or 2 depending on a setting in the registry.

Facility2_Employee has an overloaded constructor which means I can't make
it private (as far as I know).

Facilty1_employee takes one argument in its constructor as well which
means I can't make that private.

Should I be setting properties instead of using passing arguments to the
constructors to accomplish what I'm trying to do?

Thanks a million,

Jason MacKenzie
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
Jason,
The "easiest" way is to make the constructor private which will only
allow the class itself to create an instance.

This unfortunately will not prevent creating an instance via reflection
& Activator.CreateInstance... Using Activator.CreateInstance to create
an instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm**********************@formet.com> wrote in message
news:e$**************@tk2msftngp13.phx.gbl...
Is there a way to prevent classes from being instantiated by methods
other than my factory pattern?

I have a couple of classes and want to force the factory class to be
used as the "entry point".

Any help is appreciated,

Jason MacKenzie



Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.