Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python适配器模式_Python_Design Patterns_Adapter - Fatal编程技术网

Python适配器模式

Python适配器模式,python,design-patterns,adapter,Python,Design Patterns,Adapter,假设我有下面的课 class MultiplePeopleInBook(object): def __init__(self, names, ages, book_title): self._names = names self._ages = ages self._book_title = book_title @property def names(self): return self._names

假设我有下面的课

class MultiplePeopleInBook(object):
    def __init__(self, names, ages, book_title):
        self._names = names
        self._ages = ages
        self._book_title = book_title

    @property
    def names(self):
        return self._names

    @property
    def ages(self):
        return ages

    @property
    def book_title(self):
        return book_title
现在我需要另一个类,假设这个类代表一本书,里面正好有一个人

class SinglePersonInBook(object):
    def __init__(self, multiple_person_object):
        self._person = multiple_person_object

    @property
    def name(self):
        return self._person.names[0]

    @property
    def age(self):
        return self._person.ages[0]
因此,我将创建一个MultiplePeopleInBook对象,如下所示:

mpib = MultiplePeopleInBook(['Bob', 'Foo'], [12,24],'foo book')
def __init__(self, name, age, book_title):
    self._person = MultiplePeopleInBook([name],[age],boo_title)
spib = SinglePersonInBook('Bar', 122, 'bar book')
def __init__(self, name, age, book_title):
    self._person = MultiplePeopleInBook([name],[age],boo_title)
spib = SinglePersonInBook('Bar', 122, 'bar book')
和一本单人书,如下所示:

spib = SinglePersonInBook(MultiplePeopleInBook(['Bar'], [122], 'bar book')
问题: 我的设置正确吗

我怎样才能得到spib的书名?我需要在SinglePersonInBook适配器中有一个方法吗?如果是这样,那么将其作为适配器有什么意义

我可以这样为SPIB编写init方法吗:

mpib = MultiplePeopleInBook(['Bob', 'Foo'], [12,24],'foo book')
def __init__(self, name, age, book_title):
    self._person = MultiplePeopleInBook([name],[age],boo_title)
spib = SinglePersonInBook('Bar', 122, 'bar book')
def __init__(self, name, age, book_title):
    self._person = MultiplePeopleInBook([name],[age],boo_title)
spib = SinglePersonInBook('Bar', 122, 'bar book')
这样,我可以创建一个SPIB对象,如下所示:

mpib = MultiplePeopleInBook(['Bob', 'Foo'], [12,24],'foo book')
def __init__(self, name, age, book_title):
    self._person = MultiplePeopleInBook([name],[age],boo_title)
spib = SinglePersonInBook('Bar', 122, 'bar book')
def __init__(self, name, age, book_title):
    self._person = MultiplePeopleInBook([name],[age],boo_title)
spib = SinglePersonInBook('Bar', 122, 'bar book')
我觉得这个干净多了

我怎样才能得到spib的书名?我需要在SinglePersonInBook适配器中有一个方法吗?如果是这样,那么将其作为适配器有什么意义

是的,你知道。在您的示例中,制作适配器是毫无意义的。更好的解决方案是在书中用两种实现(单个和多个)为人们做一个抽象。基本问题是——你需要它做什么

我可以这样为SPIB编写init方法吗:

mpib = MultiplePeopleInBook(['Bob', 'Foo'], [12,24],'foo book')
def __init__(self, name, age, book_title):
    self._person = MultiplePeopleInBook([name],[age],boo_title)
spib = SinglePersonInBook('Bar', 122, 'bar book')
def __init__(self, name, age, book_title):
    self._person = MultiplePeopleInBook([name],[age],boo_title)
spib = SinglePersonInBook('Bar', 122, 'bar book')
这样,我可以创建一个SPIB对象,如下所示:

mpib = MultiplePeopleInBook(['Bob', 'Foo'], [12,24],'foo book')
def __init__(self, name, age, book_title):
    self._person = MultiplePeopleInBook([name],[age],boo_title)
spib = SinglePersonInBook('Bar', 122, 'bar book')
def __init__(self, name, age, book_title):
    self._person = MultiplePeopleInBook([name],[age],boo_title)
spib = SinglePersonInBook('Bar', 122, 'bar book')
我觉得这个干净多了

可以,但在这种情况下,简单抽象更好