You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classShape:
def__init__(self, renderer):
self.renderer=rendererdefdraw(self):
passclassSquare(Shape):
defdraw(self):
self.renderer.render_square()
classCircle(Shape):
defdraw(self):
self.renderer.render_circle()
classRenderer:
defrender_square(self):
passdefrender_circle(self):
passclassVectorRenderer(Renderer):
defrender_square(self):
print("Drawing a square in vector format")
defrender_circle(self):
print("Drawing a circle in vector format")
classRasterRenderer(Renderer):
defrender_square(self):
print("Drawing a square in raster format")
defrender_circle(self):
print("Drawing a circle in raster format")
# Using the Bridgevector_renderer=VectorRenderer()
raster_renderer=RasterRenderer()
square=Square(vector_renderer)
square.draw() # Output: Drawing a square in vector formatcircle=Circle(raster_renderer)
circle.draw() # Output: Drawing a circle in raster format
Implementation in JavaScrip:
classVehicle{constructor(engine){this.engine=engine;}specs(){return"";}}classCarextendsVehicle{specs(){return`This is a car with ${this.engine.type} engine`;}}classBikeextendsVehicle{specs(){return`This is a bike with ${this.engine.type} engine`;}}classEngine{constructor(type){this.type=type;}}classPetrolEngineextendsEngine{constructor(){super("petrol");}}classDieselEngineextendsEngine{constructor(){super("diesel");}}// Using the BridgeconstpetrolEngine=newPetrolEngine();constdieselEngine=newDieselEngine();constcar=newCar(petrolEngine);console.log(car.specs());// Output: This is a car with petrol engineconstbike=newBike(dieselEngine);console.log(bike.specs());// Output: This is a bike with diesel engine