PHP 프로토타이핑 패턴 사용 사례 분석

php中世界最好的语言
풀어 주다: 2023-03-25 22:28:02
원래의
1495명이 탐색했습니다.

이번에는 PHP 프로토타입 디자인 패턴 유스케이스 분석을 들고왔는데, PHP 프로토타입 디자인 패턴을 사용할 때 주의사항 은 무엇인지, 다음은 실제 사례를 살펴보겠습니다.

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

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

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

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

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

constructorclone()를 시작하지 않습니다.

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
promotion
jj101-2 234
Ricky Rodri 추측
Operations
rr203-5634
Olivia perez
digital Artwork
op302-1278
John Jackson
programming
jj301-1454

이 기사의 사례를 읽은 후 방법을 마스터했다고 믿습니다. , PHP 중국어 웹사이트의 다른 관련 기사도 주목해주세요!

추천 자료:

WeChat 공개 계정 access_token


PHP는 오류 위치를 찾아 strace를 통해 해결합니다

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

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!