다형성을 기반으로 한 가상 프록시는 클라이언트 코드를 수정하지 않고 비용이 많이 드는 객체 그래프를 지연시킬 수 있습니다. 에이전트는 단일 객체 또는 객체 모음으로 작동하도록 설계되어 데이터 관리의 유연성과 효율성을 제공합니다. 가상 에이전트는 스토리지에서 도메인 오브젝트의 게으른로드 컬렉션에 특히 효과적입니다 (예 : 요청시 데이터베이스에서 관련 주석을 추출 할 수있는 블로그 게시물 배치).
실제 주석 컬렉션의 동작을 시뮬레이션하는 프록시는 데이터 맵퍼를 통해 실현되므로 지속성의 관련성이 향상됩니다. 가상 프록시는 값 비싼 작업의 실행을 지연시키는 효과적인 도구 (예 : 스토리지 계층에서 다량의 데이터로드 지연) 및 객체 지향 애플리케이션에서 일반적인 강성 및 취약성 문제를 줄이는 효과적인 도구입니다.
<code class="language-php"><?php namespace Model; use ModelCollectionCommentCollectionInterface; interface PostInterface { public function setId($id); public function getId(); public function setTitle($title); public function getTitle(); public function setContent($content); public function getContent(); public function setComments(CommentCollectionInterface $comments); public function getComments(); }</code>
<code class="language-php"><?php namespace Model; use ModelCollectionCommentCollectionInterface; class Post implements PostInterface { protected $id; protected $title; protected $content; protected $comments; public function __construct($title, $content, CommentCollectionInterface $comments = null) { $this->setTitle($title); $this->setContent($content); $this->comments = $comments; } // ... (Post class methods remain largely unchanged) ... }</code>
<code class="language-php"><?php namespace Model; interface CommentInterface { public function setId($id); public function getId(); public function setContent($content); public function getContent(); public function setPoster($poster); public function getPoster(); }</code>
<code class="language-php"><?php namespace Model; class Comment implements CommentInterface { protected $id; protected $content; protected $poster; public function __construct($content, $poster) { $this->setContent($content); $this->setPoster($poster); } // ... (Comment class methods remain largely unchanged) ... }</code>가상 에이전트를 통한 도메인 객체의 수집과의 상호 작용 솔직히 말하면, 앞에서 언급했듯이 래핑 어레이 모음은 다른 종속성에 의존하지 않고 독립적으로 존재할 수 있습니다. (회의적이라면, 교리의 컬렉션이 무대 뒤에서 어떻게 작동하는지 자유롭게 확인하십시오.) 여전히 실제 검토 컬렉션의 행동을 시뮬레이션하는 대리를 구현하려고하지만 실제로는 가벼운 대안입니다. 질문해야 할 질문은 다음과 같습니다. 데이터베이스에서 댓글을 추출하여 이전 컬렉션에 넣는 방법은 무엇입니까? 이를 달성하는 방법에는 여러 가지가 있지만 가장 매력적인 것은 데이터 맵퍼를 통한 것이라고 생각합니다. 아래 매퍼는 Storage에서 댓글 모음을 얻는 것이 좋습니다. 확인하십시오 :
<code class="language-php"><?php namespace Model; use ModelCollectionCommentCollectionInterface; interface PostInterface { public function setId($id); public function getId(); public function setTitle($title); public function getTitle(); public function setContent($content); public function getContent(); public function setComments(CommentCollectionInterface $comments); public function getComments(); }</code>
<code class="language-php"><?php namespace Model; use ModelCollectionCommentCollectionInterface; class Post implements PostInterface { protected $id; protected $title; protected $content; protected $comments; public function __construct($title, $content, CommentCollectionInterface $comments = null) { $this->setTitle($title); $this->setContent($content); $this->comments = $comments; } // ... (Post class methods remain largely unchanged) ... }</code>
이 접근법의 단점은 주석이 저장소에서 먼저 추출한 다음 게시물 객체의 내부에 주입된다는 것입니다. 다른 방법으로 어떻게 해야하는지, 이번에는 클라이언트 코드를 프록시로 "스푸핑"하는 것입니까?
<code class="language-php"><?php namespace Model; interface CommentInterface { public function setId($id); public function getId(); public function setContent($content); public function getContent(); public function setPoster($poster); public function getPoster(); }</code>
<🎜 🎜> <<> 결론
<code class="language-php"><?php namespace Model; class Comment implements CommentInterface { protected $id; protected $content; protected $poster; public function __construct($content, $poster) { $this->setContent($content); $this->setPoster($poster); } // ... (Comment class methods remain largely unchanged) ... }</code>
(imredesiuk / shutterstock의 그림)
위 내용은 가상 프록시에 대한 소개, 2 부의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!