dimanche 15 décembre 2019

Understanding of adapter-pattern

I try to realize adapter pattern. In MappingAdapter init Light object, then i return light array.

class Light:
    def __init__(self, dim):
        self.dim = dim
        self.grid = [[0 for i in range(dim[0])] for _ in range(dim[1])]
        self.lights = []
        self.obstacles = []

    def set_dim(self, dim):
        self.dim = dim
        self.grid = [[0 for i in range(dim[0])] for _ in range(dim[1])]

    def set_lights(self, lights):
        self.lights = lights
        self.generate_lights()

    def set_obstacles(self, obstacles):
        self.obstacles = obstacles
        self.generate_lights()

    def generate_lights(self):
        return self.grid.copy()


class MappingAdapter(Light):
    def __init__(self, adaptee):
        self.adaptee = adaptee

    def lighten(self, grid):
        self.adaptee.set_dim()
        self.adaptee.set_lights()
        self.adaptee.set_obstacles()
        print(self.adaptee.generate_lights())
        return self.adaptee.generate_lights()

class System:
    def __init__(self):
        self.mape = self.grid = [[0 for i in range(30)] for _ in range(20)]
        self.mape[5][7] = 1 # Источники света
        self.mape[5][2] = -1 # Стены

    def get_lightening(self, light_mapper):
        self.lightmap = light_mapper.lighten(self.mape)

dim = (2,3)
light = Light(dim)
adapter = MappingAdapter(light)
system = System(adapter)

Unfortunately, i'm not understand how Light methods takes variable from System class through the Adapter.

Aucun commentaire:

Enregistrer un commentaire