Why strange naming convention of “AlertDialog.Builder” instead of “AlertDialogBuilder” in Android

前端 未结 4 1208
时光取名叫无心
时光取名叫无心 2021-02-08 06:13

Why not

AlertDialogBuilder builder = new  AlertDialogBuilder(this);
builder.setTitle(\"foo\");

instead of

AlertDialog.Builder          


        
4条回答
  •  暖寄归人
    2021-02-08 06:50

    I'll try and clear you up behind this kind of organization.

    First of all, Builder is a class inside the AlertDialog class. Why would you create a class inside another class? I personally only create nested classes if I want to add more functionality to an existing class and don't believe that from a designing point of view that the class should be extended. I'm sure one could argue about this one, since there are tons of different ways to add more functionality to a class; yet nested classes can be preferred above others (for some people).

    Here's oracles point of view on nested classes.

    I'd only use nested classes if I find that it makes the code more maintainable and intuitive.

    In this case I believe the guys over at Google found that instead of creating a new class called AlertDialogBuilder compared to AlertDialog.Builder, is because both yield the same results; a dialog displaying some information on the screen with some buttons. The only difference between both (I think) is that you can set positive, neutral and negative button on the AlertDialog.Builder (which gives more functionality to the class, yet not necessary more that the class should be extended, again personal opinion).

    In the end what matters is that the code works (hopefully without any bugs), is maintainable, readable and intuitive. There's no definite answer to your question, since people have different opinions on this topic.

    I hope this helps.

提交回复
热议问题