决定需要哪个子类的最佳方法

发布于 2024-07-24 18:39:02 字数 486 浏览 6 评论 0原文

我正在为当前项目开发一个大型结帐应用程序。 这种结帐有很多情况,具体取决于用户的管理级别、他们如何进行结帐以及他们要结帐的项目类型,因此该过程通​​过一组上下文类从 .aspx 页面中抽象出来。

这些类都是单个类 CheckoutContext 的子类,并且要使用的类的类型通过枚举来记录。

是否有类似于 typedef 的东西我可以用来选择要使用的子类,或者我应该简单地有一个返回相关类的方法,如下所示:

CheckoutContext chooseSubclass(CheckoutCase c)
{
CheckoutContext output;
switch (c):
{
  case CheckoutCase.SingleItemNew:
    output = new SingleItemNew;
    break;
  case . . . 
  return output;
}
}

I am working on a large-scale checkout application for a current project.
This checkout has many cases depending on the user's admin level, how they got to the checkout, and what type of item they are checking out, and so the process is abstracted away from the .aspx pages via a set of context classes.

These classes all subclass from a single class, CheckoutContext, and the type of class to be used is noted via an enum.

Is there something similar to typedef I can use to choose which subclass to use, or should I simply have a method that returns the relevant class, like so:

CheckoutContext chooseSubclass(CheckoutCase c)
{
CheckoutContext output;
switch (c):
{
  case CheckoutCase.SingleItemNew:
    output = new SingleItemNew;
    break;
  case . . . 
  return output;
}
}

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

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

发布评论

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

评论(5

奈何桥上唱咆哮 2024-07-31 18:39:03

您可以使用自定义属性和工厂方法来实现此目的。 让所有子类实现 CustomAttribute,例如采用 CheckOutCase Enum 值的 CheckOutCaseScenarioAttribute。

在工厂方法中,查找设置了此枚举值的类型并创建对象。 这将避免你的开关盒。 如果您的工厂方法中没有任何其他初始化逻辑,这将起作用。

You could implement this with a custom attribute and a factory method. Make all you Sub Classes implement a CustomAttribute say CheckOutCaseScenarioAttribute that takes the CheckOutCase Enum value.

Within your factory method, look for types that have this Enum Value set and create the object. This will avoid your switch case. This will work if you dont have any other initialization logic within your factory method.

乖乖公主 2024-07-31 18:39:03

这称为工厂设计模式。 我将创建一个返回所需类的静态方法。 这里的一个好习惯是实现一个接口并返回该接口。

interface ICheckoutItem
{
    void CheckOut();
}

让您的项目实现该接口。 然后在你的工厂方法中返回每个项目的接口。

ICheckoutItem chooseSubclass(CheckoutCase c)
{
ICheckoutItem output;
    switch (c):
    {
       case CheckoutCase.SingleItemNew:
         output = new SingleItemNew;
         break;
       case . . . 
  return output;
    }
}

This is called a Factory Design Pattern. I would create a static method that returns the needed class. A good practice here is to implement an Interface and return the interface.

interface ICheckoutItem
{
    void CheckOut();
}

Have your items implement the interface. Then in your factory method return the interface of each item.

ICheckoutItem chooseSubclass(CheckoutCase c)
{
ICheckoutItem output;
    switch (c):
    {
       case CheckoutCase.SingleItemNew:
         output = new SingleItemNew;
         break;
       case . . . 
  return output;
    }
}
傻比既视感 2024-07-31 18:39:03

您可以创建一个属性,该属性具有一个 CheckoutContext 类型的属性:

public class CheckoutContextAttribute : Attribute
{
   public Type CheckoutType{get;set;}
}

然后,在您的枚举上,您可以将正确的属性放在正确的枚举类型上:

public enum CheckoutCase
{
   [CheckoutContext(CheckoutType=typeof(SingleItemNew)]
   SingleItemNew,
   ...
   ...
}

然后,在该方法中您需要发回正确的 Context 类型您使用反射并执行类似的操作:

public CheckoutContext GetContext(CheckoutCase c)
{
   FieldInfo field = c.GetType().GetField(c.ToString());
   object[] attribs = field.GetCustomAttributes(typeof(CheckoutContextAttribute),false);
   CheckountContext result = null;
   if(attribs.Length > 0)
   {
      CheckoutContextAttribute attrib = attribs[0] as CheckoutContextAttribute;
      Type type = attrib.CheckoutType;
      result = Activator.CreateInstance(type) as CheckountContext;
   }

   return result;
}

这应该可以解决问题。 只需添加一些空/错误检查即可确保安全。

You can create an attribute that has one property which would be the type of CheckoutContext:

public class CheckoutContextAttribute : Attribute
{
   public Type CheckoutType{get;set;}
}

Then, on your enum, you can put the correct attribute on the correct enum type:

public enum CheckoutCase
{
   [CheckoutContext(CheckoutType=typeof(SingleItemNew)]
   SingleItemNew,
   ...
   ...
}

Then, in that method where you need to send back the correct Context type you use reflection and do something like this:

public CheckoutContext GetContext(CheckoutCase c)
{
   FieldInfo field = c.GetType().GetField(c.ToString());
   object[] attribs = field.GetCustomAttributes(typeof(CheckoutContextAttribute),false);
   CheckountContext result = null;
   if(attribs.Length > 0)
   {
      CheckoutContextAttribute attrib = attribs[0] as CheckoutContextAttribute;
      Type type = attrib.CheckoutType;
      result = Activator.CreateInstance(type) as CheckountContext;
   }

   return result;
}

This should do the trick. Just add some null / error checking to be safe.

趁年轻赶紧闹 2024-07-31 18:39:02

您正在实现的是工厂模式。 这是一种标准做法,尽管它通常意味着编写大量重复代码(很像 switch 语句,这通常是它们的实现方式)。 你可以做各种奇特的事情,比如通过反射进行动态实例化,但不要玩火。 只要坚持使用 switch 语句就可以了。

What you're implementing is a Factory Pattern. This is a standard practice, though it typically means writing a lot of repetitive code (much like your switch statement, which is often how they're implemented). You could do all sorts of fancy things like dynamic instantiation via reflection, but don't play with fire. Just stick with switch statement and you'll be fine.

谎言月老 2024-07-31 18:39:02

如果存在大量案例,我将创建一个 Dictionary 并使用一组所有 CheckoutCase 值和相应的 CheckoutContext 类型一次性填充它。 然后,您可以使用 Activator.CreateInstance 返回适当的类型,而不是使用巨大的 switch 语句。

If there are a large number of cases, I would create a Dictionary<CheckoutCase, Type> and populate it one time with the set of all CheckoutCase values and corresponding CheckoutContext Types. Then you could use Activator.CreateInstance to return the appropriate type instead of a gigantic switch statement.

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