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

visitor pattern

Hi,

Given this class definition,
public class Node
{
Node parent;
object current;
ArrayList children;

}
I would need to type check the object variable everytime I'd need to
use it. Like shown below;

void SomeMethod(Node node)
{
object obj = node.current;
if (obj is SomeType1)
{
// code
}
else if (obj is SomeType2)
{
// code
}
else if (obj is SomeType3)
{
// code
}

}
Is such a type check at runtime a bad thing in c#? Would using a
visitor patterns' double dispatch be a more elegant solution? This
would lead to changing the original Node to;

public class Node
{
Node parent;
baseObject current; // change from object to base class in apps'
object model
ArrayList children;

}

and adding the neccessary interfaces etc. Suppose it would make the
Node class less usable for other purposes - i,e, by binding it to a
specific object model.

Apr 14 '07 #1
1 2346
void SomeMethod(Node node)
{
object obj = node.current;
if (obj is SomeType1)
{
// code
}
else if (obj is SomeType2)
{
// code
}
else if (obj is SomeType3)
{
// code
}
This is definitely a bad idea, because it is not extensible. Fourthermore,
since the type is object, the object could be any type, and there are
thousands of types, so you would have to either implement a case for each
type, or deal with exceptions. Some version of the Visitor pattern might be
your best solution. At least this way, the implementation of the Visitor
handles the operation itself. Of course, without knowing more about what
"SomeMethod" does, I couldn't say for sure. It is possible that you could do
something a bit less complex by simply creating an interface or base
abstract class to use for the "current" property, and calling a method that
must be implemented in that class for each type concerned. However, using
the Visitor design pattern, or any legitimate design pattern, has its
advantages, as the pattern is well-known.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"RedLars" <Li***********@gmail.comwrote in message
news:11*********************@w1g2000hsg.googlegrou ps.com...
Hi,

Given this class definition,
public class Node
{
Node parent;
object current;
ArrayList children;

}
I would need to type check the object variable everytime I'd need to
use it. Like shown below;

void SomeMethod(Node node)
{
object obj = node.current;
if (obj is SomeType1)
{
// code
}
else if (obj is SomeType2)
{
// code
}
else if (obj is SomeType3)
{
// code
}

}
Is such a type check at runtime a bad thing in c#? Would using a
visitor patterns' double dispatch be a more elegant solution? This
would lead to changing the original Node to;

public class Node
{
Node parent;
baseObject current; // change from object to base class in apps'
object model
ArrayList children;

}

and adding the neccessary interfaces etc. Suppose it would make the
Node class less usable for other purposes - i,e, by binding it to a
specific object model.

Apr 16 '07 #2

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

Similar topics

2
by: Wavemaker | last post by:
I've been playing with C# v2.0 today, and having quite a bit of fun. The new version has added iterators. The iterators are coded directly into the class to be iterated. For example: public...
2
by: Imre Palik | last post by:
Hi, I am trying to create a framework, that automatically generates a base class for the visitor pattern: template <typename param, typename ret = void> struct visitor { typedef ret...
12
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of...
17
by: Merlin | last post by:
Probably there is no right or wrong answer to this but I thought to ask to put my mind at rest. Ok lets say you have a object hierarchy (eg. the Glyph in Lexi from GOF book) and you want to use the...
0
by: Siphiuel | last post by:
Hi everyone. When using visitor pattern, we have a nasty dependence on the types of visitable objects that is coded way on top on the visitor hierarchy. i mean, like this: class AbstractVisitor...
1
by: JosAH | last post by:
Greetings, this week we let go of all that algebraic stuff and concentrate a bit more on what object oriented programming is all about. Java claims to support OO, so why not use it? In this...
0
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns: Visitor Introduction Polymorphism requires a class hierarchy where the interface to the hierarchy is in the base class. Virtual functions allow derived classes to override base...
3
by: C# | last post by:
Dear Freinds, I was going through Stratergy and Visitor patterns. I am really confused. Both look same passing the logic object in to the a business object and the logic object works on the...
3
by: aaragon | last post by:
Hello everyone, I've been trying to work with the visitor design pattern, and it works fine except for the following. Let's suppose that we have a fixed hierarchy of classes (many of them)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.