日期:2014-05-18 浏览次数:20906 次
public sealed class MachineParameters { // Singleton public static readonly MachineParameters instance = new MachineParameters(); public static MachineParameters GetInstance() { return instance; } // 为了实现类似单例的全局调用效果,因此想到 public Machine M1 = new Machine(1); public Machine M2 = new Machine(2); public Machine M3 = new Machine(3); public Machine M4 = new Machine(4); // 为了便于轮询,将所有 Machine 类实例放入数组 public Machine[] M = new Machine[4]; private MachineParameters() { M[1] = M1; M[2] = M2; M[3] = M3; M[4] = M4; } } public sealed class TimingController { // Singleton private static readonly TimingController instance = new TimingController(); public static TimingController GetInstance() { return instance; } MachineParameters Machines = MachineParameters.GetInstance(); // 方便的全局式调用如下 Machines.M[i].Speed = 100; } public class Machine //实例类 { **(1) TimingController Timing = TimingController.GetInstance(); UpdateData Update = UpdateData.GetInstance(); //省略其余定义 //类中用到了 TimingController 和 UpdateData ,因此声明如上 public Machine(int id) { **(2) Timing.WarmedUp += new TimingEventHandler(Update.UpdateMachine); } } public partial class MainWindow : Form { // Singleton //public static readonly MainWindow instance = new MainWindow(); //public static MainWindow GetInstance() //{ // return instance; //} // 主窗体类,程序从这里开始 ***(3) TimingController Timing = TimingController.GetInstance(); }
static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); **(4) Application.Run(new MainWindow()); } }