Mode of Magic Brand (Bridge)

zhaozj2021-02-16  45

I don't know if you have a magic card. Basic score is two. One is called an attack card. Another auxiliary card.

The attack card is responsible for attack. And the auxiliary card is responsible for the effect. For example, strengthen the attack effect. Or weaken the enemy attack effect, etc.

Seeing this. The implementation part is the so-called attack card. And the abstract part is the effect card. They have the connection, but contact

It is not parallel to contact. That is to say, it is not like a swordsman who can be born out of magical swordsman, bright swordsman, etc.

Attack effect card. The connection between the attack card itself is not static, can be done by inheritance. Instead, it needs to be dynamic.

Binding. This is clearly used to use abstract classes and interfaces. Then it will lead to the design pattern bridge. So below

I will simulate the effect in the magic card. Suppose I have two Swordsman cards. They are magical swords and bright swords. And

Two effect cards. One is an attacking effect 400 another is a weakened attack effect 300. and is a dynamic establishment

The connection between the effect of the attack card.

Implementation:

// Attack card public interface attackcode {int point {get; set}; ImpactCode impact {get;}; void effect ();

// Effect Card Behavior Interface Public Interface IimpactCode {Iattackcode Attack {Get;} Void Effect ();}

Below we will formally implement the connection between specific cards.

@ Magic Swordsman public MagicKnightCode: IAttackCode {IImpactCode impact; int pow; public MagicKnightCode () {impact = ImpSingleton.impact; pow = 1800;} public IImpactCode Impact {get {return this.impact;}} public int Pow {get {RETURN POW;} set {pow = value;}}

Public void effect () {console.writeLine ("Magic Swordsman's Attack" POW);}}

// Light Swordsman public SunKnightCode: IAttackCode {IImpactCode impact; int pow; public SunKnightCode () {impact = ImpSingleton.impact; pow = 2000;} public IImpactCode Impact {get {return this.impact;}} public int Pow {get {RETURN POW;} set {pow = value;}}

Public void effect () {impact.effect (); console.writeLine ("Bright Swordsman's Attack" POW);}}

Next is the specific implementation of the effect card.

// attack power 400public class AddActtack: IImpactCode {IActtackCode acttack; public AddActtack () {acttack = ImpSingleton.Acttack;} public IActtackCode Acttack {return acttack;} public void effect {impact.effect (); acttack.Pow = 400; }} // Attacking force Debuilding 300PUBLIC CLASS SubtrationActtack: IIMPACTCODE {

IACTTACKCODE ACTTACK; Public SubtrationActtack () {acttack = impSingleton.acttack;} public icn acttack;} public void effect {acttack.pow- = 300;}}}

Here is the last step of this mode. I will build a Singleton mode. To complete the docking work

Because it is necessary to ensure that the card must be single in each round. So use Singleton mode

Public Class Impsingleton {Prviate Static ItemptactCode Imptack; Public Impswin (iimptactCode Im Iacttackcode AC) {this.Imptack = IM; this.acttack = ac;}

PUBLIC Static IimpatackCode Imptack;}} public static Iacttack actack {get {retrun acttack;}} public void effect ()}}}}}}}}}}}}}

Public class client {

Public static int main (String [] args) {// uses an attacking force for magic swordsman Impsingleton IS = New Impsingleton (New MagickNightCode (), new addresstack ()); is.effect ();

// Use the weakening effect card for the light Swordsman is = null is = new impSingleton (new sucknightcode (), new subtrationActtack (); is.effect (); return 0;}}}}}

This makes it possible to see that the Bridge mode is useful to use the object that needs to dynamically bind. If in the database system

You can separate data storage and actual business logic.

转载请注明原文地址:https://www.9cbs.com/read-27422.html

New Post(0)