1 / 33

Pattern Bridge

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.

kimo
Download Presentation

Pattern Bridge

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pattern Bridge

  2. Definition • Bridge is the structural pattern that separates abstraction from the implementation so thatboth of them can be changed independently.

  3. 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.

  4. Reasons for the use • Then we define its successors and override the method “Draw” for each graphic library.

  5. 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

  6. 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

  7. 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.

  8. Class diagram

  9. 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”.

  10. 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”.

  11. Example 1 Consider the system of graphic primitives, which should not depend on the graphics library. Let define the interface of Implementor:

  12. Example 1 • Define the class “Shape”, which will be the base for all of graphic primitives:

  13. Example 1 • Class Line will be the following.

  14. Example 1 • Class Circle will be the following.

  15. Example 1 • Classes that implement the abstract methods of the class DrawingApican be the following:

  16. 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.

  17. Example 2 • Examples of possible variants of insert: • usual inserting • inserting of several rows by a single expression • updating

  18. Example 2

  19. 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.

  20. Pattern Strategy

  21. 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.

  22. 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.

  23. Class diagram

  24. 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.

  25. 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:

  26. Example 1 One of the possible implementations can be a strategy, which calculates the sum of investments based on the phases of the moon:

  27. Example 1 Another implementation strategy proposes to invest all funds that are on your Bank accounts:

  28. Example 1 A class that uses a strategy tells the user about the amount of investments:

  29. 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.

  30. Example 2 Class “Customer” provides an interface to obtain the data necessary to generate a report:

  31. Example 2 Concrete implementation of the strategy, which is responsible for displaying of the information in text form:

  32. Example 2 Another implementation strategy responsible for displaying information as HTML:

  33. Example 2 Class “Customer” implements the method of receipt of the report, delegating the work to the strategy:

More Related