Home Backend Development PHP Tutorial PHP implementation of simple factory pattern c# simple factory pattern factory pattern java java simple factory pattern example

PHP implementation of simple factory pattern c# simple factory pattern factory pattern java java simple factory pattern example

Jul 29, 2016 am 08:54 AM
Simple factory pattern

	简单工厂模式又叫静态工厂方法模式,主要作用是通过一个简单工厂类来实例化(创建)各个类的对象,而不需要通过new来实例化对象。优点在于,工厂类中包含了一定的逻辑判断,会根据客户端的选择条件动态实例化相关的类。缺点在于,当需要增加新的功能类时,需要去修改工厂类。

以下内容以一个简单的计算器程序作为案例分析。第一步,定义Operation,是一个父类,有两个属性,表示用于计算的两个参数。

<?php
/*
* 计算类
*/
class Operation{
	private $numA=0;
	private $numB=0;
	public function setNumA($numA)
	{
		$this->numA=$numA;
	}

	public function getNumA()
	{
		return $this->numA;
	}

	public function setNumB($numB)
	{
		$this->numB=$numB;
	}

	public function getNumB()
	{
		return $this->numB;
	}
}
?>
Copy after login

The second step is to define an interface, which declares the method to implement the operation

<?php
/*
*工厂接口
*/
interface InterOperate{
	function getResult();
}
?>
Copy after login

The third step is an addition operation class (omitting the subtraction class, multiplication class, trigger class, etc.)

<?php

/**
* 加法运算类
*/
include_once "IOperate.php";
include_once &#39;Operation.php&#39;;
class OperationAdd extends Operation implements InterOperate
{
	function getResult()
	{
		$result=$this->getNumA()+$this->getNumB();
		return $result;
	}
}

?>
Copy after login

Finally, define a simple factory class for creating object instances of various classes. Usually the objects returned by simple factory classes have a common parent class. In this example, the common parent class is the Operation class, and the addition class and the subtraction class are both subclasses of Operation.

<?php
include_once "OperationAdd.php";
include_once "OperationMinus.php";
class SimpleFactory {
	static function createAdd()
	{
		return new OperationAdd;
	}

	static function createMinus()
	{
		return new OperationMinus;
	}
}

?>
Copy after login

The client code is as follows:

<?php
/*
*	客户端代码
*/
include_once "OperationAdd.php";
include_once &#39;Operation.php&#39;;
include_once &#39;SimpleFactory.php&#39;;

$op=SimpleFactory::createAdd();
$op->setNumA(2);
$op->setNumB(4);
echo $op->getResult();

$om=SimpleFactory::createMinus();
$om->setNumA(45);
$om->setNumB(34);
echo "<br>";
echo $om->getResult();
?>
Copy after login

The above introduces the PHP implementation of the simple factory pattern, including the content of the simple factory pattern. I hope it will be helpful to friends who are interested in PHP tutorials.

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 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)

How to apply the simple factory pattern in PHP to improve code reusability How to apply the simple factory pattern in PHP to improve code reusability Sep 05, 2023 pm 12:27 PM

How to apply the simple factory pattern in PHP to improve code reusability. The simple factory pattern (SimpleFactoryPattern) is a commonly used design pattern that can provide a unified interface when creating objects so that different objects can be created according to different conditions. Example. This mode can effectively reduce the coupling of code and improve the maintainability and reusability of code. In PHP, we can use the simple factory pattern to optimize the structure and logic of the code. Understanding the simple factory pattern The simple factory pattern consists of three

How to implement object version control and management through PHP object-oriented simple factory pattern How to implement object version control and management through PHP object-oriented simple factory pattern Sep 06, 2023 pm 02:39 PM

How to implement object version control and management through the PHP object-oriented simple factory model. When developing large and complex PHP projects, version control and management are very important. Through appropriate design patterns, we can better manage and control the creation and use of objects, thereby improving the maintainability and scalability of the code. This article will introduce how to use the PHP object-oriented simple factory pattern to implement object version control and management. The simple factory pattern is a design pattern for creating classes that instantiates specified objects through a factory class

Research on three design methods of Java factory pattern Research on three design methods of Java factory pattern Feb 18, 2024 pm 05:16 PM

Explore Three Design Ideas of Java Factory Pattern Factory pattern is a commonly used design pattern for creating objects without specifying a specific class. In Java, the factory pattern can be implemented in many ways. This article will explore the implementation of three Java factory patterns based on different design ideas and give specific code examples. Simple Factory Pattern The simple factory pattern is the most basic factory pattern, which creates objects through a factory class. The factory class determines what kind of specific object should be created based on the client's request parameters. Below is a brief

How to create testable object instances using PHP object-oriented simple factory pattern How to create testable object instances using PHP object-oriented simple factory pattern Sep 05, 2023 pm 02:45 PM

How to use the PHP object-oriented simple factory pattern to create testable object instances. The simple factory pattern is a commonly used software design pattern that helps us create different object instances based on different conditions. In PHP object-oriented programming, combining the simple factory pattern can improve the testability and maintainability of the code. In this article, we will learn how to create testable object instances using the object-oriented simple factory pattern in PHP. We will illustrate this process with a simple example. First, let's define an interface to represent the

In-depth discussion on the implementation and application of Java factory pattern In-depth discussion on the implementation and application of Java factory pattern Feb 24, 2024 pm 10:15 PM

Detailed explanation of the principles and applications of Java factory pattern Factory pattern is a commonly used design pattern, which is used to create objects and encapsulate the object creation process. There are many ways to implement the factory pattern in Java, the most common of which are the simple factory pattern, the factory method pattern and the abstract factory pattern. This article will introduce the principles and applications of these three factory patterns in detail, and give corresponding code examples. 1. Simple Factory Pattern The simple factory pattern is the simplest and most commonly used factory pattern. It uses a factory class to return different values ​​based on the parameters passed in.

How to apply the simple factory pattern in PHP to realize automated creation of objects How to apply the simple factory pattern in PHP to realize automated creation of objects Sep 05, 2023 pm 02:27 PM

How to apply the simple factory pattern in PHP to automate object creation. The simple factory pattern is a common design pattern that is used to create objects and abstracts the process of instantiating objects. In PHP, applying the simple factory pattern can help us decouple object creation and specific implementation, making the code more flexible and maintainable. In this article, we will use an example to illustrate how to apply the Simple Factory Pattern in PHP. Suppose we have an electronics store that sells mobile phones and televisions. We need to create photos based on user selections

How to achieve object polymorphism through PHP object-oriented simple factory pattern How to achieve object polymorphism through PHP object-oriented simple factory pattern Sep 05, 2023 am 08:43 AM

How to achieve object polymorphism through the PHP object-oriented simple factory pattern. The simple factory pattern is a common design pattern that can create objects of different types through a common factory class and hide the object creation process. PHP object-oriented simple factory pattern can help us achieve object polymorphism. The simple factory pattern contains three basic roles: factory class, abstract class and concrete class. First we define an abstract class Animal, which contains an abstract method say(): abstractclas

How to realize unified entry and output of objects through PHP object-oriented simple factory pattern How to realize unified entry and output of objects through PHP object-oriented simple factory pattern Sep 06, 2023 pm 01:57 PM

How to realize unified entry and output of objects through PHP object-oriented simple factory pattern Introduction: In PHP development, the maintainability and scalability of the code can be improved by using object-oriented programming (Object-Oriented Programming, OOP). Object-oriented design pattern is a widely used method that can help us better organize and manage code. In this article, we will focus on how to achieve a unified entrance to objects by using the PHP object-oriented simple factory pattern.

See all articles