Csharp: Simple Factory Pattern

  /// <summary>
    /// Summary description for Namer.
    /// Base class for getting split names
    /// Simple Factory Patterns
    /// geovindu,Geovin Du,涂聚文 edit
    /// </summary>
    
    public class Namer
    {
        /// <summary>
        /// parts stored here
        /// </summary>
        protected string frName, lName;

        /// <summary>
        /// return first name
        /// </summary>
        /// <returns></returns>
        public string getFrname()
        {
            return frName;
        }
        /// <summary>
        /// return last name
        /// </summary>
        /// <returns></returns>
        public string getLname()
        {
            return lName;
        }
    }

  

  /// Summary description for LastFirst.
    /// Simple Factory Patterns
    /// geovindu,Geovin Du,涂聚文 edit
    /// </summary>
    public class LastFirst : Namer
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        public LastFirst(string name)
        {
            int i = name.IndexOf(",");
            if (i > 0)
            {
                lName = name.Substring(0, i);
                frName = name.Substring(i + 1).Trim();
            }
            else
            {
                lName = name;
                frName = "";
            }
        }
    }

  

    /// <summary>
    /// Summary description for FirstFirst.
    /// Simple Factory Patterns
    /// geovindu,Geovin Du,涂聚文 edit
    /// </summary>
    public class FirstFirst : Namer
    {
         
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        public FirstFirst(string name)
        {
            int i = name.IndexOf(" ");
            if (i > 0)
            {
                frName = name.Substring(0, i).Trim();
                lName = name.Substring(i + 1).Trim();
            }
            else
            {
                lName = name;
                frName = "";
            }
        }
    }

  

    /// <summary>
    /// Summary description for NameFactory.
    /// Simple Factory Patterns
    /// geovindu,Geovin Du,涂聚文 edit
    /// </summary>
    public class NameFactory
    {
        public NameFactory() { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Namer getName(string name)
        {
            int i = name.IndexOf(",");
            if (i > 0)
                return new LastFirst(name);
            else
                return new FirstFirst(name);
        }
    }

  

窗體中調用:

 /// <summary>
    /// Simple Factory Patterns
    /// geovindu,Geovin Du,涂聚文 edit
    /// </summary>
    public partial class SimpleFactoryForm : Form
    {

        private NameFactory nameFact;

        /// <summary>
        /// 
        /// </summary>
        public SimpleFactoryForm()
        {
            InitializeComponent();
            nameFact = new NameFactory();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SimpleFactoryForm_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Namer name = nameFact.getName(txtbName.Text);
            txtbFirst.Text = name.getFrname();
            txtbLast.Text = name.getLname();
        }
    }

  

 

 

posted @ 2022-09-17 22:52  ®Geovin Du Dream Park™  阅读(13)  评论(0编辑  收藏  举报