• 技术文章 >后端开发 >C#.Net教程

    从0自学C#09--抽象工厂模式实例

    黄舟黄舟2017-02-04 10:52:46原创744
    1.适用环境

    (主要是用在生产制造多种产品,软件有多种皮肤,多种数据库等环境)

    2.模式定义

    抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。抽象工厂模式又称为Kit模式,属于对象创建型模式。

    3.模式结构

    抽象工厂模式包含如下角色:

    为了更清晰地理解工厂方法模式,需要先引入两个概念:

    786.jpg

    4.代码

    流程接口

    interface IFlow
        {
            IRxAlign CreateRxAlign();
    
            IDispensing CreateDispensing();
        }
    
    class FlowA : IFlow
        {        public IRxAlign CreateRxAlign()
            {            return new RxAlignA();
            }        public IDispensing CreateDispensing()
            {            return new DispensingA();
            }
        }
    
    
    class FlowB : IFlow
        {        public IRxAlign CreateRxAlign()
            {            return new RxAlignB();
            }        public IDispensing CreateDispensing()
            {            return new DispensingB();
            }
        }

    耦合接口

    interface IRxAlign
        { 
            event LocationChange LocChange;        
    int[] RoPulse { set; get; }        
    int[] FiPulse { set; get; }        
    int[] RoMaxStep { set; get; }        
    double[] RoRESTarget { set; get; }        
    double[] TxP { set; get; }        
    double[] RESmin { set; get; }        
    double[] RESmax { set; get; }
    
            Coordinate Location { set; get; }        
    void LoadPara(string PN);        
    void GoHome();        
    void Start();        
    void Stop();
        }
    
    class RxAlignA : IRxAlign
        {        private ControlCard card;        
    private QSFP28 product;        
    private double RESmax, RESmin, RES;        
    private double[] TxPower;        
    public RxAlignA()
            {            
    this.RESmax = 1.0;            
    this.RESmin = 0.4;
            }        
    public bool ConfigPara()
            {            
    try
                {                
    this.card = new ControlCard();                
    this.product = new QSFP28();               
     return true;
                }            catch
                {                return false;
                }
            }        public void GoHome()
            {            //QSFP28 go home
            }        public void Start()
            {            //QSFP28 start
            }        public void Stop()
            {
                card = null;
                product = null;
            }
            ...
        }
    
    class RxAlignB : IRxAlign
        {        private ControlCard card;        
     private CFP4 product;        
     private double RESmax, RESmin, RES;        
     private double[] TxPower;        
     public RxAlignB()
            {            this.RESmax = 1.0;            
     this.RESmin = 0.4;
            }        public bool ConfigPara()
            {            try
                {                this.card = new ControlCard();                
     this.product = new CFP4();                
     return true;
                }            catch
                {                return false;
                }
            }        public void GoHome()
            {            //CFP4 go home
            }        public void Start()
            {            //CFP4 start
            }        public void Stop()
            {
                card = null;
                product = null;
            }
            ...
        }

    点胶接口

    interface IDispensing
        {
            //code
        }class DispensingA : IDispensing
        {
            //code
        }class DispensingB : IDispensing
        {
            //code
        }

    主线程调用

    flow = new FlowA();
    align = flow.CreateRxAlign();
    align.LoadPara(this.comboBoxPN.Text);
    
    dispens = flow.CreateDispensing();
    dispens.//code...

    以上就是 从0自学C#09--抽象工厂模式实例的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇: 从0自学C#08--绘制曲线chart控件 下一篇: 从0自学C#10--在线绘制类图

    相关文章推荐

    • c语言中形参的缺省存储类别是什么• C#的多线程机制初探(3)• asp.net core mvc实现文件上传实例• C#2.0 Specification(泛型一)• C# 2.0 Specification (泛型三)

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网