Τρίτη 25 Ιανουαρίου 2022

Bridge Pattern

  • What problem does Bridge pattern solve?



Suppose we have a red Triangle, a red Square, and a red Circle shape

What if we want a yellow, green, purple etc. for each of above shapes?
Will we create a new class, like YellowCircleShape, PurpleCircleShape, etc.,  for each case?

This will lead to overpopulation of objects in a huge hierarchy, not manageable at all.

This may seem ok:


But not this one:







  • How does Bridge pattern solve this?


-Decouple Shapes and Colors
-Create a Color once, and re-use it in any Shape

1. Distinguish Shapes and Colors hierarchies



2. Color classes will have the sole responsibility of altering the color of the underlying Shape reference (using Graphics reference)



3. Shape classes will accept Colors in their constructors (Bridge)



4. Use final Shape classes, passing the Color as parameter:























Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

What may be missing, or could get better?