如何在Web MVC框架中正确实现Strategy模式?

时间:2022-04-17 19:54:38

In my Django app, I have a model (lets call it Foo) with a field called "type". I'd like to use Foo.type to indicate what type the specific instance of Foo is (possible choices are "Number", "Date", "Single Line of Text", "Multiple Lines of Text", and a few others).

在我的Django应用程序中,我有一个名为“type”的模型(让我们称之为Foo)。我想使用Foo.type来指示Foo的特定实例的类型(可能的选择是“数字”,“日期”,“单行文本”,“多行文本”等等) 。

There are two things I'd like the "type" field to end up affecting; the way a value is converted from its normal type to text (for example, in "Date", it may be str(the_date.isoformat())), and the way a value is converted from text to the specified type (in "Date", it may be datetime.date.fromtimestamp(the_text)).

有两件事我想要“类型”字段最终影响;将值从其普通类型转换为文本的方式(例如,在“Date”中,它可以是str(the_date.isoformat())),以及将值从文本转换为指定类型的方式(在“日期“,它可能是datetime.date.fromtimestamp(the_text))。

To me, this seems like the Strategy pattern (I may be completely wrong, and feel free to correct me if I am). My question is, what's the proper way to code this in a web MVC framework?

对我而言,这似乎是战略模式(我可能完全错了,如果我是,请随时纠正我)。我的问题是,在Web MVC框架中对此进行编码的正确方法是什么?

In a client-side app, I'd create a Type class with abstract methods "serialize()" and "unserialize()", override those methods in subclasses of Type (such as NumberType and DateType), and dynamically set the "type" field of a newly-instantiated Foo to the appropriate Type subclass at runtime.

在客户端应用程序中,我将使用抽象方法“serialize()”和“unserialize()”创建一个Type类,覆盖Type的子类中的那些方法(如NumberType和DateType),并动态设置“类型” “在运行时将新实例化的Foo字段移动到相应的Type子类。

In a web framework, it's not quite as straightforward for me. Right now, the way that makes the most sense is to define Foo.type as a Small Integer field and define a limited set of choices (0 = "Number", 1 = "Date", 2 = "Single Line of Text", etc.) in the code. Then, when a Foo object is instantiated, use a Factory method to look at the value of the instance's "type" field and plug in the correct Type subclass (as described in the paragraph above). Foo would also have serialize() and unserialize() methods, which would delegate directly to the plugged-in Type subclass.

在Web框架中,对我来说并不那么简单。现在,最有意义的方法是将Foo.type定义为小整数字段并定义一组有限的选择(0 =“数字”,1 =“日期”,2 =“单行文本”,等等。在代码中。然后,当实例化Foo对象时,使用Factory方法查看实例的“type”字段的值并插入正确的Type子类(如上段所述)。 Foo还有serialize()和unserialize()方法,它们会直接委托给插件类型的子类。

How does this design sound? I've never run into this issue before, so I'd really like to know if other people have, and how they've solved it.

这个设计听起来怎么样?我之前从未遇到过这个问题,所以我真的想知道其他人是否有,以及他们是如何解决的。

1 个解决方案

#1


You could look through inheritance stuff that the django ORM provides. This will give you ways to actually subclass whatever the model is w/ discriminators, different tables, etc.

您可以查看django ORM提供的继承内容。这将为您提供实际子类化的方法,无论模型具有判别器,不同的表等。

Foo # base
DateFoo(Foo)
OtherFoo(Foo)

etc...

#1


You could look through inheritance stuff that the django ORM provides. This will give you ways to actually subclass whatever the model is w/ discriminators, different tables, etc.

您可以查看django ORM提供的继承内容。这将为您提供实际子类化的方法,无论模型具有判别器,不同的表等。

Foo # base
DateFoo(Foo)
OtherFoo(Foo)

etc...