适配器和存储库模式之间的区别是什么?

2024-04-25 06:38:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我对这种“模式”有点陌生,所以我建立了一个存储库来帮助我使用和测试python中的API。然后我把它展示给一位朋友,他说这不是一个存储库模式,而是一个适配器。一旦他是一个比我年长、更好的程序员,我就无法反驳,而且我也不知道两者之间有什么区别

下面是代码(我为这一条感到非常自豪)

# imports
from repository.utils import factory_getter
from typing import Union

# return objects containing lists of animes
class Animes(object):
    # get anime list for a given days of the week
    def get_day_schedule(self, day: str):
        return factory_getter("schedule", day)

    # return a object containing a list of animes airing in the current day
    def get_today_schedule(self):
        return factory_getter("schedule")

    # get anime list for a given year and season
    def get_early_season(self, season: Union[str, int], year: Union[str, int]):
        return factory_getter("season", str(season), str(year))

    # get animes airing in the current season
    def get_current_season(self):
        return factory_getter("season")

    # get animes that have been announced for the upcoming seasons
    def get_later_season(self):
        return factory_getter("season", "later")

    # get the best rated animes of the season
    def get_top_airing(self, page: Union[str, int] = 1):
        return factory_getter("top", "anime", str(page), "airing")

    # get the best rated movies
    def get_top_movies(self, page: Union[str, int] = 1):
        return factory_getter("top", "anime", str(page), "airing")

    # get animes of a certain genre
    def get_genre(self, genre, page: Union[str, int] = 1):
        return factory_getter("genre", "anime", str(genre), str(page))

就这样。
如果你能告诉我一些学习资料,我也将非常感激。谢谢


Tags: oftheselfgetreturnfactorydefpage
1条回答
网友
1楼 · 发布于 2024-04-25 06:38:48

嗯,适配器模式用于一个旧实现和一个新实现,但它们的接口不同的情况,也就是说,这两个impl不是可替代的,但在概念上它们做的是相同的事情。也许新的实现需要重用大部分旧的实现。如果我们用新的实现替换旧的实现,它将导致破坏性的更改。所以我们要做的是,提取旧实现的接口。在使用旧IML的地方使用该接口,同时我们编写该“适配器”的自己的IML。适配器的impl可以重用旧的impl并提供它的新行为,但现在符合一个公共接口

存储库是一个完全不同的东西。它用于封装对实体和DAO的所有访问。适配器与存储库有什么关系?有没有具体的案例

您可能需要为存储库编写适配器,为它提供额外的功能,同时仍然重用它的旧方法—这在Spring数据中非常有用,因为IMPL使用动态代理具有开箱即用的行为。我不知道你的确切情况。但希望这能有所帮助

相关问题 更多 >