PHP 대상을 대상으로 깊이 연구하는 【대상 생성】

28149 단어

대상


예를 들다
<?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
(
)

좋은 웹페이지 즐겨찾기