Home php教程 php手册 基于php设计模式中工厂模式详细介绍

基于php设计模式中工厂模式详细介绍

Jun 13, 2016 am 11:52 AM
php introduce out create parameter based on factory model kind Design Patterns detailed

工厂模式:由工厂类根据参数来决定创建出哪一种产片类的实例
工厂类:一个专门用来创建其他对象的方法类。即按需分配,传入参数进行选择,返回具体的类
作用:对象创建的封装、简化创建对象的操作,即调用工厂类的一个方法来得到需要的类
补充:
1.主要角色
:抽象产品(Product)、具体产品(Concrete Product)、抽象工厂角色(Creator)
2.优缺点
    优点:工厂方法模式可以允许系统在不修改工厂角色的情况下引进心产品
    缺点:客户可能仅仅为了创建一个特定的Concrete Product对象,就不得不创建一个Creator子类
3.适用性
    当一个类不知道它所必须创建的对象的时候
    当一个类希望由它的子类来制定它所创建的对象的时候
    当一个类将创建对象的职责委托给多个帮助子类的某一个,并且希望你将哪一个帮助子类是代理这一信息局部化的时候

复制代码 代码如下:


//对象
class MyObject{
    public function __construct(){}
    public function test(){
        return 'test';
    }
}
//工厂
class MyFactory{
    public static function factory(){
        return new MyObject();
    }
}

$myObject = MyFactory::factory();
echo $myObject->test();
?>
 

?//抽象类 定义属性及抽象方法
abstract class Operation{
    protected $_NumberA = 0;
    protected $_NumberB = 0;
    protected $_Result= 0;

    public function __construct($A,$B){
        $this->_NumberA = $A;
        $this->_NumberB = $B;
    }

    public function setNumber($A,$B){
        $this->_NumberA = $A;
        $this->_NumberB = $B;
    }

    public function clearResult(){
        $this->_Result = 0;
    }

    abstract protected function getResult();
}

//操作类
class OperationAdd extends Operation{
    public function getResult(){
        $this->_Result = $this->_NumbserA + $this->_NumberB;
        return $this->_Result;
    }
}

class OperationSub extends Operation{
    public function getResult(){
        $this->_Result = $this->_NumberA - $this->_NumberB;
        return $this->_Result;
    }
}
…………

//工厂类
class OperationFactory{
    private static $obj;

    public static function CreationOperation($type,$A,$B){
        switch($type){
            case '+':
                self::$obj = new OperationAdd($A,$B);
                break;
            case '-':
                self::$obj = new OperationSub($A,$B);
                break;
            ……
        }
    }
}

//操作
$obj = OperationFactory:: CreationOperation('+',5,6);
echo $obj-> getResult();
?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles