Example #1
0
        static void Main(string[] args)
        {
            try
            {
                //{
                //    AbstractStudent student = new StudentVIPInherit()
                //    {
                //        Id = 1234,
                //        Name = "Ivan"
                //    };
                //    student.Study();
                //}
                //{
                //    AbstractStudent student = new StudentVIP()
                //    {
                //        Id = 1234,
                //        Name = "Ivan"
                //    };
                //    // 比起继承,组合跟家灵活 可以为多个类型服务,但是这个调用成本更高一点
                //    // 因为即关注了 AbstractStudent 类, 又关注了StudentCombination
                //    StudentCombination studentCombination = new StudentCombination(student);
                //    studentCombination.Study();
                //}

                {
                    AbstractStudent student = new StudentFree()
                    {
                        Id   = 1234,
                        Name = "Ivan"
                    };

                    // BaseStudentDecorator baseStudentDecorator = new BaseStudentDecorator(student); // 1.

                    // AbstractStudent baseStudentDecorator = new BaseStudentDecorator(student); //2.
                    student = new BaseStudentDecorator(student);

                    student = new StudentHomeworkDecorator(student);

                    student = new StudentCommentDecorator(student);
                    // 不修改业务类, 可以随意添加功能 装饰器
                    // 还可以随意调整顺序
                    student.Study();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            AbstractStudent student = new StudentFree()
            {
                Id   = 001,
                Name = "小张"
            };

            //student.Study();


            //AbstractStudent studentPreviewDecorator = new StudentPreviewDecorator(student);
            //studentPreviewDecorator.Study();

            student = new StudentPreviewDecorator(student);
            student = new StudentRegDecorator(student);
            student = new StudentPayDecorator(student);
            student = new StudentHomeWorkDecorator(student);
            student.Study();


            Console.Read();
        }