You are on page 1of 17

Bridge Pattern

• The Bridge design pattern decouples an abstraction from its


implementation so that the two can vary independently. It acts as an
intermediary between two components.

• GoF's definition, this pattern "separates an object's interface from its


implementation".
Adapter Vs Bridge
• Adapter focuses on resolving incompatibilities between two existing
interfaces. It doesn't focus on how those interfaces are implemented,
nor does it consider how they might evolve independently. It's a way
of making two independently designed classes work together without
reimplementing one or the other.

• Bridge, on the other hand, bridges abstraction and its (potentially


numerous) implementations. It provides a stable interface to clients
even as it lets you vary the classes that implement it. It also
accommodates new implementations as the system evolves.
•Abstraction
•defines the abstraction's interface.
•maintains a reference to an object of type Implementor.
•RefinedAbstraction
•extends the interface defined by Abstraction.
•Implementor
•defines the interface for implementation classes. This interface doesn't
have to correspond exactly to Abstraction's interface; in fact the two
interfaces can be quite different. Typically the Implementation interface
provides only primitive operations, and Abstraction defines higher-level
operations based on these primitives.
•ConcreteImplementor
•implements the Implementor interface and defines its concrete
implementation.
• A system that is capable of sending Emails and SMSs using a web service created
by you. An interface that will have a method declaration, to send the email and
SMS, is implemented by the Email and the SMS concrete classes. Sending the
SMS and Email notifications to users is our business logic and sending it through a
Web service, is one of the many different ways to do it.
Consider a case where you are required to add the ability to send the Emails and SMS's using a third-party API.
Then the code becomes complex and we have to add more classes. So our first step will be to define the the
bridge abstraction and the logic abstraction classes. Our logic abstraction class will have a reference to the
abstract bridge. Next, we will implement the abstract logic and call the bridge method, using its reference, that
these concrete logic based classes hold withing them
• Next we will be have multiple implementations of the bridge, that is nothing but different
ways in which we can send these user notifications. So this is nothing but another
implementation of the interface, but this time its the Bridge interface.
Flyweight
• Flyweight Design pattern falls under Structural Pattern.
• Flyweight pattern tries to reuse already existing similar kind objects
by storing them and creating a new object when no matching object
is found.
• When to use it?
1.Flyweight is used when there is a need to create a large number
of objects of almost similar nature and storage cost is high.
2.A few shared objects can replace many unshared ones.
3.Most of the state can be kept on disk or calculated at runtime.
• Flyweight pattern is used to reduce the number of objects
created, to decrease memory and resource usage. As a result, it
increases performance.
• Flyweight pattern tries to reuse already existing similar kind
objects by storing them and creates a new object when no
matching object is found.
Flyweight
This is an interface that defines the members of the flyweight objects.

ConcreteFlyweight
This is a class that Inherits from the Flyweight class.

UnsharedFlyweight
This is a class that Inherits from the Flyweight class and enables sharing of
information, it is possible to create instances of concrete flyweight classes that
are not shared.

FlyweightFactory
This is a class that holds the references of already created flyweight objects.
When the GetFlyweight method is called from client code, these references are
checked to determine if an appropriate flyweight object is already present or
not. If present, it is returned. Otherwise, a new object is generated, added to
the collection and returned.
1.ShapeObjectFactory- FlyweightFactory class.
2.IShape - Flyweight interface.
3.Circle & Rectangle - ConcreteFlyweight class.

You might also like