Learning notes for the PHP Observer observer model

Source: Internet
Author: User

So how do you let computers know that their allies are being attacked? and respond automatically?


The problem to be solved: Once a computer is attacked by us, other computers will be informed and automatically send troops to help.


Train of thought: Set up some additional observation system for the computer, let them inform other computers.

Observer (Observer) Pattern Example:

The code is as follows Copy Code

<?php

The Abstract Association Class

abstractclassabstractally{

Place the collection of observers, here with a simple array to visually demonstrate

Public$oberservercollection;

Increase the observer's method, the parameter is the Observer (also the player) name

Publicfunctionaddoberserver ($oberserverName)

{

To put an observer object into the viewer's collection in an element manner

$this->oberservercollection[]=newoberserver ($oberserverName);

}

Notify each observer of the name of the attacked computer

Publicfunctionnotify ($beAttackedPlayerName)

{

Loop The Observer's collection

foreach ($this->oberservercollectionas$oberserver)

{

Call each observer's rescue function, which is the name of the attacked computer, if used to exclude the observer of the attacked computer

if ($oberserver->name!= $beAttackedPlayerName) $oberserver->help ($beAttackedPlayerName);

}

}

Abstractpublicfunctionbeattacked ($beAttackedPlayer);

}

Specific Allied Classes

classallyextendsabstractally{

Constructor that takes an array of the names of all computer players as arguments

Publicfunction__construct ($allPlayerName)

{

Loop the array of all the computer players

foreach ($allPlayerNameas $playername)

{

Add observer, parameters for each computer player's name

$this->addoberserver ($playerName);

}

}

Notify each observer of the name of the attacked computer

Publicfunctionbeattacked ($beAttackedPlayerName)

{

Call each observer's rescue function, which is the name of the attacked computer, if used to exclude the observer of the attacked computer

$this->notify ($beAttackedPlayerName);

}

}

The Observer's interface

interfaceioberserver{

Define a standard rescue method

Functionhelp ($beAttackedPlayer);

}

The specific observer class

classoberserverimplementsioberserver{

The name of the Observer (who is also the player) object

Public$name;

constructor, parameter is the name of the Observer (also the player)

Publicfunction__construct ($name)

{

$this->name= $name;

}

The observer's way to rescue the people

Publichelp ($beAttackedPlayerName)

{

Here simple output, who to save who, and finally add a line, easy to display

Echo$this->name. " Help ". $beAttackedPlayerName." <br> ";

}

Abstractpublicfunctionbeattacked ($beAttackedPlayer);

}

Let's say I'm a couple of three, two Zerg, a Protoss.

$allComputePlayer =array (' Zerg1 ', ' Protoss2 ', ' Zerg2 ');

New computer Alliances

$Ally =newally ($allComputePlayer);

Let's say I attacked the second Zerg.

$Ally->beattacked (' Zerg2 ');

?>


WAY SUMMARY: The Observer pattern can immediately notify all relevant objects of a change in a state and invoke the other's handling.


Implementation SUMMARY: Requires an observer class to handle changes, and the observed object needs to implement a method of informing all observers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.