the 5th advantage of a static factory is that when you write a class that contains the method, the class of the returned object does not need to exist. This flexible static factory approach forms the basis of the service provider framework, such as the Java Database Connection API (JDBC). The service provider framework is the system in which the provider implements the service, and the system makes the implementation available to the client, separating the client from the implementation.
There are three basic groups in the service provider framework: The service interface, which represents the implementation, the provider registration API, the provider used to register the implementation, and the service access API, which the client uses to obtain an instance of the service. The Service Access API allows customers to specify criteria for selecting implementations. In the absence of such a standard, the API returns an instance of the default implementation, or allows the customer to traverse through all the available implementations. The Service access API is a flexible, static factory that forms the basis of the service provider framework.
SPF includes the following components: (1) service interface, (2) Service provider registration interface, (3) Service Access interface, (4) Service Provider Interface (optional).
Take JDBC For example:
Driver: Service Provider Interface
Drivermanager.registerdriver (): Service Provider Registration interface
Drivermanager.getconnection (): Service provider
Connection (): Service interface
Transfer Service provider framework mode
Service Provider Framework Mode
Service Provider Frameworks -Provider framework: Multiple service providers implement a service that provides multiple implementations for the service provider's clients and decouples them from multiple implementations.
SPF includes the following components:
Components |
Description |
Services Interface (Service Interface) |
This is implemented by the provider |
Provider Registration API (Provider Registration API) |
This is the system used to register the implementation, allowing the client to access |
Services access API (service access API) |
Is the instance that the client uses to obtain the service. The Service access API generally allows but does not require the client to specify a condition for a selection provider, and if there is no such provision, the API returns an instance of the default implementation The Service access API is a "flexible static factory" that forms the basis of SPF |
Service Provider Interface (Provider Interface) |
The instance responsible for creating its service implementation |
Example: Spftest
As can be seen from the above example, we only need to enter a key to get the corresponding class instance, and then use the method of the class, this method can greatly reduce the coupling degree of code, here the map collection can be understood as the service provider framework of the registration mechanism, The value of the key and value in the map should be in the configuration of the property file (properties), so that we can easily view and add the service class, we just need to manipulate the configuration file, so our actual mode of implementation of the Java solution coupling problem.
The implementation classes of the service interface and the service provider interface are implemented by the service provider itself. In JDBC, for example, these 2 modules are implemented by a specific database provider.
Example: JDBC (Java database connection, Java DB Connectivity)
To load the driver:
1 Class.forName (Driverclass); 2//load MySQL driver 3 class.forname ("Com.mysql.jdbc.Driver"); 4//Load Oracle Driver 5 class.forname (" Oracle.jdbc.driver.OracleDriver ");
To obtain a database connection:
1 drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/fireway", "Root ", "root");
To create a Statement/preparedstatement object:
1 conn.createstatement (); 2 conn.preparestatement (SQL);
Let's take a look at the above statement to do something separately:
- Connection is the service interface of JDBC;
Class.forName("...")
Instantiates a Com.mysql.jdbc.Driver class (The implementation class of the service provider interface) and registers an instance of the class with the Drivermanager.registerdriver (provider registration API);
- Get the connection service linked to the MySQL database by establishing a connection URL, username, and password. Drivermanager.getconnection () is a service access API that returns a concrete implementation class object.
- Driver is the service provider interface.
Resources:
- Effective Java
- Service Provider Framework
- Introduction to the JAVA Service provider framework
- JDBC Usage Notes
One, (effectivejava3rd entry one). The 5th advantage of a static factory is that when you write a class that contains the method, the class of the returned object does not need to exist.