>  기사  >  백엔드 개발  >  PHP 디자인 패턴 프로토타입 디자인 패턴 원리 및 사용 분석 PHP 기술

PHP 디자인 패턴 프로토타입 디자인 패턴 원리 및 사용 분석 PHP 기술

jacklove
jacklove원래의
2018-06-26 17:18:131458검색

이 글은 주로 PHP 디자인 패턴 중 프로토타입 디자인 패턴을 소개하고, 프로토타입 디자인 패턴의 개념과 원리를 간략하게 설명하고, PHP 프로토타입 디자인 패턴의 정의와 활용 방법을 예시 형태로 분석하여 도움이 필요한 친구들이 참고할 수 있도록 합니다. 이 기사

예제에서는 PHP 디자인 패턴 중 프로토타입 디자인 패턴의 원리와 사용법을 설명합니다. 다음과 같이 참고할 수 있도록 모두와 공유하세요.

1. 프로토타입 디자인 패턴이란

프로토타입 디자인 패턴은 복제 기술을 사용하여 인스턴스화된 객체를 복사하고, 프로토타입 인스턴스를 복사하여 새로운 객체를 생성합니다. 프로토타입 디자인 패턴의 목적은 복제를 사용하여 객체를 인스턴스화하는 오버헤드를 줄이는 것입니다.

프로토타이핑 디자인 패턴에서 Client 클래스는 빼놓을 수 없는 부분입니다.

PHP에는 디자인 모드에서 사용할 수 있는 복제 방법

이 내장되어 있지만 직접 액세스할 수는 없으며 단지 clone 키워드를 사용하면 됩니다. 복제는 생성자를 시작하지 않습니다.

__clone()

2. 프로토타입 디자인 패턴을 사용해야 하는 경우

프로젝트에서 프로토타입 객체의 여러 인스턴스를 생성해야 하는 경우 프로토타입 디자인 패턴을 사용할 수 있습니다.

3. 프로토타입 디자인 패턴의 예

다음은 현대 기업 조직의 예입니다.

<?php
/**
*  原型设计模式
*        以现代企业组织为例
**/
//部门抽象类
abstract class IAcmePrototype
{
  protected $id;   //员工ID号
  protected $name;  //员工名字
  protected $dept;  //员工部门
  //克隆方法
  abstract function __clone();
  //员工部门设置方法
  abstract function setDept($orgCode);
  //员工部门获取方法
  public function getDept()
  {
    return $this->dept;
  }
  //员工ID号设置方法
  public function setId($id)
  {
    $this->id = $id;
  }
  //员工ID号获取方法
  public function getId()
  {
    return $this->id;
  }
  //员工名字设置方法
  public function setName($name)
  {
    $this->name = $name;
  }
  //员工名字获取方法
  public function getName()
  {
    return $this->name;
  }
}
//市场部类
class Marketing extends IAcmePrototype
{
  const UNIT = "Marketing";  //标识
  //市场部门类别
  private $sales = "sales";
  private $promotion = "promotion";
  private $strategic = "strategic planning";
  //克隆函数
  function __clone()
  {
  }
  //部门设置函数
  public function setDept($orgCode)
  {
    switch($orgCode)
    {
      case 101:
          $this->dept = $this->sales;
          break;
      case 102:
          $this->dept = $this->promotion;
          break;
      case 103:
          $this->dept = $this->strategic;
          break;
      default:
          $this->dept = "Unrecognized Marketing";
    }
  }
}
//管理部类
class Management extends IAcmePrototype
{
  const UNIT = "Management";
  private $research = "research";
  private $plan = "planning";
  private $operations = "operations";
  function __clone()
  {
  }
  public function setDept($orgCode)
  {
    switch($orgCode)
    {
      case 201:
          $this->dept = $this->research;
          break;
      case 202:
          $this->dept = $this->plan;
          break;
      case 203:
          $this->dept = $this->operations;
          break;
      default:
          $this->dept = "Unrecognized Marketing";
    }
  }
}
//工厂部类
class Engineering extends IAcmePrototype
{
  const UNIT = "Engineering";
  private $development = "programming";
  private $design = "digital artwork";
  private $sysAd = "system administration";
  function __clone()
  {
  }
  public function setDept($orgCode)
  {
    switch($orgCode)
    {
      case 301:
          $this->dept = $this->development;
          break;
      case 302:
          $this->dept = $this->design;
          break;
      case 303:
          $this->dept = $this->sysAd;
          break;
      default:
          $this->dept = "Unrecognized Marketing";
    }
  }
}
//客户类
class Client
{
  private $market;  //市场部类实例
  private $manage;  //管理部类实例
  private $engineer; //工厂部类实例
  //构造函数
  public function __construct()
  {
    $this->makeConProto();
    //市场部类实例克隆
    $Tess = clone $this->market;
    $this->setEmployee($Tess,"Tess Smith",101,"ts101-1234");
    $this->showEmployee($Tess);
    $Jacob = clone $this->market;
    $this->setEmployee($Jacob,"Jacob Jones",102,"jj101-2234");
    $this->showEmployee($Jacob);
    //管理部类实例克隆
    $Ricky = clone $this->manage;
    $this->setEmployee($Ricky,"Ricky Rodrigues",203,"rr203-5634");
    $this->showEmployee($Ricky);
    //工程部类实例克隆
    $Olivia = clone $this->engineer;
    $this->setEmployee($Olivia,"Olivia perez",302,"op302-1278");
    $this->showEmployee($Olivia);
    $John = clone $this->engineer;
    $this->setEmployee($John,"John Jackson",301,"jj301-1454");
    $this->showEmployee($John);
  }
  //实例化部门对象
  private function makeConProto()
  {
    $this->market = new Marketing();
    $this->manage = new Management();
    $this->engineer = new Engineering();
  }
  //员工信息设置方法
  private function setEmployee(IAcmePrototype $employee,$name,$dept,$id)
  {
    $employee->setName($name);
    $employee->setDept($dept);
    $employee->setId($id);
  }
  //员工信息显示方法
  private function showEmployee(IAcmePrototype $employee)
  {
    echo $employee->getName() . &#39;<br />&#39;;
    echo $employee->getDept() . &#39;<br />&#39;;
    echo $employee->getId() . &#39;<br />&#39;;
  }
}
$client = new Client();
?>

실행 결과:

Tess Smith
sales

ts101-1234
Jacob Jones
프로모션
jj1 01-2234
Ricky Rodrigues
Operations
rr203-5634
Olivia Perez
digital Artwork
op302-1278
John Jackson
programming
jj301-1454

관심 있는 항목:

인증 코드 보내기 laravel의 문자 메시지를 통해 구현 방법 php 예제


PHP receiveMail은 이메일 수신 기능을 구현합니다. php 예제


PHP 공유 이미지 생성 방법 php 기술


위 내용은 PHP 디자인 패턴 프로토타입 디자인 패턴 원리 및 사용 분석 PHP 기술의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.