The registration mode of the introduction of PHP design pattern

Source: Internet
Author: User

We generally consider it a good choice to avoid using global variables, so objects are often passed as arguments from one piece of code to another. But one problem with passing instances is that objects sometimes don't know who to pass to ——? pass a function before being passed to a function that really needs that object.

In order to write, read, and modify the code easily, it is best to reduce the number of different objects, and can be a large number of widely used objects uniformly expressed as a single, commonly used objects.

Problem:

How do you get references to other objects through a single global object?

Solution:

"Registration Mode" is like "the phone book of an object"--stores and can retrieve a register of references to objects. (Note: the "federated Array" in PHP also functions like a phone book.) In fact, the "registration mode" is done around a powerful array of PHP. Some of the features of the registration model are often included in "single Mode" (see chap. Fourth), making "registration mode" the definitive source of your entire application information.

Note: the "registration Mode" class mainly refers to Martin Fowlerdescribes's Patterns of EntERPrise application Architecture (Enterprise Application architecture model) implemented in the Java language. Marcus Baker Thanks a detailed article in PHP that applies the "registration mode". This article can be obtained at the phppatterns.com site (http://www.PHPpatterns.com/index.PHP/article/articleview/75/1/1/). Baker also involved a number of test considerations, demonstrating the test-driven development approach.

Sample code:

As Martin flower The sample code mentioned in his "Registration mode" article, you can provide various interfaces to implement the "registration mode" in various ways. Let's delve into this idea and establish some different implementations of the "registration model" in PHP4.

Let's start by writing code that stores and restores object instances and provides global access to the registration mode. An instance variable of this class can cache an object, and the "registration mode" itself is a "single mode". As before, testing determines demand. Our first test is to determine that "registration mode" is a "single piece Mode" Class.

PHP4

class ReGIStryPHP4TestCase extends UnitTestCase {
function testRegistryIsSingleton() {
$this->assertIsA($reg =& Registry::getInstance(), ‘Registry’);
$this->assertReference($reg, Registry::getInstance());
}
}

Here, to use the knowledge you have learned in the previous chapters "monolithic mode," you should be able to quickly write out the classes that pass the test. The following is a "registration mode" class (ignoring the code required to enforce no direct object creation) that satisfies the test requirements:

class Registry {
function &getInstance() {
static $instance = array();
if (!$instance) $instance[0] =& new Registry;
return $instance [0];
}
}

A simple static array is sufficient to record this single instance.

Next, let's go to the unique feature of the registration mode. A "registration mode" should provide a get () and set () method to store and obtain objects (with some property keys) and should also provide a isvalid () method to determine whether a given property has been set.

A simple implementation of these three methods is discussed next. Here are two IsValid (): Methods for testing methods.

Code:

class RegistryPHP4TestCase extends UnitTestCase
{function testRegistryIsSingleton() { /*...*/ }
function testEmptyRegistryKeyIsInvalid()
{$reg =& Registry::getInstance();
$this- >assertFalse($reg->isValid('key'));
}
function testEmptyRegistryKeyReturnsNull()
{$reg =& Registry::getInstance();
$this- >assertNull($reg->get('key'));
}
}

Author Note: Assertfalse ()

Assertfalse () is just the opposite of Asserttrue (), and if the first argument is expected to be a Boolean value in PHP false, the test passes.

With a test-driven development approach, you can write as few code as possible to meet your current test requirements, and you can add tests-if you haven't yet met the needs of this class.

The following are the simplest code to satisfy the aforementioned test requirements:

Code:

class ReGIStry
{function isValid() {return false;}
function get() {}
function &getInstance()
{static $instance = array();
if (!$instance) $instance[0] =& new Registry;
return $instance[0];
}
}

Indeed, the code snippets for the IsValid () and get () methods were not very good, but all the tests passed! Here we add a richer test case.

Code:

class RegistryPHP4TestCase extends UnitTestCase
{function testRegistryIsSingleton() { /*...*/ }
function testEmptyRegistryKeyIsInvalid() { /*...*/ }
function testEmptyRegistryKeyReturnsNull() { /*...*/ }
function testSetRegistryKeyBecomesValid ()
{$reg =& Registry::getInstance();
$test_value = 'something';$reg- >set('key', $test_value);
$this->assertTrue($reg->isValid ('key'));
}
}

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.