question:
So, how do you define an efficient class for simple applications like Date or Dollar, and it is easy to create.
Solution:
Efficient objects should work like PHP integers: if you assign the same object resource to two different variables and then change one of them, the other variable is still unaffected. In fact, this is the goal of the Value Object pattern.
When executing Value Object, there is a difference between php4 and php5.
As you can see above, PHP5 uses new to transfer object resources to transfer pointers to object resources, just like we pass pointers in PHP4. Obviously, this is a problem. To solve that problem and implement the value of a proprietary object Dollar, we must make a value of all properties of the object of property $amount in general immutable or unchangeable. However, in the case where the PHP language does not provide the function of unchangeable parameters, you can fully combine the visibility of attributes with the acquisition and setting methods to achieve it.
On the contrary, all objects in PHP4 operates follow the rules of Value Objects, because the assignment operation of PHP4 is equivalent to making a copy of the object. Therefore, in order to implement the Value Objects design pattern in PHP4, you need to break the habit of creating, passing and extracting objects through pointer assignment that you have carefully cultivated.
Note: The term Immutable:
In the dictionary, the definition of Immutable is not allowed or is not easily affected. In programming, this term refers to a value that cannot be changed once it is set.
PHP5 sample code:
Now that we have started writing code in PHP5, let's optimize an instance of PHP5's Value Object and create a better Dollar class definition. Naming is very important in object-oriented programming. Choosing a unique currency type as the name of this class means that it is not defined as a class that can handle multiple currency types.
class Dollar {
protected $amount;
public function __construct($amount=0) {
$this->amount = (float)$amount;
}
public function getAmount() {
return $this->amount;
}
public function add($dollar) {
return new Dollar($this->amount + $dollar->getAmount());
}
}
If the properties in the class are prefixed with protected prefix, other classes cannot access it. protected (and private) refuses to be accessed directly through attributes.
Usually, when you use object-oriented programming, you often need to create a "setter" function, similar to:
public setAmount($amount)
{
$this->amount=$amount;
}
Similarly, in this case, although the function Dollar::amount() is not set, the parameter Dollar::amount has been assigned during the instantiation period of the object. The function Dollar::getAmount() only provides a function to access Dollar attributes, and the data type accessed here is floating point type.
The most interesting change is in the Dollar::add() method function. Instead of directly changing the value of the $this->amount variable to change the existing Dollar object instance, it creates and returns a new Dollar instance. Now, although you specify the current object to multiple variables, the change in each variable will not affect other variable instances.
The invariance of the value design pattern is key. Any change to a value of the variable amount of a Value Object is done by creating a new instance of a class with different expected values. The value of the initial $this->amount variable raised above has never changed.
Simply put, when using the value design model in PHP5, you need to pay attention to the following aspects:
Protect the properties of the value object and prohibit direct access.
Assign attributes in the constructor.
Remove any setter that will change the property value, otherwise the property value will be easily changed.
The above three steps create an unchanged value, which cannot be changed once it is initialized and set. Of course, you should also provide a method to view functions or access the properties of Value Object, and you can add some functions related to this class. Value objects are not only used on a simple architecture, they can also implement important business logic applications. Let's look at the next example:
Previous page12345Next pageRead the full text