PHP design mode builder mode and adapter (adapter mode)

Source: Internet
Author: User
Tags php tutorial

PHP Tutorial Design mode builder mode and adapter (adapter mode)

Adapter mode
*
* Convert the interface of a class to another interface that the customer expects, and those classes that are not compatible to work together can work together

Builder model
*
* Separate the build of a complex object from its representation, and use the same build process to create different representations

<?php
/**
* Adapter Mode
*
* Convert the interface of a class to another interface that the customer expects, and those classes that are not compatible to work together can work together
*/

This is the original type.
Class Oldcache
{
Public Function __construct ()
{
echo "Oldcache construct<br/>";
}

Public function Store ($key, $value)
{
echo "Oldcache store<br/>";
}

Public function Remove ($key)
{
echo "Oldcache remove<br/>";
}

Public function Fetch ($KEY)
{
echo "Oldcache fetch<br/>";
}
}

Interface cacheable
{
Public function set ($key, $value);
Public function get ($key);
Public Function del ($key);
}

Class Oldcacheadapter implements Cacheable
{
Private $_cache = null;
Public Function __construct ()
{
$this->_cache = new Oldcache ();
}

Public function set ($key, $value)
{
return $this->_cache->store ($key, $value);
}

Public function Get ($key)
{
return $this->_cache->fetch ($key);
}

Public Function del ($key)
{
return $this->_cache->remove ($key);
}
}

$objCache = new Oldcacheadapter ();
$objCache->set ("test", 1);
$objCache->get ("test");
$objCache->del ("test", 1);

PHP design mode Builder (Builder mode)

<?php

/**
* Builders ' model
*
* Separate the build of a complex object from its representation, and use the same build process to create different representations
*/
Class Product
{
public $_type = null;
public $_size = null;
public $_color = null;

Public Function SetType ($type)
{
echo "Set product type<br/>";
$this->_type = $type;
}

Public Function setSize ($size)
{
echo "Set product size<br/>";
$this->_size = $size;
}

Public Function SetColor ($color)
{
echo "Set product color<br/>";
$this->_color = $color;
}
}

$config = Array (
"Type" => "shirt",
"Size" => "XL",
"Color" => "red",
);

No previous processing using Bulider
$oProduct = new Product ();
$oProduct->settype ($config [' type ']);
$oProduct->setsize ($config [' size ']);
$oProduct->setcolor ($config [' Color ']);


Create a builder class
Class Productbuilder
{
var $_config = null;
var $_object = null;

Public Function Productbuilder ($config)
{
$this->_object = new Product ();
$this->_config = $config;
}

Public Function Build ()
{
echo "---in builder---<br/>";
$this->_object->settype ($this->_config[' type '));
$this->_object->setsize ($this->_config[' size '));
$this->_object->setcolor ($this->_config[' color ');
}

Public Function getproduct ()
{
return $this->_object;
}
}

$objBuilder = new Productbuilder ($config);
$objBuilder->build ();
$objProduct = $objBuilder->getproduct ();

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.