473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Factory Class Question

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
8 1859
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
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....
4
by: Eric | last post by:
Perhaps this question has been posed before (I'd be surprised if it hasn't) but I just gotta know... Is it possible to combine the Singleton and Factory Method design patterns in the same class?...
3
by: Paramesh | last post by:
Hello friends, My friend asked me this question: This question regards proprietary software (of which I am one of the developers), so I cannot post actual code for this question. I will try...
8
by: Craig Buchanan | last post by:
I've seen design patterns for class factories that work well to create (fetch) objects, but I haven't seen anything about how to persist the class' data when it has changed. Is this done thru the...
6
by: Dave | last post by:
Hello all, Please see my question embedded in comment form below. Thanks, Dave #include <iostream> #include <boost/shared_ptr.hpp>
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
5
by: ma740988 | last post by:
Consider: #include "handyfactory.h" #include <iostream> struct Shape { virtual void print() const=0; };
6
by: GarrettD78 | last post by:
Accidently posted this to the wrong group so I am reposting. This is probably a newbie question but I am a little confused about how to go next with my code. I think I want to use a factory pattern...
1
by: neoairus | last post by:
I'm developing a pseudo-library for neural network. To simplify librarary using i wont to implement a sistem to instantiate different implementation of interface(Layer.h Neuron.h) passing a string...
5
by: CSharper | last post by:
I have created a Factory pattern code. One thing I noticed after coding, each factory has some methods they are exactly same and doing the same code using the values specific to each factory, if I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.