I have a class that reads an XML and
according to its contents, it creates a
List<Letter>, where Letter is an abstract
class that is implemented in Alpha and
Beta classes.
I use a foreach statement consisting of a
switch upon which i create an instance of
e.g. Alpha an add it to the list.
The only limitation is that if i ever
invent a new implementation, say Gamma,
i'll be forced to rewrite, recompile and
reinstall the executable.
I'm looking for a suggestion on how to
approach the problem so that a new type
will be handled appropriately (provided
that i distribute SOME definition of the
appropriate actions to be taken).
--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond. 6 1662
You could have an app.config file
<classes>
<add key="Alpha" value="MyNameSp ace.MyClass, MyAssemblyName"/>
<add key="Beta" value="MyNameSp ace.MyClass2, MyAssemblyName"/>
<add key="Gamma" value="MyNameSp ace.MyClass3, NameOfMyNewAsse mbly"/>
</classes>
You can read these in and add them to a Dictionary<stri ng, Type>, then you
can easily create instances based on the name. This way you can drop a new
DLL into the folder + update the app.config and you are all done.
Pete
"K Viltersten" <tm**@vilterste n.comwrote in message
news:6i******** ****@mid.indivi dual.net...
>I have a class that reads an XML and
according to its contents, it creates a
List<Letter>, where Letter is an abstract
class that is implemented in Alpha and
Beta classes.
I use a foreach statement consisting of a
switch upon which i create an instance of
e.g. Alpha an add it to the list.
The only limitation is that if i ever
invent a new implementation, say Gamma,
i'll be forced to rewrite, recompile and
reinstall the executable.
I'm looking for a suggestion on how to
approach the problem so that a new type
will be handled appropriately (provided
that i distribute SOME definition of the
appropriate actions to be taken).
Sounds like a classic case for the abstract factory pattern. For instance,
let's say that you decide on the class to use depending on the name of the
XML element that you encounter. Then, you abstract away the logic of
creating a letter of specific type from an XML node as a delegate (or an
interface), and introduce a registry (could be one, could be several) for
known implementations of that interface/delegate.
delegate Letter LetterFactory(X Element element);
Dictionary<XNam e, LetterFactoryFa ctories;
List<LetterPars eLetters(XDocum ent document)
{
XElement current;
...
var factory = Factories[current.Name];
var letter = factory(current );
...
}
...
Alpha AlphaLetterFact ory(XElement element)
{
Alpha result = ...; // create and fill from XML
return result;
}
Factories.Add(" alpha", AlphaLetterFact ory);
...
Beta BetaLetterFacto ry(XElement element)
{
Beta result = ...; // create and fill from XML
return result;
}
Factories.Add(" beta", BetaLetterFacto ry);
...
XDocument doc = ...;
ParseLetters(do c);
>>I have a class that reads an XML and
>according to its contents, it creates a List<Letter> , where Letter is an abstract class that is implemented in Alpha and Beta classes.
I use a foreach statement consisting of a switch upon which i create an instance of e.g. Alpha an add it to the list.
The only limitation is that if i ever invent a new implementation, say Gamma, i'll be forced to rewrite, recompile and reinstall the executable.
I'm looking for a suggestion on how to approach the problem so that a new type will be handled appropriately (provided that i distribute SOME definition of the appropriate actions to be taken).
Sounds like a classic case for the abstract factory pattern. For instance,
let's say that you decide on the class to use depending on the name of the
XML element that you encounter. Then, you abstract away the logic of
creating a letter of specific type from an XML node as a delegate (or an
interface), and introduce a registry (could be one, could be several) for
known implementations of that interface/delegate.
delegate Letter LetterFactory(X Element element);
Dictionary<XNam e, LetterFactoryFa ctories;
List<LetterPars eLetters(XDocum ent document)
{
XElement current;
...
var factory = Factories[current.Name];
var letter = factory(current );
...
}
...
Alpha AlphaLetterFact ory(XElement element)
{
Alpha result = ...; // create and fill from XML
return result;
}
Factories.Add(" alpha", AlphaLetterFact ory);
...
Beta BetaLetterFacto ry(XElement element)
{
Beta result = ...; // create and fill from XML
return result;
}
Factories.Add(" beta", BetaLetterFacto ry);
...
XDocument doc = ...;
ParseLetters(do c);
Oh, i think i see the approach. Only one
question, regarding the var keyword. Is
it allowed even in DotNet 2.0? I fear
that it's a newer thing, like 3.0 or 3.5
and such great utilities i can't use. :(
--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Only one
question, regarding the var keyword. Is
it allowed even in DotNet 2.0?
Yes, but it needs C# 3.0 (the language and the .NET library version are
separate). In this case it probably just saves a little typing. The only
*essential* use of var is with anonymous types, which are a C# 3.0 feature.
Marc
K Viltersten <tm**@vilterste n.comwrote:
<snip>
Oh, i think i see the approach. Only one
question, regarding the var keyword. Is
it allowed even in DotNet 2.0? I fear
that it's a newer thing, like 3.0 or 3.5
and such great utilities i can't use. :(
"var" is part of C# 3.0, but you can still use it when targeting .NET
2.0 from VS2008. However, it's unnecessary anyway - unless you're using
anonymous types (another part of C# 3.0) you can always just use the
real name instead of "var". It's just the compiler inferring the type
for you.
--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
This approach wont solve the problem you described, in particular
========
The only limitation is that if i ever
invent a new implementation, say Gamma,
i'll be forced to rewrite, recompile and
reinstall the executable.
======== This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Samuel Kleiner |
last post by:
Is there a builtin way of making making another instance of your own
class? I really expected type(self)(*args, **keywords) to work this way.
Currently i'm doing this:
def new(self,*args,**keywords):
from new import instance
s=instance(self.__class__)
if hasattr(D(),'__init__'):
s.__init__(*args,**keywords)
return s
|
by: Arthur |
last post by:
A bit inspired by the decorator discussions, I'm trying to tackle something
I had been avoiding.
Essentially I am trying to create a non-destructive tranformation of an
instance of a class - is one way of putting it.
The way I am currently conceptualizing a solution, what I need is a method
of the class that returns a new instance of the class.
I'm sure this is not new territory.
|
by: Earl Teigrob |
last post by:
In the exampe below, the constructor of the container class is passed the
string called "foo". Within the container class, I would like to create an
instance(s) of foo and add them to the ArrayList that is returned. (Once I
figure this out I will use reflection on the instance to do other stuff)
Anyway, How to I dyanamically create a new instance of a class from
ClassName string?
Thanks for your Help
|
by: Brett |
last post by:
In a class, I have several Private subs. I declare an instance of the class
such as:
Dim MySelf as new Class1
within a private sub. The motive is to provide access to other subs within
the same class. Is this correct?
It would be nice to declare the MySelf instance in the Class public space
(just under Private Class Class1). That will give this error:
Cannot refer to an instance member of a class from within a shared
|
by: Ernesto Díaz |
last post by:
Hi
I need to instance a class at runtime, at runtime I can get the name in a
string, i would like to know if its possible to instance the class at
runtime with the name in a string.
Thanks for your assistance.
Ernesto Díaz
| |
by: Tony Johansson |
last post by:
Hello!
I have a class definition called MyClass see below.
I create an instance of this class MyClass
I also want this instance to be able to modify the test instance that exist
in this class.
I can make the instance of this class MyClass to be able to access the
instance test in two ways.
|
by: JH |
last post by:
Hi
I found that a type/class are both a subclass and a instance of base
type "object".
It conflicts to my understanding that:
1.) a type/class object is created from class statement
2.) a instance is created by "calling" a class object.
|
by: mkadasi |
last post by:
I want to dynamically create instance of class stored in other application.
I am writing the following code for this purpose:
Here in the AssemblyFunc() I am loading the assembly of TariffClass application which is loaded successfully. In objectCreate() I am creating object of class present in TariffClass application. But I am getting the following error
'Could not load file or assembly 'file:///C:\Inetpub\wwwroot\QuickQuote\TariffClass,...
|
by: rythmic |
last post by:
Hi!
PHP is really nifty in the way you can instantiate classes by using string variables like so:
$str_class_name = 'User';
$instance = new $str_class_name();
That way you can create flows where classes that are used in the same way can use the same code even if you don't know which class will use the code beforehand.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |