zoukankan      html  css  js  c++  java
  • 3、公共接口不应该使用太过频繁,当有大量类型实现公共接口时,应当避免通过公共接口调用对象

    using System;
    
    namespace ShouldCode
    {
        public interface IShouldBaseNotInterface
        {
            bool Show();
        }
        public class A1 : IShouldBaseNotInterface
        {
            public bool Show()
            {
                throw new NotImplementedException();
            }
        }
        //A2:IShouldClassNotInterface
        //A3:IShouldClassNotInterface
        //...
        public class A100000 : IShouldBaseNotInterface
        {
            public bool Show()
            {
                throw new NotImplementedException();
            }
        }
        public abstract class Base : IShouldBaseNotInterface
        {
            public bool Show()
            {
           //第一次通过某个接口调用方法时会去查找具体类型,内部会有 Monomorphic Stub 优化。 Console.WriteLine(
    "InterFace.Do 需要查找类型,运行时子类类型太多就会影响程序性能。"); Console.WriteLine("Base.Do 通过基类来减少查找的类型数量"); return true; } } public class ShouldBaseNotInterface : Base { public Base Base { get; set; } = new ShouldBaseNotInterface(); public IShouldBaseNotInterface Interface { get; set; } = new ShouldBaseNotInterface(); public void ShouldClass() { System.Console.WriteLine(Base.Show()); } public void NotInterFace() { System.Console.WriteLine(Interface.Show()); } } }
  • 相关阅读:
    aa
    ECS上搭建Docker(CentOS7)
    mysql时间戳转日期
    rsync用法
    docker安装mysql8
    使用Docker安装mysql,挂载外部配置和数据
    my.cnf
    Centos7通过yum安装jdk8
    maven添加本地包命令mvn install:install-file
    Mysql——查看数据库,表占用磁盘大小
  • 原文地址:https://www.cnblogs.com/zhuwansu/p/11082493.html
Copyright © 2011-2022 走看看