473,545 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2364
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******** *************@w 1g2000hsg.googl egroups.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
2346
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 IEnumerable<int> Visit(SomeVisitor visitor) { int position = 0; for(int i = 0; i < Count; i++) {
2
1475
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 return_type; virtual return_type visit(param *) = 0;
12
3020
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 itself. The called class has special functions for each class that can call it. With the visitor pattern, the calling class can have new...
17
2589
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 visitor pattern. So we place an accept method in the the base class glyph and procede to create the visitor hierarchy. The accept signature will...
0
1903
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 { public: virtual void visit(Object_type_1 *); virtual void visit(Object_type_2 *)
1
6501
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 week's article we're going to talk a bit about when and why to apply certain patterns. We'll start with the Visitor pattern. The pattern is also...
0
11604
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 class functions. Applications using polymorphism typically have functions with base class pointers or references as arguments. Then derived objects are...
3
1499
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 business object. Please clarify. Regards, Design Pattern Learner
3
2298
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) which I cannot modify. I decided to use the visitor design pattern depending on the actual type of the classes because those classes already support the...
0
7496
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, 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...
0
7428
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...
0
7941
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...
0
7784
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...
0
6014
agi2029
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...
0
5071
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...
1
1916
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
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
738
bsmnconsultancy
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...

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.