模板方法,就是通过抽像类(abstract)将复用部份抽离出来、差异部份,通过(abstract、virtual)来规范起来。

这样做的好处是。

1、复用性

2、耦合性降低。

试想一下,有一个流程是80%重合度的,20%差异化的。这个时候,你要用到的就是模板方法。

例如:机场的安检系统、

1、所有人都检查机票

2、所有人都必须检查身份证(在警方室)

3、所有人都要检查包裹

4、男性要去男性检查室,女性要去女性检查室

5、男性发放男性救生衣、女性发放女性救生衣

6、发放安全手册

这个时候我们就要用到模板方法了。来做这个事情

例2:

银行账户分为活期、定期、保密账户。

代码:

    public abstract class AccountBase
    {
        public void Query(string Name, string Password)
        {
            if (Check(Name, Password))
            {
                if (!IsSecurity(Name))
                {
                    Show(Name: Name, Balance: GetAccountBalance(Name), Interest: GetAccountInterest(Name));
                }
                else
                {
                    Console.WriteLine("安全账户不能显示!");
                }
            }

        }


        /// <summary>
        /// 这里定义了一个钩子方法。通过重写的方式,允许子类改变父类的行为
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        protected virtual bool IsSecurity(string Name)
        {
            return false;
        }
        protected double GetAccountBalance(string Name)
        {
            return 51286789;
        }

        protected abstract double GetAccountInterest(string Name);

        protected virtual void Show(string Name, double Balance, double Interest)
        {
            Console.WriteLine("账户:{0},余额:{1},利息:{2}", Name, Balance, Interest);
        }

        protected bool Check(string Name, string Password)
        {
            return Name.Equals("admin") && Password.Equals("123456");
        }

    }
    public class AccountCurrent : AccountBase
    {
        protected override double GetAccountInterest(string Name)
        {
            return 51286789 * 0.001;
        }
        protected override void Show(string Name, double Balance, double Interest)
        {
            Console.WriteLine("这里是活期账户");
            base.Show(Name, Balance, Interest);
        }
    }
    public class AccountFixed : AccountBase
    {
        protected override double GetAccountInterest(string Name)
        {
            return 51286789 * 0.0015;
        }

        protected override void Show(string Name, double Balance, double Interest)
        {
            Console.WriteLine("这里是定期账户");
            base.Show(Name, Balance, Interest);
        }
    }
    public class AccountSecurity : AccountBase
    {
        protected override double GetAccountInterest(string Name)
        {
            throw new NotImplementedException();
        }


        protected override void Show(string Name, double Balance, double Interest)
        {
            Console.WriteLine("这里是保密账户");
            base.Show(Name, Balance, Interest);
        }

        /// <summary>
        /// 重写这个方法,限制账户显示
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        protected override bool IsSecurity(string Name)
        {
            return true;
        }
    }
        static void Main(string[] args)
        {

            AccountBase accountFixed = new AccountFixed();
            accountFixed.Query("admin", "123456");

            AccountBase accountCurrent = new AccountCurrent();
            accountCurrent.Query("admin", "123456");

            AccountBase accountSecurity = new AccountSecurity();
            accountSecurity.Query("admin", "123456");

            Console.ReadLine();

        }

运行结果:

 

posted on 2016-05-25 09:38  梦回过去  阅读(209)  评论(0编辑  收藏  举报