473,395 Members | 1,668 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.

About Polymorphism

Dear Everybody,

In the following codesnippet, rceceiver gets different
requests of which sender knows interfaces.
I want to do diffent job based on concrete class in
receiver.

How to implement this pattern?

Thanks,
// ----- Code -----------------------
using System;

namespace TestConsole
{
class Apps
{
[STAThread]
static void Main(string[] args)
{
Receiver receiver = new Receiver();
Sender sender = new Sender(receiver);

RequestSquare requestSquare = new RequestSquare
();
requestSquare.Op1 = 10;
sender.Send(requestSquare);

RequestTimes requestTimes = new RequestTimes();
requestTimes.Op1 = 30;
requestTimes.Times = 3;

sender.Send(requestSquare);
sender.Send(requestTimes);
}
}

public class Sender
{
private IReceiver _Receiver = null;
public Sender(IReceiver receiver)
{
_Receiver = receiver;
}

public void Send(IRequest request)
{
_Receiver.Receive(request);
}
}

public class Receiver : IReceiver
{
public void Receive(IRequest request)
{
if (request is RequestSquare)
{
Receive((RequestSquare)request);
return;
}
if (request is RequestTimes)
{
Receive((RequestTimes)request);
return;
}
}

public void Receive(RequestSquare request)
{
Console.WriteLine(request.Op1*request.Op1);
}

public void Receive(RequestTimes request)
{
Console.WriteLine(request.Op1*request.Times);
}

}

public interface IReceiver
{
void Receive(IRequest request);
}

public class RequestSquare : IRequest
{
private int _Op1;
public int Op1
{
get { return _Op1;}
set {_Op1 = value;}
}
}

public class RequestTimes : IRequest
{
public int Times;
private int _Op1;
public int Op1
{
get { return _Op1;}
set {_Op1 = value;}
}
}

public interface IRequest
{
int Op1
{
get; set;
}
}
}

Nov 16 '05 #1
0 814

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

Similar topics

7
by: rashkatsa | last post by:
Hi all ! I have written a little module called 'polymorph' that allows to call different methods of a class that have the same name but that have not the same number of arguments. *args are...
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
10
by: Danny Ni | last post by:
Hi, I was asked a question in a recent interview, where in ASP.Net is polymorphism used? I got differrent answers from different people, One said ToString() method because it is working for...
35
by: JKop | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en- us/vccore98/HTML/_core_using_strict_type_checking.asp Pay particular attention to: The types WPARAM, LPARAM, LRESULT, and void *...
3
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
11
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
8
by: Locia | last post by:
What are the techniques used to implement parametric polymorphism in C++? A Polymorphic method can be virtual?
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
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.