473,698 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1882
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.Creat eInstance... Using Activator.Creat eInstance to create an
instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm************ **********@form et.com> wrote in message
news:e$******** ******@tk2msftn gp13.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.Creat eInstance... Using Activator.Creat eInstance to create an
instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm************ **********@form et.com> wrote in message
news:e$******** ******@tk2msftn gp13.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_Employ ee and
Facilty2_Employ ee

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_Emplo yee has an overloaded constructor which means I can't make it
private (as far as I know).

Facilty1_employ ee 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******** ******@TK2MSFTN GP10.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.Creat eInstance... Using Activator.Creat eInstance to create an
instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm************ **********@form et.com> wrote in message
news:e$******** ******@tk2msftn gp13.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, Facilty1Employe e, and Facilty2Employe e all
to their own class library, then make the constructors of Employee,
Facilty1Employe e, and Facilty2Employe e 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************ **********@form et.com> wrote in message
news:%2******** ********@tk2msf tngp13.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_Employ ee and
Facilty2_Employ ee

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_Emplo yee has an overloaded constructor which means I can't make
it private (as far as I know).

Facilty1_employ ee 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******** ******@TK2MSFTN GP10.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.Creat eInstance... Using Activator.Creat eInstance to create an
instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm************ **********@form et.com> wrote in message
news:e$******** ******@tk2msftn gp13.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_Employ ee and
Facilty2_Employ ee

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_Emplo yee has an overloaded constructor which means I can't make it
private (as far as I know).

Facilty1_employ ee 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******** ******@TK2MSFTN GP10.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.Creat eInstance... Using Activator.Creat eInstance to create an
instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm************ **********@form et.com> wrote in message
news:e$******** ******@tk2msftn gp13.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, Facilty1Employe e, and Facilty2Employe e all
to their own class library, then make the constructors of Employee,
Facilty1Employe e, and Facilty2Employe e 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************ **********@form et.com> wrote in message
news:%2******** ********@tk2msf tngp13.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_Employ ee and
Facilty2_Employ ee

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_Emplo yee has an overloaded constructor which means I can't make
it private (as far as I know).

Facilty1_employ ee 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******** ******@TK2MSFTN GP10.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.Creat eInstance... Using Activator.Creat eInstance to create an
instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm************ **********@form et.com> wrote in message
news:e$******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP15.phx.gbl...
Jason,
Seeing as you have parameterized constructors, the "easiest" way may be to
move EmployeeFactory , Employee, Facilty1Employe e, and Facilty2Employe e all
to their own class library, then make the constructors of Employee,
Facilty1Employe e, and Facilty2Employe e 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************ **********@form et.com> wrote in message
news:%2******** ********@tk2msf tngp13.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_Employ ee and
Facilty2_Employ ee

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_Emplo yee has an overloaded constructor which means I can't make
it private (as far as I know).

Facilty1_employ ee 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******** ******@TK2MSFTN GP10.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.Creat eInstance... Using Activator.Creat eInstance to create
an instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm************ **********@form et.com> wrote in message
news:e$******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP15.phx.gbl...
Jason,
Seeing as you have parameterized constructors, the "easiest" way may be to
move EmployeeFactory , Employee, Facilty1Employe e, and Facilty2Employe e all
to their own class library, then make the constructors of Employee,
Facilty1Employe e, and Facilty2Employe e 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************ **********@form et.com> wrote in message
news:%2******** ********@tk2msf tngp13.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_Employ ee and
Facilty2_Employ ee

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_Emplo yee has an overloaded constructor which means I can't make
it private (as far as I know).

Facilty1_employ ee 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******** ******@TK2MSFTN GP10.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.Creat eInstance... Using Activator.Creat eInstance to create
an instance of a class with a private constructor is rare, however it is
possible.

Hope this helps
Jay

"Jason MacKenzie" <jm************ **********@form et.com> wrote in message
news:e$******** ******@tk2msftn gp13.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
6640
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;
4
8232
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? Let's start with your basic Singleton class: class Singleton {
3
1582
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 to use illustrative examples. I have two libraries for distribution; one is an essential library and the other is an add-on (providing specialized extensions and
8
254
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 factory? What about security? I'm assuming that the factory should enforce security. Has anyone seen examples of this? Thanks, Craig Buchanan
6
1598
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
4574
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 constructors (because we do not allow developers to instantiate them directly). Because our business objects are instantiated very frequently, the idea of using reflection sounds like a performance killer (I haven't done any tests on this, but the...
5
1692
by: ma740988 | last post by:
Consider: #include "handyfactory.h" #include <iostream> struct Shape { virtual void print() const=0; };
6
1316
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 in this situation but I am having trouble getting access to properties at the presentation level. So my situation is I have a Vehicle base class. Public Class Vehicle Property BodyType as String End Class
1
3603
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 to a function of a ùregistry vlass that hold all sub-classes... /* ------Neuron.h--------*/ #include <math.h> namespace Mnetlib { class Neuron
5
1363
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 would refactor the code and make it a seperate class and use the class in each factory then the code look look and more DRY. My question is what is the right way to do this in Factory Pattern. Refactoring them into a seperate class is a solution?...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
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
9032
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...
1
8905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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
6532
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
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3053
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
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.