330 likes | 579 Views
Pattern Bridge. Definition. Bridge is the structural pattern that separates abstraction from the implementation so that both of them can be changed independently. Reasons for the use. Let us have a task to develop a set of graphic primitives.
E N D
Definition • Bridge is the structural pattern that separates abstraction from the implementation so thatboth of them can be changed independently.
Reasons for the use • Let us have a task to develop a set of graphic primitives. • Our system must support different graphics library (OpenGL, DirectX, etc). • Let us declare the entity of the first graphic primitive.
Reasons for the use • Then we define its successors and override the method “Draw” for each graphic library.
Reasons for the use • Wedo the same while adding a new graphical primitive. Result: The number of classes = number of primitives * number of graphics libraries
Reasons for the use • The pattern of the bridge is intended to separate the implementation (graphics library) from abstraction (graphics primitive): Result: The number of classes = number of primitives + number of graphics libraries
Motivation • You want to avoid binding abstraction and implementation. • You must select the specific implementation during the program execution. • Both abstraction and implementation should be expanded with new subclasses. • Changes in the implementation should not influence on clients. • You want to share the same implementation between several objects, and this fact must be hidden from the client.
Participants • Abstraction defines the interface of abstraction and stores a reference to the object of a type “Implement”. • RefinedAbstractionis the refined abstraction. A more specific subclass of the main abstraction. • Implementor is the executor, it defines the interface for classes of implementation. It is not obliged to exactly match the interface “Abstraction”, moreover both interfaces can be completely different. Usually the interface of the class “Implementor” provides only primitive operations, and the class “Abstraction” defines the operations of a higher level based on these primitives. • ConcreteImplementor- one of the concrete executors. It contains concrete implementation of the interface of the class “Implementor”, it has its own implementation of the primitives defined in “Implementor”.
Advantages • The separation of implementation from interface. The implementation can be configured at run time. The separation of the classes “Abstraction” and “Implementor” removes depending on the implementation, which are set on the stage of compilation. To change the implementation class, now you don't need to recompile the class Abstraction and its clients. • Increase of a degree of extensibility. The classes “Abstraction” and “Implementor” can be extended independently. • Hiding the implementation details from clients. Clients using the interface “Abstraction” are isolated from the implementation details of these operations, which actually run through calls to methods of the “Implementor”.
Example 1 Consider the system of graphic primitives, which should not depend on the graphics library. Let define the interface of Implementor:
Example 1 • Define the class “Shape”, which will be the base for all of graphic primitives:
Example 1 • Class Line will be the following.
Example 1 • Class Circle will be the following.
Example 1 • Classes that implement the abstract methods of the class DrawingApican be the following:
Example 2 • Consider the example of inputting data to the database, for example, restoring data frombackup storage. • There are several ways to input data to the database: • using regular expressions “insert”, • using a single expression for inserting of several rows, • embedding data “update”. • In addition the system design must take into account the peculiarities of representation of data for each database server.
Example 2 • Examples of possible variants of insert: • usual inserting • inserting of several rows by a single expression • updating
Example 2 Participants: • DataDumperis the class which represents the interface to transfer the data. • InsertDataDumper, MultiInsertDataDumper, UpsertDataDumperare the classes which transfer the data. • ExtractImp is the class that encapsulates knowledge about the peculiarities of representation of the data in any database server. • MySqlExtractImp, SqlServerExtractImpare the classes which implement the logic of data representation for each individual server.
Definition and motivation • Strategy is the pattern of objects’ behavior, they encapsulate some algorithms. • Motivation: • There are a lot of related classes, differing only by the behavior. • You need to have several variants of the algorithm. For example, you can define two possible variants of the algorithm, one of which requires more time, and the other requires more memory. • The algorithm contains data, which the client must not know. • Many behaviors are defined in a class, they are presented with conditional operators.
Advantages • Collection of related algorithms. Strategy defines a collection of algorithms or behavior which can be reused in different contexts and applications. • Alternative generation of subclasses. The use of strategies allows to encapsulate the changing behavior in a separate class. It eliminates the need for generation of subclasses for behavioral changes. • Eliminating from a series of conditional statements. When a variety of behaviors are placed in one class, it is difficult to choose the right behavior without conditional operators. Encapsulation of each of behavior in a separate class “Strategy” solves this problem. • The choice of implementation. Strategies may offer different implementation of the same behavior. The client can choose an appropriate strategy according to his need for speed and memory.
Participants • Strategyannounces a common interface for all supported strategies. The class “Context” uses this interface to call a specific algorithm defined in the class “ConcreteStrategy”. • ConcreteStrategyis the specific strategy. Implements an algorithm that uses the interface,declared in the class Strategy. • Context is configured by the object ConcreteStrategy. Stores a reference to an object of class Strategy. May define an interface, which allows the object Strategy to get access to the data context.
Example 1 • Consider an application that allows you to calculate the amount of investments in a certain project in different ways. • Define the interface of the strategy, which calculates the amount to be invested:
Example 1 One of the possible implementations can be a strategy, which calculates the sum of investments based on the phases of the moon:
Example 1 Another implementation strategy proposes to invest all funds that are on your Bank accounts:
Example 1 A class that uses a strategy tells the user about the amount of investments:
Example 2 Consider a class which displays for the customer a report about rented movies, the total debt and the number of points for activity.
Example 2 Class “Customer” provides an interface to obtain the data necessary to generate a report:
Example 2 Concrete implementation of the strategy, which is responsible for displaying of the information in text form:
Example 2 Another implementation strategy responsible for displaying information as HTML:
Example 2 Class “Customer” implements the method of receipt of the report, delegating the work to the strategy: