Beispiel #1
0
        public void DipResponsibility()
        {
            StudentTest student = new StudentTest()
            {
                Id   = 1,
                Name = "小鸟班长"
            };
            {
                iPhone phone = new iPhone();
                student.PlayiPhone(phone);
                student.PlayT(phone);
                student.Play(phone);
            }
            {
                Honor phone = new Honor();
                student.PlayiPhone(phone);
                student.PlayT(phone);
                student.Play(phone);
            }

            /*依赖细节 高层就依赖了下层,这种不适合扩展 不推荐
             * //用泛型+父类约束等同于用父类参数类型
             * //一个方法满足了不同的扩展需求
             * //面向抽象后,不能使用子类的特别内容,比如下面的小米手机的 手环Bracelet方法
             *
             * 面向抽象不止一个类型,用的就是通用的功能,非通用的,那就不要面向抽象
             * 面向对象语言开发,就是类与类之间进行交互,如果高层直接依赖低层的细节,细节多变的,
             * 低层的修改会直接水波效应传递到高层最上层,一点细微的改变就会导致整个系统的从下往上的修改。非常不稳定
             *
             * 面向抽象,高层不直接依赖低层,而是依赖抽象,抽象一般相对稳定,低层细节的扩展就不会影响高层,这样就能支撑层内部的横向扩展,不会影响其他地方,这样的程序架构就稳定
             *
             * 依赖倒置(理论基础)--IOC控制反转(实践封装)---DI依赖注入(实现IOC的手段)
             */
            {
                Mi phone = new Mi();

                student.PlayT(phone);
                student.Play(phone);
            }
        }
Beispiel #2
0
 public void PlayiPhone(Honor phone)
 {
     Console.WriteLine($"这里是{phone}");
     phone.Call();
     phone.SendMsg();
 }