Design Pattern Bridge Design mode, patternbridge
The bridge design pattern is actually a simple has a relationship, that is, one class has another class and uses another class to implement the required functions.
For example, you can use the bridge design mode between the remote control and the TV to control multiple TVs using the same remote control.
Such a design idea is the basic idea of using multiple design patterns in turn.
After careful consideration, we will find that the underlying ideas of multiple design modes are actually the same, but there are some differences in implementation or some details and applications.
Next we will implement a TV and remoter class, in which remoter can be changed at any time.
#include <stdio.h>class Remoter{public:virtual void changeChannel() = 0;};class OldRemoter : public Remoter{short channel;public:OldRemoter(short c) : channel(c) {}void changeChannel(){printf("Channel : %d\n", channel++);}};class NewRemoter : public Remoter{int channel;public:NewRemoter(int c) : channel(c) {}void changeChannel(){printf("Channel : %d\n", channel++);}};class TV{protected:Remoter *remoter;int channel;public:TV(Remoter *r) : remoter(r), channel(0) {}virtual void changeRemoter(Remoter *r){remoter = r;}virtual void changeChannel(){remoter->changeChannel();}};class BrandOneTV : public TV{public:BrandOneTV(Remoter *r) : TV(r){}};int main(){Remoter *ore = new OldRemoter(0);Remoter *nre = new NewRemoter(1);TV *tv1 = new BrandOneTV(ore);tv1->changeChannel();ore->changeChannel();tv1->changeChannel();tv1->changeRemoter(nre);tv1->changeChannel();nre->changeChannel();tv1->changeChannel();return 0;}
Run:
What are the design patterns?
There are three types of design patterns: creation, structure, and behavior.
The creation types include:
I. Singleton, Singleton mode: ensure that a class has only one instance and provide a global access point to it.
2. Abstract Factory: provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
3. Factory Method: Define an interface used to create objects, and let the subclass decide which class to instantiate. Factory Method delays the instantiation of a class to the subclass.
4. Builder: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
5. Prototype: Use a Prototype instance to specify the type of the object to be created, and copy the Prototype to create a new object.
Behavior types:
6. Iterator: provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.
7. Observer: Observer mode: defines one-to-many dependencies between objects. When the status of an object changes, all objects dependent on it will be automatically updated by notification.
8. Template Method: defines the skeleton of an algorithm in an operation, and delays some steps to the subclass, templateMethod allows the subclass to redefine a specific step without changing the structure of an algorithm.
9. Command: encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests and record request logs, and supports unrecoverable operations.
10. State: allows an object to change its behavior when its internal State changes. The object seems to have changed its class.
11. Strategy: Define a series of algorithms, encapsulate them one by one, and enable them to replace each other. This mode allows algorithms to be independent of customers who use them.
12. China of Responsibility, Responsibility chain mode: Enables multiple objects to process requests to avoid coupling between the request sender and receiver.
13. Mediator: uses an intermediary object to encapsulate object interaction of some columns.
14. Visitor, Visitor mode: indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on this element without changing the element classes.
15th, Interpreter, Interpreter mode: a language is defined to define a representation of its grammar and an Interpreter. This Interpreter uses this representation to explain sentences in the language.
16. Memento: capture the internal state of an object without interrupting the object, and save the state outside the object.
There are:
17. Composite: Composite combines objects into a tree structure to represent the relationship between parts of the whole. Composite makes the use of a single object and a Composite object consistent.
18. Facade, appearance mode: provides a consistent interface for a group of interfaces in the subsystem. fa? Ade provides a high-level interface, which makes the subsystem easier to use.
19. Proxy: provides a Proxy for other objects to control access to this object.
20. Adapter: the Adapter mode converts a class of interfaces into another interface that the customer wants. The Adapter mode makes those classes unable to work together due to interface incompatibility.
21. Decrator: the Decorator mode dynamically adds some additional responsibilities to an object. In terms of the added functions, the Decorator mode is more flexible than the subclass generation mode.
22. Bridge: link the abstract Part with its implementation... the remaining full text>
What is the most widely used design pattern in the MVC pattern? A singleton B Observer (Observer) C Proxy (Proxy) D Bridge (Bridge)
Observer B (Observer)
==================================
Relationship between observer mode and MVC mode:
1. MVC is an architecture model. An architecture model describes the basic structure or outline of a software system. In the MVC mode, there are three roles: model, view, and controller ).
2. The observer mode can be used to implement the MVC mode. In observer mode, the topic role is the model role in MVC mode and the Controller role. The observer role is the view role in MVC mode.
Hope to help you. Pai_^