"公开];嵌套类或不 [英] "Public" nested classes or not

查看:148
本文介绍了"公开];嵌套类或不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一类应用程序。为了被初始化它在构造函数某些设置。让我们也假设设置的数量这么多,它的吸引力将它们放在一个自己的类。

Suppose I have a class 'Application'. In order to be initialised it takes certain settings in the constructor. Let's also assume that the number of settings is so many that it's compelling to place them in a class of their own.

比较这个场景以下两种实现方式。

Compare the following two implementations of this scenario.

实施1:

class Application 
{
   Application(ApplicationSettings settings) 
   {
       //Do initialisation here
   }
}

class ApplicationSettings 
{
   //Settings related methods and properties here
}

实施2:

class Application 
{
   Application(Application.Settings settings) 
   {
       //Do initialisation here
   }

   class Settings 
   {
      //Settings related methods and properties here
   }
}

对我来说,第二种方法是非常可取的。这是更可读的,因为它非常强调这两个类之间的关系。当我写的代码在任何地方应用实例类,第二种方法是将会是什么样漂亮。

To me, the second approach is very much preferable. It is more readable because it strongly emphasises the relation between the two classes. When I write code to instantiate Application class anywhere, the second approach is going to look prettier.

现在只是想象中的设置类本身又将有一些类似的相关类和类又这样做了。去只有三个这样的水平和类命名失控失控,在非嵌套案。如果你窝,然而,事情仍停留优雅。

Now just imagine the Settings class itself in turn had some similarly "related" class and that class in turn did so too. Go only three such levels and the class naming gets out out of hand in the 'non-nested' case. If you nest, however, things still stay elegant.

尽管如此,我读过的人在计算器上说,只有当他们是不可见的嵌套类是合理的到外界;也就是说,如果它们仅用于内部执行包含类的。常用引异议腹胀包含类的源文件的大小,但局部类是这个问题的完美解决方案。

Despite the above, I've read people saying on StackOverflow that nested classes are justified only if they're not visible to the outside world; that is if they are used only for the internal implementation of the containing class. The commonly cited objection is bloating the size of containing class's source file, but partial classes is the perfect solution for that problem.

我的问题是,为什么我们警惕的公开曝光使用嵌套类?有没有对这种使用的任何其他参数?

My question is, why are we wary of the "publicly exposed" use of nested classes? Are there any other arguments against such use?

推荐答案

我觉得这很好。这基本上是生成器模式,并使用嵌套类工作得很好。它也让建设者访问外部类的私有成员,这是非常有用的。例如,你可以在其中呼吁对外部类的私人构造函数取建设者的一个实例建设者的build方法:

I think it's fine. This is basically the builder pattern, and using nested classes works pretty well. It also lets the builder access private members of the outer class, which can be very useful. For instance, you can have a Build method on the builder which calls a private constructor on the outer class which takes an instance of the builder:

public class Outer
{
    private Outer(Builder builder)
    {
        // Copy stuff
    }

    public class Builder
    {
        public Outer Build()
        {
            return new Outer(this);
        }
    }
}

这确保了的建筑外部类的一个实例的方法是通过生成器。

That ensures that the only way of building an instance of the outer class is via the builder.

我在Protocol Buffers的我的C#端口使用模式非常喜欢这个

I use a pattern very much like this in my C# port of Protocol Buffers.

这篇关于"公开];嵌套类或不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆