samedi 18 février 2017

Difference between Builder / Mediator / Bridge Design Pattern

Referring this git url for the DPs : http://ift.tt/2m7I8CI

My understanding was that,

builder starts with one factory and later that factory object is re-used and its state changed ( like pizza=Pizza(), pizza.addCheese(), pizza.addChicken() etc) - doesn't take ingredients from outside

bridge is like builder but you got to pass the methods in the object calls ( like pizza=Pizza(pizza.addCheese(pizza.addChicken()))etc) -takes ingredients from outside

mediator - no understanding!!! :\

Having a look at the below programs, really had me confused as it somewhat looks to follow same approach and design
(Cutting short to just main methods, how the code is being executed / triggered, this should give a fair amount of idea.)

Bridge.py :

def main():
    shapes = (
        CircleShape(1, 2, 3, DrawingAPI1()),
        CircleShape(5, 7, 11, DrawingAPI2())
    )

    for shape in shapes:
        shape.scale(2.5)
        shape.draw()


if __name__ == '__main__':
    main()

Mediator.py

if __name__ == '__main__':
    reporter = Reporter()
    db = DB()
    tm = TestManager()
    tm.setReporter(reporter)
    tm.setDB(db)
    reporter.setTM(tm)
    db.setTM(tm)
    # For simplification we are looping on the same test.
    # Practically, it could be about various unique test classes and their
    # objects
    for i in range(3):
        tc = TC()
        tc.setTM(tm)
        tm.setTC(tc)
        tc.setup()
        tc.execute()
        tc.tearDown()

Builder.py

if __name__ == "__main__":
    director = Director()
    director.builder = BuilderHouse()
    director.construct_building()
    building = director.get_building()
    print(building)
    director.builder = BuilderFlat()
    director.construct_building()
    building = director.get_building()
    print(building)


Most of the thread on SO focus on theoretical definition, i am looking for some practical implementations which can state the difference in implementation or a missing piece, please see the main programs above

Can someone please help me understand it better??????
TIA

Aucun commentaire:

Enregistrer un commentaire