Bladeren bron

Add hardware factory as a sigleton

HaoShuang 5 jaren geleden
bovenliggende
commit
5e2c0686c1
3 gewijzigde bestanden met toevoegingen van 47 en 7 verwijderingen
  1. 9 1
      HOZProject/FormUnitControl.cs
  2. 37 5
      SmartSEMControl/HardwareInterface.cs
  3. 1 1
      SmartSEMControl/SmartSEM.cs

+ 9 - 1
HOZProject/FormUnitControl.cs

@@ -21,6 +21,9 @@ namespace HOZProject
 {
     public partial class FormUnitControl : Form
     {
+        //全局只有一个fatorySEM
+        FactoryHardware factorySEM = FactoryHardware.Instance; 
+
         #region 系统参数
         private String path = Directory.GetCurrentDirectory();
         private SmartSEM sem = new SmartSEM();
@@ -29,7 +32,8 @@ namespace HOZProject
         #region 构造函数
         public FormUnitControl()
         {
-            InitializeComponent();
+            InitializeComponent();     
+            
 
             //Calling Notification for updated status
             //CZEMApi.Notify += new _EMApiEvents_NotifyEventHandler(CZEMApi_Notify);
@@ -187,6 +191,10 @@ namespace HOZProject
         #region 缩放Get
         private void btnenlargeGet_Click(object sender, EventArgs e)
         {
+            ISEMControl iSEM = factorySEM.ISEM;
+            iSEM.GetMagnification();
+
+
             float ret = sem.GetMagnification();
             if (float.IsNaN(ret))
             {

+ 37 - 5
SmartSEMControl/HardwareInterface.cs

@@ -6,11 +6,45 @@ using System.Threading.Tasks;
 
 namespace SmartSEMControl
 {
+    //sealed, 避免继承出新类造成重构
+    public sealed class FactoryHardware
+    {
+        //只读的静态成员
+        private static readonly FactoryHardware instance = new FactoryHardware();
+
+        // Explicit static constructor to tell C# compiler
+        // not to mark type as beforefieldinit
+        //C#的静态构造函数只有在当其类的实例被创建或者有静态成员被引用时执行,
+        //在整个应用程序域中只会被执行一次。
+        static FactoryHardware()
+        {
+        }
+
+        private FactoryHardware()
+        {
+        }
+
+        //使用这个实例
+        public static FactoryHardware Instance
+        {
+            get
+            {
+                return instance;
+            }
+        }
+
+        //其他使用的成员变量
+        private ISEMControl m_iSEM = new SmartSEM(); //成员变量
+
+        public ISEMControl ISEM
+        {
+            get { return m_iSEM; }
+        } //属性,只能当前类创建 
+    
+    }
     //SEM控制
-    interface SEMControl
+    public interface ISEMControl
     {
-
-
         //缩放
         float GetMagnification();
         Boolean SetMagnification(float set);
@@ -77,8 +111,6 @@ namespace SmartSEMControl
         //清除控件
         Boolean Dispose();
 
-
-
     }
 
     interface FIBControl

+ 1 - 1
SmartSEMControl/SmartSEM.cs

@@ -10,7 +10,7 @@ using System.Threading;
 
 namespace SmartSEMControl
 {
-    public class SmartSEM : SEMControl
+    public class SmartSEM : ISEMControl
     {
         #region 模拟参数
         //放大缩小