473,395 Members | 1,652 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,395 software developers and data experts.

Creating objects polymorphically

Hi all

Let's say I am creating a model to represent classes and properties. In
addition to this I need instances of classes and values for those
properties.

KEY:
[ClassName]
(AssociationEndName)
A classifier has many properties
[Classifier] (Classifier) 1-----* (Properties) [Property]

A classifier has many instances
[Classifier] (Classifier) 1-----* (Instances) [Instance]

An instance has many property values
[Instance] (Instance) 1----* (PropertyValues) [PropertyValue]

A Property has many PropertyValues
[Property] (Property) 1----* (PropertyValues) [PropertyValue]
I hope that's clear :-)

var personClassifier = new Classifier();
var firstNameProperty = new Property();
personClassifier.Properties.Add(firstNameProperty) ;

At this point I want to do something like this

var personInstance = Instance.FromClassifier(personClassifier);

the implementation would look something like this

public static Instance FromClassifier(Classifier classifier)
{
var result = new Instance();
result.Classifier = classifier;

foreach (var property in classifier.Properties)
{
var propertyValue = new PropertyValue();
result.PropertyValues.Add(propertyValue);
propertyValue.Property = property;
}
}

The above is a simple example. The fact is I have a pre-defined set of
property types. So descended from Property I might have

StringProperty, NumericProperty, BooleanProperty, etc

and a corresponding PropertyValue class for each

StringPropertyValue, NumericPropertyValue, BooleanPropertyValue, etc
To achieve the creation of the correct PropertyValue descendant I added an
abstract method to Property

public abstract PropertyValue CreatePropertyValue(Property property);

and implement in each descendant

public override PropertyValue CreatePropertyValue(Property property)
{
var result = new BooleanPropertyValue();
result.StronglyTypedReference = (BooleanProperty)property;
return result;
}
Now onto the question :-)

To me this seems logical, but a little messy. I don't like the fact that
the Property class creates the instance of the PropertyValue. What I would
expect to write in code is something like

var property = BooleanPropertyValue.FromProperty(booleanProperty) ;

but obviously in this case I can't hard-code the class type into my foreach
loop. So it seems I am stuck with the Property being responsible for
creating the correct PropertyValue instances. Or do I have another option?

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 7 '08 #1
3 1458
On Tue, 07 Oct 2008 09:42:39 -0700, Peter Morris
<mr*********@spamgmail.comwrote:
[...]
To me this seems logical, but a little messy. I don't like the fact
that the Property class creates the instance of the PropertyValue. What
I would expect to write in code is something like

var property = BooleanPropertyValue.FromProperty(booleanProperty) ;

but obviously in this case I can't hard-code the class type into my
foreach loop. So it seems I am stuck with the Property being
responsible for creating the correct PropertyValue instances. Or do I
have another option?
I have to admit, the whole way through your explanation of the design, I
found myself thinking "surely this is overcomplicated".

And I still think that. I tried to keep an open mind, but there just seem
to be a lot of extra unnecessary layers. It's possible I just don't
understand the bigger picture enough, but my impression is what it is.

That said, what you're talking about seems pretty much like a classic
factory pattern. That is, put your FromProperty() method in the
PropertyValue base class, rather than each sub-class. Then in that
method, check the type of the passed-in Property instance to determine
what kind of PropertyValue to return.

Of course, _that_ said, I'm not really clear on why you feel it's bad to
have the abstract method in the Property sub-class. The factory approach
is less "polymorphic" than the abstract method approach would be. It does
spread the logic around through each sub-class, but then putting
type-specific implementation into each sub-class is the whole point of
polymorphism. It seems like the solution you've already been using should
be fine.

Pete
Oct 7 '08 #2
Peter Morris wrote:
Hi all

Let's say I am creating a model to represent classes and properties.
In addition to this I need instances of classes and values for those
properties.
Rebuilding the Delphi "class of class" metaclasses? AFAIK, Joanna
Carter once wrote a nice article on this, IOW, how to do this using C#.
--
Rudy Velthuis http://rvelthuis.de

"I'm not under the alkafluence of inkahol that some thinkle
peep I am. It's just the drunker I sit here the longer I get."
-- Unknown
Oct 7 '08 #3
I'm actually building messages from templates. For example

[MessageTemplate] (Template) 1-----* (Properties) [MessageTemplateProperty]

The MessageTemplate might be something like

Name : string;
=========
Message : string;
Discount : decimal;
So I would create 1 MessageTemplate, and
MessageTemplateStringProperty
MessageTemplateNumericalProperty

That's the template defined by some kind of admin, now the users will create
messages based on it...

Name = "10% off handbags"
Message = "This week only. 10% of all marked prices on handbags!!!"
Discount = 10m;

Name = "15% off squirrels"
Message = "This week only. 15% of all marked prices on squirrels!!!"
Discount = 15m;
This information gets packaged up as XML something like this

<messages>
<message id="1" templateID="{GuidOfTheTemplate}">
<values>
<value id="{GuidOfTheTemplateProperty}">
This week only. 10% of all marked prices on handbags!!!"
</value>
<value name="{GuidOfTheTemplateProperty}">
10
</value>
</values>
</message>
<message id="1" templateID="{GuidOfTheTemplate}">
<values>
<value id="{GuidOfTheTemplateProperty}">
This week only. 15% of all marked prices on squirrels!!!"
</value>
<value name="{GuidOfTheTemplateProperty}">
15
</value>
</values>
</message></messages>

It's concocted, but illustrates the point :-)

Having a virtual method on the abstract MessageTemplateProperty does the
trick, but there's just something about it that makes me feel dirty, and not
good dirty :-) I was hoping there was a pattern or something that might
make more sense, otherwise I will just leave it as it is :-)

Thinking about it, I use the service provider pattern throughout my system.
Maybe I should create something like this:

public interface IMessagePropertyFactory
{
MessageProperty CreateMessageProperty(MessageTemplateProperty
templateProperty);
}

I can then get this factory from my service provider, which would mean I can
mock it easily and check that it is being called from a unit test. Or maybe
that is overkill :-)
--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ajit goel | last post by:
Hi; I have a question which respect to initialising and creating objects in loops.Here are the 2 scenarios: 1. ###############################################################################...
2
by: ikl | last post by:
When creating a list of objects of the same class, what should be concerned to decide if using "new" or not? Since how many number of the objects are unknown until runtime, probably it is not a...
0
by: Alexandre | last post by:
hey this is the error im getting : --> Cannot widen from target type to primitive type. this line produces the error : temp = o.GetType().InvokeMember("", BindingFlags.CreateInstance, null,...
1
by: chris | last post by:
I know I've asked this before, but I didn't really get an answer and I bet it's because I didn't explain myself very well. Here goes again. I have this code: Dim arrData(intNoOfRows,...
3
by: Richard Thornley | last post by:
Hello, I need some clarification in creating objects. Consider the following code... (note: The function InitializeListCombo initializes the combobox) Private daLists As New...
5
by: fireball | last post by:
please help newbie I need to create a lot of objects the same type (let's say: schemas) I wish to use paramerized block in loop to do so. - how to put names of my objects to such control-flow? ...
6
by: RSH | last post by:
Hi, i have a situation where I need to dynamically create objects in a loop. My question surrounds intantiation naming in such a scenerio. Below is a snippet that is basically hardcoding each...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
1
by: Bartholomew Simpson | last post by:
I know this is a VB.Net ng. But hopefully, someone remembers some classic (VB6) coding - besides I've not had any response when I posted this to the VB6 specific ng. I wrote a C++ librray that I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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.