17

PythonでUML用語で構成と集計を実装する方法を知りたいです。

私が理解した場合:

  1. 集計:

class B:
    pass

class A(object):
    def __init__(self):
        self.B = B

  1. 構成:

self.B他の言語では、これが B へのポインターとして実装されているのを見ました。これは Python のポインターだと思います。

class A(object):
    def __init__(self, B):
        self.B = B

そうですか?

4

3 に答える 3

34

私が正しく理解していれば、集約と構成は、オブジェクトのメンバーに対する責任に関するものです (たとえば、インスタンスを削除すると、そのメンバーも削除されますか?)。

主に、実装に大きく依存します。たとえば、クラス B (集約) のインスタンスを受け取るクラス A を作成するには、次のように記述できます。

class B(object): pass

class A(object):
    def __init__(self, b):
        self.b = b

b = B()
a = A(b)

ただし、注意点として、Python には、他のものを渡すのを妨げるものは何も組み込まれていません。たとえば、次のようになります。

a = A("string") # still valid

A (コンポジション) のコンストラクター内で B のインスタンスを作成する場合は、次のように記述できます。

class A(object):
    def __init__(self):
        self.b = B()

または、クラスをコンストラクターに挿入してから、次のようにインスタンスを作成することもできます。

class A(object):
    def __init__(self, B):
        self.b = B()

余談ですが、少なくとも最初の例とおそらく2番目の例では、 B を B のインスタンスではなく、 B のクラス定義に設定しています。

class A(object):
    def __init__(self, B):
        self.B = B

>>> a = A()
>>> a.B # class definition
<class __main__.B at 0x028586C0>
>>> a.B() # which you can make instances of
<__main__.B instance at 0x02860990>

したがって、A のインスタンスが B のクラス定義を指すことになりますが、これはあなたが求めているものではないと確信しています。とはいえ、それは一般的に他の言語でははるかに難しいので、それが混乱のポイントの1つであったかどうかは理解しています.

于 2013-11-08T15:53:38.700 に答える
4
# Aggregation is NOT exclusive
class BaseChapter:
    '''
    We can use this BaseChapter in any book, like in OpenBook.
    '''

    def __init__(self, name):
        self.name = name
        self.subject = None
        self.content = None
        return

class OpenBook:

    def __init__(self, isbn):
        self.isbn = isbn
        self.chapters = list()

    def add_chapter(self, obj):

        # This constrain dont have correlation with composition/aggregation
        if isinstance(obj, BaseChapter):
            self.chapters.append(obj)
        else:
            raise TypeError('ChapterError')

# .. but Composition is Exclusive
# Example:
class MyBook:

    class MyChapter:
        '''
        This MyChapter can be used only by MyBook
        '''
        def __init__(self, name, subject):
            self.name = name
            self.subject = subject
            self.title = None
            self.content = None
            self.techincal_refs = list()
            return

    def __init__(self, isbn):
        self.isbn = isbn
        self.chapters = list()

    def add_chapter(self, obj):
        # This constrain dont have correlation with composition/aggregation
        # what is important here is MyChapter can be used only by MyBook
        # a outside object cant create a instance of MyChapter
        if isinstance(obj, self.MyChapter):
            self.chapters.append(obj)
        else:
            raise TypeError('ChapterError')

..そして、はい、私たちはもっとうまくやることができます

class MyBook:

    class MyChapter(BaseChapter):
        '''
        This MyChapter can be used only by MyBook,
        but now is based in BaseChapter.
        But you knhow, python dont create problems if you still want
        create a instance of MyChapter in other 'Books'.

        But when you see this code you will think, This class is exclusive
        to MyBook.
        '''
        def __init__(self, name):
            super().__init__(name)
            self.subject = None
            self.title = None
            self.content = None
            self.techincal_refs = list()
            return

    def __init__(self, nib):
        self.nib = nib
        self.chapters = list()

    def add_chapter(self, obj):
        # This constrain dont have correlation with composition/agregation
        # what is important here is MyChapter can be used only by MyBook
        if isinstance(obj, self.MyChapter):
            self.chapters.append(obj)
        else:
            raise TypeError('ChapterError')
于 2016-09-27T12:40:37.333 に答える