using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Start mstart = new Start(0);
Exit mexit = new Exit(1);
Pos pos1 = new Pos(1);
Pos pos2 = new Pos(2);
Pos pos3 = new Pos(3);
CylOFF cyloff = new CylOFF(1);
CylON cylON = new CylON(2);
Founction mfunc = new Founction();
mfunc.command = mstart;
mfunc.work();
mfunc.commandList.Add(mstart);
mfunc.commandList.Add(pos1);
mfunc.commandList.Add(pos2);
mfunc.commandList.Add(pos3);
mfunc.commandList.Add(cyloff);
mfunc.commandList.Add(cylON);
mfunc.commandList.Add(mexit);
mfunc.workList();
Console.ReadKey();
}
}
class Founction
{
public command command;
public List<command> commandList=new List<command>();
public void work()
{
command.Execute( );
}
public void workList()
{
foreach(var mm in commandList)
mm.Execute();
}
}
abstract class command
{
public int id = 0;
public abstract void Execute();
}
class Exit:command
{
public Exit(int i = 0)
{
id = i;
}
public override void Execute()
{
Console.WriteLine("执行退出"+ id);
}
}
class Start : command
{
public Start(int i = 0)
{
id = i;
}
public override void Execute()
{
Console.WriteLine("执行开始"+ id);
}
}
class Pos : command
{
public Pos(int i = 0)
{
id = i;
}
public override void Execute()
{
Console.WriteLine("运动到位置:" + id);
}
}
class CylON : command
{
public CylON(int i = 0)
{
id = i;
}
public override void Execute()
{
Console.WriteLine("气缸打开:" + id);
}
}
class CylOFF : command
{
public CylOFF(int i = 0)
{
id = i;
}
public override void Execute()
{
Console.WriteLine("气缸关闭:" + id);
}
}
}
优点:
将具体命令和调用命令分开,完全解耦
缺点:
系统有过多的具体命令