Is it right strategy for control two similar instrument?

L

lotus

Hi all.

I want to control two different instrtument but have simliar
functionality.
Acually the fisrt one is controlled by using serial communication, and
the other is controlled by LAN communication.
The number of instruments will be extetned by 5 or more.
So If user select instrument (ex. by selection of combo box items), I
must do right procedure of contolling proper instrument.

I want to contol these instrument by same command. so I select
following strategy by using INTERFACE keyword of C#.

Is it right strategy? Is there more powerful method?

---lotus----

In the following code, Console.WriteLine is acaully substitution of
sending command to instrument.
-----------Code start-------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int id = 5;
machine src;
instrument1 ins1 = new instrument1(ref id);
instrument2 ins2 = new instrument2(ref id);

Console.WriteLine("Select instrument 1 or 2");
string input = Console.ReadLine();
if (input =="1")
{
src = ins1;
src.turn_on();
}
else if (input == "2")
{
src = ins2;
src.turn_on();
}
}
}
public interface machine
{
void turn_on();
}
public class instrument1 : machine
{
int id = 3;
public instrument1(ref int id_)
{
id = id_;
Console.WriteLine(" instrument 1 is initilized");
}
void machine.turn_on()
{
Console.WriteLine("turn on");
}
}
public class instrument2 : machine
{
int id = 5;
public instrument2(ref int id_)
{
id = id_;
Console.WriteLine(" instrument 2 is initilized");
}
void machine.turn_on()
{
Console.WriteLine("power on");
Console.WriteLine("I need more step -- plevel 1");
}
}
}

------------------ end of code ----------------
 
G

Guest

As an alternative, I would create a base class and inherit from it. This
would allow you to share common functionality:

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int id = 5;
machine src;
instrument1 ins1 = new instrument1(ref id);
instrument2 ins2 = new instrument2(ref id);

Console.WriteLine("Select instrument 1 or 2");
string input = Console.ReadLine();
if (input =="1")
{
src = ins1;
src.turn_on();
}
else if (input == "2")
{
src = ins2;
src.turn_on();
}
}
}
public class machine
{
private int id;

public machine(ref int _id)
{
id = _id;
}
abstract void turn_on();
}
public class instrument1 : machine
{
public instrument1(ref int id_) : base(id_)
{
Console.WriteLine(" instrument 1 is initilized");
}
public override void turn_on()
{
Console.WriteLine("turn on");
}
}
public class instrument2 : machine
{
public instrument2(ref int id_) : base(id_)
{
id = id_;
Console.WriteLine(" instrument 2 is initilized");
}
public override void turn_on()
{
Console.WriteLine("power on");
Console.WriteLine("I need more step -- plevel 1");
}
}
}

Now you can instantiate objects as either instrument1 or instrument2 but
refer to them as type machine.

--
Jeffrey Hornby
Hornby Consulting, Inc.



lotus said:
Hi all.

I want to control two different instrtument but have simliar
functionality.
Acually the fisrt one is controlled by using serial communication, and
the other is controlled by LAN communication.
The number of instruments will be extetned by 5 or more.
So If user select instrument (ex. by selection of combo box items), I
must do right procedure of contolling proper instrument.

I want to contol these instrument by same command. so I select
following strategy by using INTERFACE keyword of C#.

Is it right strategy? Is there more powerful method?

---lotus----

In the following code, Console.WriteLine is acaully substitution of
sending command to instrument.
-----------Code start-------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int id = 5;
machine src;
instrument1 ins1 = new instrument1(ref id);
instrument2 ins2 = new instrument2(ref id);

Console.WriteLine("Select instrument 1 or 2");
string input = Console.ReadLine();
if (input =="1")
{
src = ins1;
src.turn_on();
}
else if (input == "2")
{
src = ins2;
src.turn_on();
}
}
}
public interface machine
{
void turn_on();
}
public class instrument1 : machine
{
int id = 3;
public instrument1(ref int id_)
{
id = id_;
Console.WriteLine(" instrument 1 is initilized");
}
void machine.turn_on()
{
Console.WriteLine("turn on");
}
}
public class instrument2 : machine
{
int id = 5;
public instrument2(ref int id_)
{
id = id_;
Console.WriteLine(" instrument 2 is initilized");
}
void machine.turn_on()
{
Console.WriteLine("power on");
Console.WriteLine("I need more step -- plevel 1");
}
}
}

------------------ end of code ----------------
 
N

Nick Hounsome

The use of an interface is good but since the ID stuff is common you should
probably have an abstract base class as well or instead just to hold that.

There is no reason to use "ref" anywhere.

Use a Dictionary<string,machine> to look up the machine from the id rather
than a switch [then it is just dict[input].turn_on()].
 
L

lotus

Thank you, Jeffrey and Nick

Actually, I didn't know about abstract and generics.
But from your recommendation. I could know about power of that.

Generic functionality of .Net 2.0 is really fantastic to me.

-lotus-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top