Simple Factory mode:
① abstract base class: class definition abstract Some methods to implement in subclasses
② inherits from the abstract base class: Implementing an abstract method in a base class
③ Factory class: Used to instantiate objects
First look at a code:
<?php
Class calc{
/**
* Calculation results
*
* @param int|float $num 1
* @param int|float $num 2
* @param string $operator
* @return Int|float
*/
Public function Calculate ($num 1, $num 2, $operator) {
try {
$result = 0;
Switch ($operator) {
Case ' + ':
$result = $num 1+ $num 2;
Break
Case '-':
$result = $num 1-$num 2;
Break
Case ' * ':
$result = $num 1* $num 2;
Break
Case '/':
if ($num 2==0) {
throw new Exception ("divisor cannot be 0");
}
$result = $num 1/$num 2;
Break
}
return $result;
}catch (Exception $e) {
echo "You entered incorrectly:". $e->getmessage ();
}
}
}
$test =new Calc ();
echo $test->calculate (2,3, ' + ');//Print: 5
echo $test->calculate (5,0, '/');//print: You entered incorrectly: divisor cannot be 0
?>
Advantages: The above code uses the Object-oriented encapsulation feature, as long as the Include this class, other pages can be used casually
Disadvantages: Inability to extend and maintain flexibility
For example, if you want to add a "remainder" operation, you need a branch statement in the switch statement block, and the code needs a second case to judge
The following problems arise
① need to change the original code block, may be in order to "add new features" and change the original code, accidentally changed the original code wrong
② If you want to add a lot of functionality, for example: ' Power ', ' root ', ' log ', ' trigonometric ', ' statistics ', or add some programmer-specific computing functions, such as: and, or, not, Xor, so you need to add N branch statements in the switch statement. Imagine, a function of computing function if there are twenty or thirty case branch statements, the code will be more than one screen, not only the readability of the code is greatly reduced, the key is, in order to add small functionality, but also to allow the rest of the irrelevant to participate in the interpretation, which makes the implementation of the program efficiency greatly
Solution: Using Oop's inheritance and polymorphic thinking
<?php
/**
* Operation class
* Class must be declared abstract because it contains abstract methods
*/
Abstract class operation{
An abstract method cannot contain a function body
Abstract public Function GetValue ($num 1, $num 2);//strongly require subclasses to implement this function
}
/**
* Addition class
*/
Class Operationadd extends Operation {
Public Function GetValue ($num 1, $num 2) {
return $num 1+ $num 2;
}
}
/**
* Subtraction Class
*/
Class Operationsub extends Operation {
Public Function GetValue ($num 1, $num 2) {
Return $num 1-$num 2;
}
}
/**
* Multiplication Class
*/
Class Operationmul extends Operation {
Public Function GetValue ($num 1, $num 2) {
return $num 1* $num 2;
}
}
/**
* Division Class
*/
Class Operationdiv extends Operation {
Public Function GetValue ($num 1, $num 2) {
try {
if ($num 2==0) {
throw new Exception ("divisor cannot be 0");
}else {
return $num 1/$num 2;
}
}catch (Exception $e) {
echo "error message:". $e->getmessage ();
}
}
}
?>
<?php
/**
* Operation class
* Class must be declared abstract because it contains abstract methods
*/
Abstract class operation{
An abstract method cannot contain a function body
Abstract public Function GetValue ($num 1, $num 2);//strongly require subclasses to implement this function
}
/**
* Addition class
*/
Class Operationadd extends Operation {
Public Function GetValue ($num 1, $num 2) {
return $num 1+ $num 2;
}
}
/**
* Subtraction Class
*/
Class Operationsub extends Operation {
Public Function GetValue ($num 1, $num 2) {
Return $num 1-$num 2;
}
}
/**
* Multiplication Class
*/
Class Operationmul extends Operation {
Public Function GetValue ($num 1, $num 2) {
return $num 1* $num 2;
}
}
/**
* Division Class
*/
Class Operationdiv extends Operation {
Public Function GetValue ($num 1, $num 2) {
try {
if ($num 2==0) {
throw new Exception ("divisor cannot be 0");
}else {
return $num 1/$num 2;
}
}catch (Exception $e) {
echo "error message:". $e->getmessage ();
}
}
}
?>
This takes an object-oriented inheritance feature, first declaring a virtual base class, and specifying the method that subclasses must implement in the base class (GetValue ())
Analysis: By adopting the Object-oriented inheritance feature, we can easily extend the original program, such as: ' Powers ', ' square ', ' logarithm ', ' trigonometric ', ' statistics ' and so on.
We only need to write a separate class (this class inherits the virtual base class), completes the corresponding function in the class (for example: the computation of the exponentiation), and greatly reduces the coupling degree, facilitates the future maintenance and the extension.
There is still a problem unresolved, that is, how to get the program to instantiate the corresponding object based on the operator entered by the user?
Workaround: Use a separate class to implement the instantiation process, this class is the factory.
<?php
/**
* Engineering class, mainly used to create objects
* Function: According to the input operation symbol, the factory can instantiate the appropriate object
*
*/
Class factory{
public static function Createobj ($operate) {
Switch ($operate) {
Case ' + ':
return new Operationadd ();
Break
Case '-':
return new Operationsub ();
Break
Case ' * ':
return new Operationsub ();
Break
Case '/':
return new Operationdiv ();
Break
}
}
}
$test =factory::createobj ('/');
$result = $test->getvalue (23,0);
echo $result;
?>
My summary is that different factories (classes) are invoked depending on the parameters (objects) passed. It is then used to implement the specified functionality. The bigger benefit is simplifying the switch case,if else, which used to be functional programming, to distribute the classes, and to invoke different classes for different things.