枚举 vs 查找表 vs 枚举反射 vs 状态模式

发布于 2024-07-14 15:41:15 字数 794 浏览 5 评论 0原文

我将要构建的软件将涉及很多不同状态之间的“应用程序”切换。 可以完成某些任务取决于应用程序所处的状态。 我正在考虑使用枚举作为状态

public class Application
{
  public int Id {get;set;}
  public Status {get;set;}
}
public enum Status
{
  [Description("New")]New = 1, [Description("Closed")]Closed = 2
}

但后来我想也许在数据库中使用查找表是件好事,因为状态确实经常更新/重新排序

table status (id int pk, desc string, sort_order int)
table application (id int pk, status_id int fk)

在我的情况下,我需要做类似的事情,

if (application.Status == Status.New)
{ //do something }
else if (application.Status == Status.Closed)
{ //do other things }

我认为上面的情况更容易与枚举有关。 然而,当涉及到更新状态排序顺序或描述时,这将是相当困难的。

我应该使用反射根据查找表中的值动态创建枚举吗? 或者我应该使用状态模式? 我看到枚举反射的问题是性能影响。 而且状态模式会产生很多冗余代码。

你怎么认为? 提前致谢!

The software I will be building will involve "application" switching between different status a lot. Certain tasks can be done depends on the status an application is at. I was thinking about using enum as the status

public class Application
{
  public int Id {get;set;}
  public Status {get;set;}
}
public enum Status
{
  [Description("New")]New = 1, [Description("Closed")]Closed = 2
}

But then I thought maybe it’s good to use lookup table in the database as status does get updated/re-ordered quite often

table status (id int pk, desc string, sort_order int)
table application (id int pk, status_id int fk)

In my case I need to do things like

if (application.Status == Status.New)
{ //do something }
else if (application.Status == Status.Closed)
{ //do other things }

I think the above case is easier to do with enum. However when it comes to updating status sort order or description it'll be quite hard.

Should I used reflection to dynamically create enum based on values from lookup table? Or should I use state pattern? The problem I see with enum relfection is performance impact. And state pattern can produce a lot of redundant code.

What do you think? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尛丟丟 2024-07-21 15:41:15

您不应该在代码中到处都进行此检查

if (application.Status == Status.New)
{ //do something }
else if (application.Status == Status.Closed)
{ //do other things }

,而是使用状态模式。 每当应用程序的模式发生变化时就更改状态,并将所有调用转发到状态的方法。 您将拥有更干净且更易于维护的代码。

至于改变状态,与状态模式无关。 所以你可以使用任何一种优雅的方法。

You should not sprinkle your code with this check everywhere

if (application.Status == Status.New)
{ //do something }
else if (application.Status == Status.Closed)
{ //do other things }

Instead, use the state pattern. Change the state whenever the mode of the application changes and forward all your calls to the state's methods. You'll have a much cleaner and easier to maintain code.

As for changing the status, that has got nothing do with the state pattern. So you can use whichever approach is elegant.

流年已逝 2024-07-21 15:41:15

我将创建一个包含差异的 Status 类,并调用它们。 所以(在Python中):

class StatusZero(object):
    def call_me(self, app):
       print 'Hello, from ' + app.name
       return db.prepare_specific_status_zero_request()


class StatusOne(object):
    def call_me(self, app):
        print 'Hi, from ' + app.name
        return db.prepare_specific_status_one_request()

states = { 'status_zero' : StatusZero(), 'status_one' : StatusOne() }

class Application(object):
    name = 'My App'
    status = states['status_zero']

    def change_state(self, state):
        status = state

    def call_me(self):
        state_key = self.status.call_me(self)
        self.change_state(states[state_key])

快速、轻松地保持功能划分,并且通过状态之间合理的继承模式,您可以共享没有不同的功能。

I'd create a Status class that contains the differences, and call those. So (in Python):

class StatusZero(object):
    def call_me(self, app):
       print 'Hello, from ' + app.name
       return db.prepare_specific_status_zero_request()


class StatusOne(object):
    def call_me(self, app):
        print 'Hi, from ' + app.name
        return db.prepare_specific_status_one_request()

states = { 'status_zero' : StatusZero(), 'status_one' : StatusOne() }

class Application(object):
    name = 'My App'
    status = states['status_zero']

    def change_state(self, state):
        status = state

    def call_me(self):
        state_key = self.status.call_me(self)
        self.change_state(states[state_key])

Fast, easy to keep the functionality compartmentalized, and with a reasonable inheritance pattern between the states you can share the functions which don't differ.

离笑几人歌 2024-07-21 15:41:15

我的理解是,状态模式对于仅 UI 或仅在内存中执行非常好,在我的情况下,当从应用程序表检索数据时,仍然需要 if else 语句来确定要转换到哪个对象。

public AbstractApplication convert_db_application_to_object(obj db_application)
{
   AbstractApplication app;
   if (db_application.Status == (int)Status.New)
      app = application_factory.create(application_state_new);
   else if(db_application.Status == (int)Status.Closed)
      app = application_factory.create(application_state_closed);

   return app;
}

我不认为这是一个优雅的解决方案
因为我仍然需要枚举或查找表来将应用程序状态保存到数据库中

my understanding is that state pattern is quite good for UI only or in memory only executions, where in my situation when retrieving data back from application table, there still need to be if else statement to determine what object to cast to.

public AbstractApplication convert_db_application_to_object(obj db_application)
{
   AbstractApplication app;
   if (db_application.Status == (int)Status.New)
      app = application_factory.create(application_state_new);
   else if(db_application.Status == (int)Status.Closed)
      app = application_factory.create(application_state_closed);

   return app;
}

i don't see this as an elegant solution
as i still need either an enum or lookup table to save the application status into database

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文