PHP 대상을 대상으로 깊이 연구하는 【대상 생성】
대상
예를 들다
<?php
abstract class Employee { //
protected $name;
function __construct( $name ) {
$this->name = $name;
}
abstract function fire();
}
class Minion extends Employee { //
function fire() {
print "{$this->name}: I'll clear my desk
";
}
}
class NastyBoss { //
private $employees = array();
function addEmployee( $employeeName ) { //
$this->employees[] = new Minion( $employeeName ); //
}
function projectFails() {
if ( count( $this->employees ) > 0 ) {
$emp = array_pop( $this->employees );
$emp->fire(); //
}
}
}
$boss = new NastyBoss();
$boss->addEmployee( "harry" );
$boss->addEmployee( "bob" );
$boss->addEmployee( "mary" );
$boss->projectFails();
// output:
// mary: I'll clear my desk
?>
더욱 융통성 있는 사례 하나 더 보기
<?php
abstract class Employee {
protected $name;
function __construct( $name ) {
$this->name = $name;
}
abstract function fire();
}
class Minion extends Employee {
function fire() {
print "{$this->name}: I'll clear my desk
";
}
}
class NastyBoss {
private $employees = array();
function addEmployee( Employee $employee ) { //
$this->employees[] = $employee;
}
function projectFails() {
if ( count( $this->employees ) ) {
$emp = array_pop( $this->employees );
$emp->fire();
}
}
}
// new Employee class...
class CluedUp extends Employee {
function fire() {
print "{$this->name}: I'll call my lawyer
";
}
}
$boss = new NastyBoss();
$boss->addEmployee( new Minion( "harry" ) ); // ,
$boss->addEmployee( new CluedUp( "bob" ) );
$boss->addEmployee( new Minion( "mary" ) );
$boss->projectFails();
$boss->projectFails();
$boss->projectFails();
// output:
// mary: I'll clear my desk
// bob: I'll call my lawyer
// harry: I'll clear my desk
?>
단례
<?php
class Preferences {
private $props = array();
private static $instance; // ,
private function __construct() { } // ,
public static function getInstance() { // ,
if ( empty( self::$instance ) ) {
self::$instance = new Preferences();
}
return self::$instance;
}
public function setProperty( $key, $val ) {
$this->props[$key] = $val;
}
public function getProperty( $key ) {
return $this->props[$key];
}
}
$pref = Preferences::getInstance();
$pref->setProperty( "name", "matt" );
unset( $pref ); // remove the reference
$pref2 = Preferences::getInstance();
print $pref2->getProperty( "name" ) ."
"; // demonstrate value is not lost
?>
평론: 대상을 마음대로 만들 수 없습니다.Preferences::getInstance () 를 통해서만 대상을 만들 수 있습니다.
공장 모델
<?php
abstract class ApptEncoder {
abstract function encode();
}
class BloggsApptEncoder extends ApptEncoder {
function encode() {
return "Appointment data encoded in BloggsCal format
";
}
}
class MegaApptEncoder extends ApptEncoder {
function encode() {
return "Appointment data encoded in MegaCal format
";
}
}
class CommsManager { // Bloggs
function getApptEncoder() {
return new BloggsApptEncoder();
}
}
$obj = new CommsManager();
$bloggs = $obj->getApptEncoder(); //
print $bloggs->encode();
?>
output:
Appointment data encoded in BloggsCal format
유연성 설정 추가
<?php
abstract class ApptEncoder {
abstract function encode();
}
class BloggsApptEncoder extends ApptEncoder {
function encode() {
return "Appointment data encoded in BloggsCal format
";
}
}
class MegaApptEncoder extends ApptEncoder {
function encode() {
return "Appointment data encoded in MegaCal format
";
}
}
class CommsManager {
const BLOGGS = 1;
const MEGA = 2;
private $mode ;
function __construct( $mode ) {
$this->mode = $mode;
}
function getHeaderText() {
switch ( $this->mode ) {
case ( self::MEGA ):
return "MegaCal header
";
default:
return "BloggsCal header
";
}
}
function getApptEncoder() {
switch ( $this->mode ) {
case ( self::MEGA ):
return new MegaApptEncoder();
default:
return new BloggsApptEncoder();
}
}
}
$man = new CommsManager( CommsManager::MEGA );
print ( get_class( $man->getApptEncoder() ) )."
";
$man = new CommsManager( CommsManager::BLOGGS );
print ( get_class( $man->getApptEncoder() ) )."
";
?>
output:
MegaApptEncoder
BloggsApptEncoder
공장 방법 모델은 창설자류와 생산할 제품류를 분리해야 한다.
추상 공장
추상을 통해 구속하여 일정한 규칙이 되다.
<?php
abstract class ApptEncoder {
abstract function encode();
}
class BloggsApptEncoder extends ApptEncoder {
function encode() {
return "Appointment data encoded in BloggsCal format
";
}
}
class MegaApptEncoder extends ApptEncoder {
function encode() {
return "Appointment data encoded in MegaCal format
";
}
}
abstract class CommsManager { //
abstract function getHeaderText();
abstract function getApptEncoder();
abstract function getTtdEncoder();
abstract function getContactEncoder();
abstract function getFooterText();
}
class BloggsCommsManager extends CommsManager {
function getHeaderText() {
return "BloggsCal header
";
}
function getApptEncoder() {
return new BloggsApptEncoder();
}
function getTtdEncoder() {
return new BloggsTtdEncoder();
}
function getContactEncoder() {
return new BloggsContactEncoder();
}
function getFooterText() {
return "BloggsCal footer
";
}
}
class MegaCommsManager extends CommsManager {
function getHeaderText() {
return "MegaCal header
";
}
function getApptEncoder() {
return new MegaApptEncoder();
}
function getTtdEncoder() {
return new MegaTtdEncoder();
}
function getContactEncoder() {
return new MegaContactEncoder();
}
function getFooterText() {
return "MegaCal footer
";
}
}
$mgr = new MegaCommsManager();
print $mgr->getHeaderText();
print $mgr->getApptEncoder()->encode(); // , , 。
print $mgr->getFooterText();
?>
output:
MegaCal header
Appointment data encoded in MegaCal format
MegaCal footer
더욱 강요하는 실현
<?php
// , 。 。
abstract class ApptEncoder {
abstract function encode();
}
class BloggsApptEncoder extends ApptEncoder {
function encode() {
return "Appointment data encoded in BloggsCal format
";
}
}
class MegaApptEncoder extends ApptEncoder {
function encode() {
return "Appointment data encoded in MegaCal format
";
}
}
abstract class CommsManager {
const APPT = 1;
const TTD = 2;
const CONTACT = 3;
abstract function getHeaderText();
abstract function make( $flag_int ); // int
abstract function getFooterText();
}
class BloggsCommsManager extends CommsManager {
function getHeaderText() {
return "BloggsCal header
";
}
function make( $flag_int ) {
switch ( $flag_int ) {
case self::APPT: // self
return new BloggsApptEncoder();
case self::CONTACT:
return new BloggsContactEncoder();
case self::TTD:
return new BloggsTtdEncoder();
}
}
function getFooterText() {
return "BloggsCal footer
";
}
}
$mgr = new BloggsCommsManager();
print $mgr->getHeaderText();
print $mgr->make( CommsManager::APPT )->encode();
print $mgr->getFooterText();
?>
output:
BloggsCal header
Appointment data encoded in BloggsCal format
BloggsCal footer
원형 모드
구체적인 제품을 보존하는 공장류로 개조하다.
<?php
class Sea {} //
class EarthSea extends Sea {}
class MarsSea extends Sea {}
class Plains {} //
class EarthPlains extends Plains {}
class MarsPlains extends Plains {}
class Forest {} //
class EarthForest extends Forest {}
class MarsForest extends Forest {}
class TerrainFactory { //
private $sea;
private $forest;
private $plains;
function __construct( Sea $sea, Plains $plains, Forest $forest ) { //
$this->sea = $sea;
$this->plains = $plains;
$this->forest = $forest;
}
function getSea( ) {
return clone $this->sea; //
}
function getPlains( ) {
return clone $this->plains;
}
function getForest( ) {
return clone $this->forest;
}
}
$factory = new TerrainFactory( new EarthSea(),
new EarthPlains(),
new EarthForest() );
print_r( $factory->getSea() );
print_r( $factory->getPlains() );
print_r( $factory->getForest() );
?>
output:
EarthSea Object
(
)
EarthPlains Object
(
)
EarthForest Object
(
)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.