2010.05.21 常用的设计模式

来源:互联网 发布:阿里云centos安全设置 编辑:程序博客网 时间:2024/05/19 19:30

using System;
using System.Collections.Generic;
using System.Text;

namespace Singletion
{
    /// <summary>
    /// 简单工厂模式
    /// 将对象的产生与使用相分离
    /// 利用了多态,高内聚低耦合
    /// 简单工厂的三要素:1.工厂,2.父类产品,3.若干个子类产品
    /// </summary>
    /// 工厂(要素一)
    class SimpleFactory
    {
        /// <summary>
        /// 父类产品(要素二)
        /// </summary>
        /// <returns></returns>
        public SuperFactory CreateProduct(int type)
        {
            //父类产品(可指向若干个子类产品)
            SuperFactory sup = null;
            //选择若干个子类产品
            switch(type)
            {
                case 1: sup = new SubFactory1(); break;
                case 2: sup = new SubFactory2(); break;
            }
            return sup;
        }
    }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Singletion
{
    public class SuperFactory
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public virtual void Mothed()
        {
            Console.WriteLine("父类工厂产生父类产品的方法");
        }
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

1.namespace Singletion
{
    class SubFactory1:SuperFactory
    {
        public override void Mothed()
        {
            Console.WriteLine("子类工厂一产生子类产品的行为");
        }
    }
}

 

 

 

2.namespace Singletion
{
    class SubFactory2:SuperFactory
    {
        public override void Mothed()
        {
            Console.WriteLine("子类工厂二产生子类产品的行为");
        }
    }
}

原创粉丝点击