Exemple #1
0
        static void Main(string[] args)
        {
            try
            {
                //{
                //    Apple apple = new Apple();
                //    Vivo vivo = new Vivo();
                //    IPersonService service = new PersonService();//new IPersonService();

                //    service.Study();
                //    service.PlayApple(apple);
                //    service.Play(apple);
                //    service.Play(vivo);
                //}
                {
                    AbstractPhone  apple   = Factory.GetAbstractPhone(PhoneType.Apple);  //new Apple();
                    AbstractPhone  vivo    = Factory.GetAbstractPhone(PhoneType.Vivo);   // new Vivo();
                    AbstractPhone  luima   = Factory.GetAbstractPhone(PhoneType.Luima);  // new Vivo();
                    IPersonService service = Factory.GetPersonService("IPersonService"); //new PersonService();//new IPersonService();

                    service.Study();
                    service.Play(apple);
                    service.Play(vivo);
                    service.Play(luima);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
Exemple #2
0
        public static AbstractPhone GetAbstractPhone(PhoneType phoneType)
        {
            string        dllName  = System.Configuration.ConfigurationManager.AppSettings[phoneType.ToString()];
            Assembly      assembly = Assembly.Load(dllName.Split(',')[1]);
            Type          type     = assembly.GetType(dllName.Split(',')[0]);
            AbstractPhone phone    = (AbstractPhone)Activator.CreateInstance(type);

            return(phone);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            try
            {
                //Iphone phone = new Iphone();
                //Lumia lumia = new Lumia();

                //AbstractPhone phone = new Iphone();
                //AbstractPhone  lumia = new Lumia();
                {
                    // StudentService studentService = new StudentService();//依赖
                    IStudentService studentService = SimpleFactory.CreateService();
                    studentService.Study();
                    //    studentService.PlayPhone(phone);
                }
                {
                    //  IStudentService service = new StudentService(); // 依赖 左边换成抽象

                    //{
                    // IStudentService service = new StudentService(); // 依赖 左边换成抽象
                    //    // 每增加一个phone都要添加不同的play方法
                    //    service.PlayPhone(phone);
                    //    // service.PlayLumia(phone);
                    //    service.PlayLumia(lumia);
                    //}

                    {
                        // 1.加接口没有用
                        // 2. 不方便看代码 细节还是在依赖,依赖 StudentService
                        IStudentService service = SimpleFactory.CreateService(); // 依赖 左边换成抽象
                        AbstractPhone   phone   = SimpleFactory.CreatePhone();
                        service.Play(phone);
                        //   service.Play(lumia);
                        service.Study();
                    }

                    {
                        // 让别人来做, 第三方 封装
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Exemple #4
0
        // 依赖倒置写法
        static void DIP()
        {
            {   //依赖于BLL,DAL引用
                //IStudentService service = new StudentService();
                //service.Study();

                //AbstractPhone redmi = new Redmi();
                //service.PlayAbstractPhone(redmi);

                //AbstractPhone vivo = new Vivo();
                //service.PlayAbstractPhone(vivo);
            }
            {   //使用反射
                IStudentService service = SimpleFactory.CreateStudenService();
                service.Study();
                AbstractPhone phone = SimpleFactory.CreateAbstractPhone();
                service.PlayAbstractPhone(phone);
            }
            {    //使用反射
                IPhone phone = SimpleFactory.CreatePhone();
                phone.Call();
            }
        }
Exemple #5
0
        /// <summary>
        /// 依赖细节--只能是iPhone或者其他子类
        /// 不同的手机需要多个方法
        /// 有没有办法,一个方法满足不同类型手机的需求?
        /// </summary>
        /// <param name="phone"></param>
        //public void PlayPhone(iPhone phone)
        //{
        //    Console.WriteLine("Use {0}", phone.GetType().Name);
        //    phone.Call();
        //    phone.Text();
        //}
        //public void PlayPhone(HTC phone)
        //{
        //    Console.WriteLine("Use {0}", phone.GetType().Name);
        //    phone.Call();
        //    phone.Text();
        //}

        /// <summary>
        /// 依赖抽象--只要是AbstractPhone极其子类都可以
        /// </summary>
        /// <param name="phone"></param>
        public void PlayPhone(AbstractPhone phone)
        {
            Console.WriteLine("Use {0}", phone.GetType().Name);
            phone.Call();
            phone.Text();
        }
Exemple #6
0
 public void PlayAbstract(AbstractPhone phone)
 {
     Console.WriteLine($"Use{phone.GetType().Name}");
     phone.Call();
     phone.SendMessage();
 }
 public void Play(AbstractPhone phone)
 {
     Console.WriteLine("AbstractPhone Play - {0} Update", phone);
     phone.System();
     phone.Call();
 }
        ///// <summary>
        ///// 依赖细节,导致底层的变化传递到上层
        ///// </summary>
        ///// <param name="phone"></param>

        //public void PlayPhone(Iphone phone)
        //{
        //    Console.WriteLine($" {nameof(phone)}");
        //    phone.Call();
        //    phone.Text();
        //}
        //public void PlayLumia(Lumia phone)
        //{
        //    Console.WriteLine($" {nameof(phone)}");
        //    phone.Call();
        //    phone.Text();
        //}


        // 更具扩展性,
        public void Play(AbstractPhone phone)
        {
            Console.WriteLine($" {nameof(phone)}");
            phone.Call();
            phone.Text();
        }
Exemple #9
0
 public DecoratorPhonePlay(AbstractPhone phone)
 {
     _phone = phone;
 }