Many design patterns encourage loose coupling in the design patterns book. To understand this concept, we 'd better talk about the hard work of many developers engaged in large-scale systems. A problem occurs when you change a code snippet. other parts of the system, which you once thought were completely unrelated, may also cause cascading damages.
Many design patterns encourage loose coupling in the design patterns book. To understand this concept, we 'd better talk about the hard work of many developers engaged in large-scale systems. A problem occurs when you change a code snippet. other parts of the system, which you once thought were completely unrelated, may also cause cascading damages.
This problem lies in tight coupling. Functions and classes in a certain part of the system depend heavily on the behavior and structure of functions and classes in other parts of the system. You need a set of modes to enable these classes to communicate with each other, but do not want to closely bind them together to avoid interlocking.
In large systems, many codes depend on a few key classes. It may be difficult to change these classes. For example, assume that you have a User class to read from a file. You want to change it to other classes read from the database, but all code references the original class read from the file. At this time, it is very convenient to use the factory model.
Factory mode is a type that provides methods for creating objects for you. You can use the factory class to create objects instead of using new directly. In this way, if you want to change the type of the created object, you only need to change the factory. All codes used in the factory are automatically changed.
Example 1: Display a display column of the factory class. The server side of the equation consists of a database and a set of PHP pages that allow you to add feedback, request feedback lists, and obtain articles related to specific feedback.
- interface IUser
- {
- function getName();
- }
- class User implements IUser
- {
- public function __construct( $id ) { }
- public function getName()
- {
- return "Jack";
- }
- }
- class UserFactory
- {
- public static function Create( $id )
- {
- return new User( $id );
- }
- }
- $uo = UserFactory::Create( 1 );
- echo( $uo->getName()."\n" );
- ?>
The IUser interface defines what operations a user object should perform. The implementation of IUser is called User, and the UserFactory class creates an IUser object. This relationship can be expressed in UML.
Figure 1. factory class and related IUser interface and user class
If you use the php interpreter to run this code on the command line, the following result is displayed:
- % php factory1.php
- Jack
- %
The test code will request the User object to the factory and output the result of the getName method.
There is a variation in the factory model that uses the Factory method. Class. This method is useful when you create an object of this type. For example, suppose you need to create an object first and then set many attributes. The factory mode of this version encapsulates the process in a single location, so that you do not need to copy complicated initialization code or paste the copied code everywhere in the code base.
Example 2 shows an example using the factory method.
- interface IUser
- {
- function getName();
- }
- class User implements IUser
- {
- public static function Load( $id )
- {
- return new User( $id );
- }
- public static function Create( )
- {
- return new User( null );
- }
- public function __construct( $id ) { }
- public function getName()
- {
- return "Jack";
- }
- }
- $uo = User::Load( 1 );
- echo( $uo->getName()."\n" );
- ?>
This code is much simpler. It only has one IUser interface and one User class that implements this interface. The User class has two static methods for creating objects. This link can be expressed in UML in figure 2.
Figure 2. IUser interface and user class with factory method
The result of running the script in the command line is the same as that in listing 1, as shown below:
- % php factory2.php
- Jack
- %
As mentioned above, sometimes such models seem to be a little useful in a small environment. However, it is best to learn this solid coding format to apply it to any scale of projects.