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



我对这种"模式"有点陌生。所以我创建了一个存储库来帮助我使用和测试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))

就是这样。
如果您能指导我一些学习资料,我将非常感激。由于

好吧,适配器模式用于你有一个旧的实现和一个新的实现对同一件事,但他们的接口是不同的情况下,即两个impls是不可替代的,但在概念上他们做同样的事情。可能会出现新实现需要重用大部分旧实现的情况。如果我们用新的实现代替旧的实现,就会造成破坏性的变化。我们要做的是,提取出旧实现的接口。在旧的接口被使用的地方使用那个接口,当我们写我们自己的适配器的接口时。适配器的impl可以重用旧的impl,并可以提供它的新行为,但现在符合一个公共接口。

Repository是完全不同的东西。它用于封装对实体和dao的所有访问。适配器与Repository有什么关系?有具体的案例吗?

可能需要为存储库编写适配器,为其提供额外的功能,同时仍然重用它的旧方法—这在Spring数据中非常有用,其中imps使用动态代理具有开箱即用的行为。我不知道你的具体案子是什么。但希望这对你有帮助。

相关内容

最新更新