6、抽象工厂模式

6、抽象工厂模式    使用一个对象,生产指定的产品(不会参杂其他的产品的东西)

 

namespace SJ.DesignMode
{
    /// <summary>
    /// 设计模式
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {

            AbstractInstance abstractInstance = new AbstractInstance(new DaiEr());
            abstractInstance.Color();
            abstractInstance.Create();
 
        }
    }

 public abstract class AbstractComputer
    {
        public abstract void Color();

        public abstract void Create();         
    }     

    public class DaiEr : AbstractComputer
    {
        public override void Color() 
        {
            Console.WriteLine("DaiEr Green !");
        }
        public override void Create()
        {
            Console.WriteLine("DaiEr create !");
        }
    }

    public class LianXiang : AbstractComputer
    {
        public override void Color()
        {
            Console.WriteLine("LianXiang Blue !");
        }

        public override void Create()
        {
            Console.WriteLine("LianXiang run !");
        }
    }

    public class AbstractInstance
    {
        AbstractComputer _abstractComputer = null;
        public AbstractInstance(AbstractComputer abstractComputer)
        {
            _abstractComputer = abstractComputer;
        }

        public void Color()
        {
            _abstractComputer.Color();
        }

        public void Create()
        {
            _abstractComputer.Create();
        }
    }
}
 

  

posted @ 2021-12-21 14:18  艾特-天空之海  阅读(29)  评论(0编辑  收藏  举报