在这种情况下应该采用哪种设计模式?
我正在做一些事情,并且已经使用了一些设计模式,但是它们都不能完全满足我的需求,这让我觉得我可能会使用模式的组合,但我有点卡住了,所以我会解释一下这里的情况,希望有人能为解决这个问题的正确方法提供一些线索。我正在使用的语言是 PHP 8,以防它有助于或限制寻找解决方案。
我有一个通过静态方法构建 GraphQL 查询和突变的类。我无法为您提供确切的实现,但它是这样的:
class GraphQLBuilder
{
public static function getSettings(): string
{
return <<<GRAPHQL
query{
settings{
id
name
alias
}
}
GRAPHQL;
}
public static function getSetting(string $id): string
{
return <<<GRAPHQL
query{
setting(id: "$id"){
id
name
alias
}
}
GRAPHQL;
}
public static function setName(string $id, string $name)
{
return <<<GRAPHQL
mutation {
setting(
id: "$id",
name: "$name"
) {
id
name
}
}
GRAPHQL;
}
}
GraphQL 服务器正在迁移到新版本,并且字段 alias
将被称为 aka
,但为了兼容性他们将保留旧场地一年的原因。我需要创建一个支持新 aka
字段的新版本 Builder 类,因此它将具有不同的 getSettings()
和 getSetting($id)< /code> 方法,但
setName()
方法是相同的。
我的目标是创建一个适用于所有使用的方法的类(在示例中,getSettings()
、getSetting($id)
和 setName($id , $name)
),但对于未实现的方法则回退到基类。我将使用一个接口来检查新类中是否涵盖了所有方法。
到目前为止,我尝试通过创建一个返回 GraphQLBuilder 或 GraphQLBuilderNewVersion 的 BuilderFactory 来使用策略模式,因此可以以相同的方式使用这些方法: >builder::getSettings(),但这让我在 GraphQLBuilderNewVersion 中包含 setName()
方法,没有任何区别,而且我不想这样做,因为我不想在不同的类中维护相同的代码。
另一种方法是创建一个抽象类 GraphQLAbstractBuilder,它具有静态 $settingProvider 属性和一个 GraphQLBuilderBase 来保存所需的所有方法。倒退。 GraphQLBuilder
或 GraphQLBuilderNewVersion
均从 GraphQLBuilderBase
扩展并实现自己的特定方法,但我希望这些特定方法在界面中进行控制,但他们没有后备措施,所以我可以以可控的方式让他们失败。
我觉得我想得太多了,这可能是一个非常简单的解决方案,因此任何针对这个问题进行稳健设计的建议或技巧都将非常感激。
I'm working on something and I've already used some design patterns, but none of them fulfills my needs completely, and that makes me think I might use a combination of patterns, but I'm a little bit stuck so I will explain the situation here and hopefully somebody could add some light about the right way to work on this one. The language I'm working with is PHP 8, in case that it helps or limits on finding a solution.
I have a class that builds, via static methods, GraphQL queries and mutations. I can't give you the exact implementation but it's something like this:
class GraphQLBuilder
{
public static function getSettings(): string
{
return <<<GRAPHQL
query{
settings{
id
name
alias
}
}
GRAPHQL;
}
public static function getSetting(string $id): string
{
return <<<GRAPHQL
query{
setting(id: "$id"){
id
name
alias
}
}
GRAPHQL;
}
public static function setName(string $id, string $name)
{
return <<<GRAPHQL
mutation {
setting(
id: "$id",
name: "$name"
) {
id
name
}
}
GRAPHQL;
}
}
The GraphQL server is migrating to a new version and the field alias
will be called aka
, but for compatibility reasons they will keep the old field for a year. I need to create a new version of the Builder class that supports the new aka
field, so it will have different getSettings()
and getSetting($id)
methods, but the setName()
method will be the same.
My goal is to create a class that works with all the methods used (in the example, getSettings()
, getSetting($id)
and setName($id, $name)
) but fallbacks to a base class for the not implemented methods. I'd use an interface to check that all the methods are covered in the new class.
So far, I've tried to use the strategy pattern by creating a BuilderFactory that returns whether GraphQLBuilder
or GraphQLBuilderNewVersion
, so the methods could be used at the same way: builder::getSettings()
, but that makes me include the setName()
method, without any difference, in the GraphQLBuilderNewVersion, and I wouldn't like to do that because I don't want to maintain the same code in different classes.
Another approach was creating an abstract class GraphQLAbstractBuilder
that has a static $settingProvider
attribute, and a GraphQLBuilderBase
that holds all the methods that would be needed to fallback. Both GraphQLBuilder
or GraphQLBuilderNewVersion
extend from GraphQLBuilderBase
and implement their own specific methods, but I'd like that those specific methods are controlled in the interface but they don't have a fallback, so I can make them fail in a controlled way.
I feel like I'm overthinking this and it may be a very straightforward solution, so any advice or tip to make a robust design on this problem will be really appreaciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论