StarCraft and design model (1) -- simple factory Model)

Source: Internet
Author: User
In OO Program We often see a simple factory mode ). The simple factory mode returns instances of one of several possible classes based on the data provided to it. Generally, the classes returned by this method have a common parent class and a common method, but each method executes different tasks and is optimized based on different data. The simple factory model does not actually belong to 23 oOf models, but it can serve as a guide to the factory method we will discuss later.
In Starcraft games, in order to create the largest number of troops as resources permit, we need to create different troops based on the amount of resources at that time to maximize the creation of troops! Let's take the scenario of using a family of machine gunshots and fire fighters as an example! Assume that at that time the mine resources were very sufficient. When the gas volume was greater than 25, a firehose was created. After the creation, if the gas volume was less than 25, it would not stop creating troops, since it is impossible to create a new firehose, we can create a machine gun ......
Using System;
Using System. Collections. Generic;
Using System. text;

Namespace Starcraft. simplefactory
{
Public   Abstract   Class Terran // People
{
Public Abstract VoidTrain ();//Creating troops
}

Public   Class Marine: Terran // Train gunshots
{
Public   Override   Void Train ()
{
Console. writeline (
"Create a machine gun");
}

}

Public   Class Firebat: Terran // Training
{
Public   Override   Void Train ()
{
Console. writeline (
"Create a firehose");
}

}

Public   Class Trainfactory // Training factory-computer program
{
Public Terran besttrain ( Int Gas)
{
If (Gas < 25 ) // When the amount of gas exceeds 25, call the program for creating fire fighters. Otherwise, call the program for creating fire fighters.
{Return NewMarine ();}
Else
{Return NewFirebat ();}
}

}


Class Game // A specific game
{
Static   Void Main ( String [] ARGs)
{
Random Ra
=   New Random ();
Int Gas;
Trainfactory barracks
=   New Trainfactory ();
For ( Int I =   0 ; I <   5 ; I ++ )
{
Gas
= RA. Next ( 50 ); // Immediately generate gas to simulate the real-time remaining computing capacity of the computer during the game.
Terran = Barracks. besttrain (gas ); // Create a specific soldier based on the amount of gas
Terran. Train (); // Completion of creation
}

}

}

}

running result:
now we are looking at the main function in game. In the program, we are also called the customer layer. In the game, we are called the commander! For specific types of troops, we will be a specific type of training trainer or a specific type of training department. The commander only needs to know that we need to build troops now, so he told the factory that we have X to boost our gas, and we do not need to know what we can create, this is a task in the military factory. The military factory allocates the task of creating troops to different training departments based on the information provided by the commander! The role of the military factory is to reduce the dependency between the frontline commander and the Training Department!
it is clear that only physical troops can be trained in the man-family military factory. In the case of strong opponents, it is difficult to win a battle by physical troops alone. Mechanized troops and high technology are also required! At this time, the simple factory model seems powerless! However, the game is just getting started. As the game goes deeper, we have other design patterns to guide everyone's production, micro-operations, and tactics until the final victory.

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.