Bridge Design Pattern Decouple an abstraction from its

Bridge Design Pattern Decouple an abstraction from its implementation so that the two can vary independently

Bridge Pattern Motivation • • • abstract interfaces are implemented using concrete implementation through inheritance – may not be flexible enough: permanently binds implementation to interface example problem: – Square and Triangle inherit from Figure – what if need to draw red and blue figures? • have to create Red. Square, Red. Triangle, Blue. Square Blue. Triangle another example problem – graphics system has window class, need to support multiple window managers – have to sublcass • what if need to sublcass Window? e. g. Icon. Window 2

Bridge Design Pattern Structural design pattern – controls relationships between classes handle body 3

Implementation Example 4

Casting C++ implements several type of casts (explicit type conversions) • static_cast <new type> (expression) – compile time casting (early binding for pointers) • dynamic_cast – run-time casting (vtable is consulted), if incorrect returns nullptr and throws std: : bad_cast • reinterpret_cast – no type checking, potentially unsafe • const_cast – adds/removes const-ness, only safe if const-ness removed from non-constant (useful for non-const compliant API functions) old-style casting • newtype(expression) or (newtype)expression try static_cast, then dynamic_cast then reinterpret_cast – since devolve to reinterpret_cast, unsafe, avoid 5

Bridge Review • what is the difference between static_cast , dynamic_cast and reinterpret_cast? What does const_cast do? What is wrong with casting like int *(myptr) or (int *)myptr? • Why is Bridge design pattern used? what is handle, body? abstract/concrete? How does bridge use delegation? 6
- Slides: 6