composition and aggregation in python

前端 未结 3 1961
南旧
南旧 2020-12-23 17:52

I want to know how to implement composition and aggregation in UML terms in python.

If I understood:

  1. Aggregation:

3条回答
  •  旧时难觅i
    2020-12-23 18:55

    # 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')
    

    .. and yes we can do better like

    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')
    

提交回复
热议问题