什么时候使用枚举,类或标签?

编程入门 行业动态 更新时间:2024-10-14 20:27:45
本文介绍了什么时候使用枚举,类或标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设您有一个特定页面类型的页面(例如普通页面,帐户页面等).该页面由Page对象表示.

Imagine you have a page that is of a specific page type (for example normal page, account page etc). The page is represented by a Page object.

我的问题是,您如何为页面分配页面类型?

My question is, how would you assign a page type to a page?

我看到以下选项:

  • 通过使用在Page对象中设置的PageType枚举.
  • 使用PageType类,并在Page对象中分配其实例.
  • 使用页面标记,这些标记是与Page对象关联的简单字符串.
  • 选项1是仅代码方式,因此添加新的页面类型意味着更改(核心)代码. 选项2更灵活,但是必须维护这些页面类型会产生额外的开销. 选项3非常灵活,因为不需要维护,并且可以扩展到页面类型以外的其他机制.但是您可以轻松地破坏事物,因为没有约束.

    Option 1 is code-only approach, so adding new page types means changing (core) code. Option 2 is more flexible, but there is additional overhead in having to maintain these page types. Option 3 is very flexible, because there's no maintenance and it's extendable to other mechanisms than page type. But you can easily break things because there are no constraints.

    选择一个而不是另一个的客观原因是什么?

    What would be other objective reasons to choose one over the other?

    推荐答案

    关于系统设计和页面要求,要支持的不同页面类型数以及页面本身之间的差异,缺少详细信息,因此很难做出这样的决定.在这种情况下,我建议保持简单,并使用枚举.

    There is a lack of details regarding a system design and pages requirements, number of different page types to support and difference between pages itself, so hard to make such decision. In such case I would recommend to keep it simple and use Enum.

    • 如果页面具有自己的特定业务逻辑,则可以坚持使用每种Page类型的类,但是只有在确实存在特定于页面的逻辑时才仔细进行操作.
    • 请考虑一下UserControl/CustomControl功能(假设您使用的是ASP.NET),这样就可以按一组由页面功能负责的控件来拆分页面,因此,您将保持单一责任原则,并建立较少耦合的系统.
    • 某些逻辑应该是extracted从Page实体本身到外部帮助器/工厂/存储库,然后注入到Page类中.
    • If pages has own specific business logic you can stick with class per Page type, but do it carefully only when really page-specific logic there.
    • Think about UserControl/CustomControl feature (assuming you are usign ASP.NET) so you can split page by a set of controls which is responsible on the part of Page functionality, so in this way you'll keep Single Responsibility Principle and build less coupled system.
    • Some logic should be extracted from Page entities itself into external helpers/factories/repositories and then injected in Page class.

    关于目标, 您应该定义潜在的页面数量以及应该提供的灵活性.还请记住诸如系统的可扩展性和维护之类的事情.

    Regarding objectives, you should define how many pages could be in place potentially and which level of flexibility should be provided. Also keep in mind such things like extensibility and maintenance of a system.

    更多推荐

    什么时候使用枚举,类或标签?

    本文发布于:2023-11-07 00:03:44,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1565016.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:什么时候   标签

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!