1. Bridge --- JDBC driverProgram
JDBC is the application programming interface used to execute SQL statements. The JDBC driver is the class that implements this interface. The database application is the abstraction of database operations, and it depends on the JDBC driver; as long as the JDBC driver is provided, the database application can operate any database. This architecture of JDBC separates abstraction from specific implementations, so that database applications and JDBC drivers can develop independently.
2. factorymethod
Figure 1 shows the structure of the factory method mode. Some terms are provided here to facilitate the description:
- Product: the abstract class of the product to be created.
- Concreteproduct: a subclass of product, a series of specific products.
- Creator: Specifies the abstract Creator interface, which declares that the factory method of the product type object is returned.
- Concretecreator: Specifies the creator. Override the factory method in the Creator and return the concreteproduct type instance.
From this we can clearly see the parallel correspondence: Product <===> creator; conreteproduct <==> conretecreator
Abstract products correspond to abstract creators, and specific products correspond to specific creators. What are the benefits of doing so? Why don't we directly use specific products and specific creators to fulfill our requirements? We can also do this. however, in the factory method mode, the customer (client) only needs to reference the abstract product and creater. It does not care about the specific concreteproduct and concretecreator. In this way, we can gain additional benefits:
- First, the client can obtain the generated instances from the abstract creator. The role of the Creator separates the client from the product creation process. The customer does not have to worry about returning the specific product, you don't have to worry about how these products are created. at the same time, concreteproduct is also hidden behind the product. conreteproduct inherits all the attributes of the product and implements the abstract methods defined in the product. According to the cast principle in Java, the concreteproduct generated by concretecreator can be automatically scaled to a product. in this way, the concreteproduct with different substantive content can be unified as the product in form and accessed through the Creator provided to the client.
- Secondly, when we add a new concretecreator, because the interface provided by the Creator remains unchanged, the client program will not be changed at all, and will not cause a disaster that will take the whole body as soon as possible, this is the embodiment of good encapsulation. however, if you directly use the concreteproduct and concretecreator classes, you cannot do this in any way. the excellent object-oriented design encourages the use of encapsulation and delegation, while the factory method mode is a typical example of using encapsulation and delegation, here, the encapsulation is embodied by the abstract creator, and the delegate is to assign the responsibility for creating objects to the specific creator concretecreator.