Php design pattern description value object pattern Page 1/5

Source: Internet
Author: User
In all the simplest programs, most objects have an identifier. an important commercial application object, such as a Customer or a SKU, has one or more attributes-id, name, email address, which can be distinguished from other instances of the same class. In addition, an object has a constant identifier: it is a unique identifier throughout the application. for programmers, "mermera" is "mermera" anywhere ", and as long as your program continues to run & quot; customerA & quot; Is Still & quot; customerA & quot ;. However, an object is not an example. generally, an object is used to describe a date, number, or currency. Date, integer, or dollar class definitions are easy to use, fast, and easy to encapsulate, and easy to copy, compare, or even create.

On the surface, objects with simple descriptions can be easily executed: they have very few statements, and they are no different in constructing classes, whether applied to Customer or SKU. This idea seems to be correct, but the so-called "seemingly correct" can easily produce some bugs.

Please refer to the following code. this is a definition and execution of the object that grants wages to employees in USD. In most cases, it runs normally. (This class is named BadDollar because it still has bugs ). Consider whether you can discover its bugs.

// PHP5
Class BadDollar {
Protected $ amount;
Public function _ construct ($ amount = 0 ){
$ This-> amount = (float) $ amount;
}
Public function getAmount (){
Return $ this-> amount;
}
Public function add ($ dollar ){
$ This-> amount + = $ dollar-> getAmount ();
}
}
Class Work {
Protected $ salary; public function _ construct (){
$ This-> salary = new BadDollar (200 );}
Public function payDay (){
Return $ this-> salary;
}
}
Class Person {
Public $ wallet;
}
Function testBadDollarWorking (){
$ Job = new Work;
$ P1 = new Person;
$ P2 = new Person;
$ P1-> wallet = $ job-> payDay ();
$ This-> assertEqual (200, $ p1-> wallet-> getAmount ());
$ P2-> wallet = $ job-> payDay ();
$ This-> assertEqual (200, $ p2-> wallet-> getAmount ());
$ P1-> wallet-> add ($ job-> payDay ());
$ This-> assertEqual (400, $ p1-> wallet-> getAmount ());
// This is bad-actually 400
$ This-> assertEqual (200, $ p2-> wallet-> getAmount ());
// This is really bad-actually 400
$ This-> assertEqual (200, $ job-> payDay ()-> getAmount ());
}
So what is a bug? If you cannot find the problem in the preceding code example, the following message is displayed: The Employee object $ p1 and object $ p2 use the same BadDollar object instance.

First, instances of class Work and class Person have been created. Assume that each employee initially has an empty e-wallet. the employee's e-wallet Person: wallet is assigned a value through the object resource variable returned by the Work: payDay () function, so it is set as an object instance of the BadDollar class.

Do you still remember the PHP 5 object assignment processing method? Because of the processing method of PHP5 object assignment, $ job: salary, $ p1: wallet and $ p2 :: although the three different object instances of wallet use different "identifiers", they all specify the same object instance.

Therefore, for the next pay-as-you-go operation (PayDay indicates the pay-as-you-go Day, which indicates the pay-as-you-go action), use $ job-> payDay () I only wanted to increase the salary of $ P1, but unexpectedly paid $ P2. In addition, this operation also changes the amount of basic salary for the job. Therefore, an error is reported when detecting the last two values.

Value Object PHP5 Unit Test
1) Equal expectation fails because [Integer: 200] differs from [Float: 400] by 200
In testBadDollarWorking
In ValueObjTestCase
2) Equal expectation fails because [Integer: 200] differs from [Float: 400] by 200
In testBadDollarWorking
In ValueObjTestCase
FAILURES !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.