PhpChainofResponsibility responsibility chain mode-PHP source code

Source: Internet
Author: User
The attributes of the StarCraft category will be modified as the balance is adjusted. In this case, we need to consider reducing the correlation between an event and the specific processing. The attributes of the StarCraft category will be modified as the balance is adjusted. In this case, we need to consider reducing the correlation between an event and the specific processing.

Script ec (2); script


For example, when an atomic bomb is dropped, blood may be reduced for troops or buildings in the killing range. However, the damage degree varies with the distance from the central point, in addition, different types of weapons and damaged buildings are different.


The problem to be solved: When an atomic bomb is cast, the killing measures are handed over to the troops within the killing range or the building's own methods.


Idea: Create an interface to implement all troops or buildings.

Chain of Responsibility Example:

The Code is as follows:

// Atomic attack Interface

Interface NuclearAttacked {

// Method for handling atomic bomb attacks. The parameter is the x and y coordinates of the serving point.

Public function NuclearAttacked ($ x, $ y );

}

// The base of the person family to implement the interface attacked by the atomic bomb. Other content is not considered for the moment.

Class CommandCenter implements NuclearAttacked {

// Method for handling atomic bomb attacks. The parameter is the x and y coordinates of the serving point.

Public function NuclearAttacked ($ x, $ y)

{

// According to the distance from the Atomic Bomb Center, the reduced blood is defined. If the remaining blood is exceeded, it will blow up.

}

}

// The cruiser (also known as Dahe) implements an interface attacked by an atomic bomb. Other content is not considered for the moment.

Class Battlecruiser implements NuclearAttacked {

// Method for handling atomic bomb attacks. The parameter is the x and y coordinates of the serving point.

Public function NuclearAttacked ($ x, $ y)

{

// According to the distance from the Atomic Bomb Center, the reduced blood is defined. If the remaining blood is exceeded, it will blow up.

}

}

// Atomic bomb

Class Nuclear {

// Targets attacked by atomic bombs

Public $ attackedThings;

// Add the object attacked by the atomic bomb

Public function addAttackedThings ($ thing)

{

// Add the object attacked by the atomic bomb

$ This-> attackedThings [] = $ thing;

}

// Atomic bomb explosion method. The parameter is the x and y coordinates of the serving point.

Public function blast ($ x, $ y)

{

// Hand over the explosion to all involved objects and let them handle it by themselves

Foreach ($ this-> attackedThings as $ thing)

{

// Hand over the explosion to all involved objects and let them handle it by themselves

$ Thing-> NuclearAttacked ($ x, $ y );

}

}

}

// Create a Base Object

$ CommandCenter = new CommandCenter ();

// Create a new cruiser object

$ Battlecruiser = new Battlecruiser ();

// Creates an atomic bomb.

$ Nuclear2 = new Nuclear ();

// Assume that the launch is successful. At that moment, a base object and a cruiser object are in the killing range.

$ Nuclear2-> addAttackedThings ($ CommandCenter );

$ Nuclear2-> addAttackedThings ($ Battlecruiser );

// Atomic bomb explosion. In this way, the event is handed over to the Processing Methods of the objects involved. Assume that the x and y coordinates of the serving point are 2353,368.

$ Nuclear2-> blast (2353,368 );

?>

Purpose Summary: In the responsibility chain mode, an event involving multiple objects can be handled by the object itself to reduce associativity.

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.