Home > Software design >  Would giving a list of objects to another class , not make this relation aggregation?
Would giving a list of objects to another class , not make this relation aggregation?

Time:02-14

class A:
    def __init__(self, id, name ,age):
        self.id = id
        self.name = name
        self.age = age

    def get_id(self):
        return self.id

    def get_name(self):
        return self.name

    def get_age(self):
        return self.age


class B:
    def __init__(self, lst, file):
        self.lst = lst
        self.file = file

    def writefile(self):
        fileObj = open(self.file, 'w')
        write_string = ''
        for person in self.lst:
            for func in [person.get_id, person.get_name, person.get_age]:
                write_string  = func()   '\t'
            write_string = write_string[:-1]   '\n'
        fileObj.write(write_string[:-1])
        fileObj.close() 
            


def main():
    file = 'sample.txt'

    def loadfile():
        try:
            filez = open(file, 'r')
        except FileNotFoundError:
            filez = open(file, 'w')
            filez.close()
            filez = open(file, 'r')
        lst = []
        for line in filez:
            id, name, age = line.split('\t')
            age = date.rstrip('\n')
            lst.append(A(id, name, age))
        return lst

    population = loadfile()
    call = B(population, file)

    def add():
        obj_A1 = A('1','bilal','29')
        obj_A3 = A('3','asda','54')
        population.append(obj_A1)
        population.append(obj_A3)
    add()

    def display():
        for i in population:
            if i.get_id() == '3':
                print('found')
    display()

    call.writefile()
                
main()

Im new to OOP. im trying to understand if giving a list of objects to class B would make any difference to its relation. The relation i hope here is aggregation between Class A and class B if im not wrong since im adding population list to class B ? ps. sorry for the long code.

CodePudding user response:

According to Martin Fowler's UML Distilled:

Aggregation is strictly meaningless; as a result, I recommend that you ignore it in your own diagrams.

That leaves us with the question of whether the objects contained within lst have an association or composition relationship with B?

Composition implies that the objects being composed do not outlive their container classes. It also implies that they are not shared by other container classes.

Association defines a looser coupling relationship than composition, namely that one object merely refers to another object. In python this basically translates to the idea that the class has a certain instance attribute.

In your code you first instantiate population then pass it to B's constructor. Suppose you had another object C which could take in a list object just like B:

class C:
    def __init__(self, lst, file):
        self.lst = lst
        self.file = file

One could imagine that later in your main block, the population variable could be reused to instantiate C. This would result in a shared instance attribute between B and C. Also if you called del B somewhere later in your code, it wouldn't destroy population. You therefore have a mere association relationship here.

If you wanted to implement true composition (which I assume is what you mean by aggregation), you could either instantiate the list within your constructor:

class B:
    def __init__(self, file):
        self.lst = loadfile(file)
        self.file = file

Or you could instantiate the list upon B instantiation as follows:

call = B(loadfile(file), file)
  • Related