zoukankan      html  css  js  c++  java
  • 代码重构之提升方法

    核心:如果多个继承类都会使用到同一个方法,则该方法就应该提升到基类里,而不是在子类中单独写。【方法可以根据需要提升到基类或是接口中】

        提高了代码的重用性(一个函数,多处使用), 如果需求有改,只需要修改一处即可,方便维护,代码结构也更加清晰。

        若是继承者(A)和被继承者(B)之间的关系是:

          A是B:就考虑使用抽象类。 【经理是管理者】

          A拥有B这样的能力:考虑使用接口,强调功能的扩展。【小鸟可以飞,飞机也可以飞】

    【类中的字段重构同理】

    代码演示:

    1、方法提升前

      1-1、基类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace PullUpMethod
    {
        public abstract class Phone
        {
            public void Call() {
                Console.WriteLine("打电话");
            }
        }
    }
    View Code

      1-2、子类一

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace PullUpMethod
    {
        public class IPhone:Phone
        {
            public void ShakeDeleteEditMsg() {
                Console.WriteLine("摇一摇删除编辑的内容~~");
            }
        }
    }
    View Code

      1-3、子类二

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace PullUpMethod
    {
        public class Galaxy:Phone
        {
            public void SendMsg()
            {
                Console.WriteLine("发送消息");
            }
        }
    }
    View Code

      1-4、客户端

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    /// <summary>
    /// 提升方法
    /// </summary>
    namespace PullUpMethod
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                //需求V1:苹果手机向外暴露摇一摇删除编辑内容 和 打电话接口,
                //     盖世手机向外暴露发送短信 和 打电话的接口
                Console.WriteLine("====================需求V1写法=================");
                Console.WriteLine("=============IPhone===============");
                IPhone iPhone = new IPhone();
                iPhone.Call(); //所有手机都有的
                iPhone.ShakeDeleteEditMsg(); //苹果特有的~ 
                Console.WriteLine("=============Galaxy===============");
                Galaxy galaxy = new Galaxy();
                galaxy.Call(); //所有手机都有的
                galaxy.SendMsg(); // 所有手机都有的
    
                Console.ReadKey();
    
            }
        }
    }
    View Code

    2、方法提升后

      2-1、基类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace PullUpMethod
    {
        public abstract class Phone
        {
            //所有手机都有打电话和发送消息的功能
            public void Call()
            {
                Console.WriteLine("打电话");
            }
            //将Galaxy中发送消息的函数提升到基类中来了,因为所有的手机都可以发送短信。而不是只有Galaxy才可以发送信息
            public void SendMsg()
            {
                Console.WriteLine("发送消息");
            }
        }
    }
    View Code

      2-2、子类一

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace PullUpMethod
    {
        public class IPhone:Phone
        {
            public void ShakeDeleteEditMsg() {
                Console.WriteLine("摇一摇删除编辑的内容~~");
            }
        }
    }
    View Code

      2-3、子类二

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace PullUpMethod
    {
        public class Galaxy:Phone
        {
        }
    }
    View Code

      2-4、客户端

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    /// <summary>
    /// 提升方法
    /// </summary>
    namespace PullUpMethod
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                //需求V1:苹果手机向外暴露摇一摇删除编辑内容 和 打电话接口,
                //     盖世手机向外暴露发送短信 和 打电话的接口
                Console.WriteLine("====================需求V1写法=================");
                Console.WriteLine("=============IPhone===============");
                IPhone iPhone = new IPhone();
                iPhone.Call(); //所有手机都有的
                iPhone.ShakeDeleteEditMsg(); //苹果特有的~ 
                Console.WriteLine("=============Galaxy===============");
                Galaxy galaxy = new Galaxy();
                galaxy.Call(); //所有手机都有的
                galaxy.SendMsg(); // 所有手机都有的
    
                //需求V2:在需求V1的基础上,苹果手机也要向外暴露发送短信的接口
                Console.WriteLine("====================需求V2写法=================");
                Console.WriteLine("=============IPhone===============");
                IPhone iPhoneV2 = new IPhone();
                iPhoneV2.Call();    //所有手机都有的
                iPhoneV2.SendMsg();  //发送信息
                iPhoneV2.ShakeDeleteEditMsg(); //苹果特有的~ 
                Console.WriteLine("=============Galaxy===============");
                Galaxy galaxyV2 = new Galaxy();
                galaxyV2.Call(); //所有手机都有的
                galaxyV2.SendMsg(); // 所有手机都有的
    
                Console.ReadKey();
    
            }
        }
    }
    View Code

    结果展示:

    写写博客,方便自己也方便有需要的人~~

  • 相关阅读:
    现在,为什么连一个 JavaScript 的厌恶者都认为:每个开发人员都应该学习 JavaScript
    .NET 部署_ASP.NET 部署的八大关键实践
    Ext.Net 1.2.0_Ext.Net.DateColumn 日期格式问题
    Ext.Net 1.2.0_Ext.UX.GMapPanel Google Map 插件
    Yahoo Web UIs——Java开发者丰富的Web UI
    单元测试_使用 Nmock 单元测试 .NET 业务对象
    公共语言运行库中的程序集05程序集安全注意事项
    Entity Framework_成功针对多种数据库使用实体框架(EF)
    反射_01概述和反射中的运行时类型以及查看类型信息
    公共语言运行库中的程序集01概述
  • 原文地址:https://www.cnblogs.com/Yisijun/p/13217637.html
Copyright © 2011-2022 走看看