Bridge - game articles (original)

xiaoxiao2021-03-06  85

Bridge - game articles

Today, two game discs are bought from the electronics market, one is a three-king game (Sanguogame), one is a CSGame. People who play games may know that the game is required for computer system (Computersystem), maybe a game can play in the Windows98 system, can't play under the Windows2000 system, so in order not to go, first look Computer system (Computersystem) required by the game!

Ok, gossip Less: Start our game journey:

1. Here, first define a computer system (Computersystem) interface class:

Public interface computersystem {

Public Abstract void PlayGame (); // play games

}

2. Refine the specific implementation class for the Computersystem interface:

A: Windows98 system

Public class windows98 imports computesystem {

Public void PlayGame () {

System.out.println ("Play Windows98 Game!");

}

}

B: Windows2000 system

Public class windows2000 imports computersystem {

Public void PlayGame () {

System.out.println ("Play Windows2000 Game!");

}

}

3, Define Games (Game) Class:

Public Abstract Class Game {

Public abstract void play (); // play games

Protected Computersystem GetSetupsystem (String Type) {// Getting System To Install

IF (Type.Equals)) {// If the game requirements must be installed under Windows 98

Return new windows98 (); // uses a Windows98 system

}

Else IF ("Windows2000")) {// If the game requires installed under Windows2000

Return new windows2000 (); // uses a Windows2000 system

}

Else {

Return new windows98 (); // Boot is started with Windows98 system

}

}

}

4. Define the subclasses of the game (GAME) class:

A: Sanguogame

Public class sanguificame extends game {

Private computersystem computersystem;

Public SANGUOGAME (STRING TYPE) {// Look at the game requirements installed in that system

Computersystem = getSetupsystem (Type); // Use this system

}

Public void play () {// play games

Computersystem.playgame ();

System.out.println ("I am playing three countries, don't bother me!");

}

}

B: CSGame (csgame) public class csgame extends Game {

Private computersystem computersystem;

Public csgame (String Type) {// See the game requirements installed on that system

Computersystem = getSetupsystem (Type); // Use this system

}

Public void play () {// play games

Computersystem.playgame ();

System.out.println ("I am playing CS, don't bother me!");

}

}

5, write test classes:

Public class test {

Public static void main (String [] args) {

Game Sanguo = New SANGUGAME ("Windows98"); // Game Requirements Windows98

SANGUO.PLAY ();

Sanguo = New Sanguogame ("Windows2000"); // The game requires Windows2000

SANGUO.PLAY ();

Game cs = new csgame ("windows98"); // game requirements Windows98

cs.play ();

CS = new csgame ("windows2000"); // Game requirements Windows2000

cs.play ();

}

}

6. Description:

A: Bridge Definition: The abstraction and behavior are divided, each independent, but can dynamically bind.

B: From this example we can see that different games are different, and the three countries and Cs may need Windows98 systems, or the Windows2000 system may also require a different system, thus processing such problems. We can use Bridge this model.

C: The behavior here refers to the game, abstract refers to the system!

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

New Post(0)