星际的升级系统做得比较平衡,不过由于不少兵种和建筑的制造都需要有相关的科技建筑,所以关系比较复杂。
比如一个科学站造出来后,所有的飞机场都可以建造科技球了,但是一旦一个科学站被摧毁,就要看是否还有科学站,否则就得让所有的飞机场都不能造科技球。
我们可以用上次说的观察者模式解决问题,不过由于星际里面的升级相关比较多,似乎比较麻烦。
其实从实质来讲,任何升级一般只要知道某种建筑是否存在就行了,因此我们不必让他们多对多联系,设置一个中介者就行了。
这就好像我们不管买什么东西,到超市就可以了,而厂家也只要和超市联系,不必和我们每个消费者直接接触。
待解决的问题:不要让各个建筑互相联系,减少复杂程度。
思路:设置中介者,每次遇到制造科技相关的东西,询问中介者。
中介者(Mediator)模式示例:
<?php //中介者 class Mediator { //存放科技建筑的数量,为了简单说明,用静态属性,其实也可以让各个对象来处理 public static $techBuilding; //根据参数$techBuildingName代表的建筑名称,返回是否存在相应的科技建筑,为了简单说明,用静态属性 public static function isTechAllow ($techBuildingName) { //如果科技建筑数量大于零,就返回true,否则返回false return self::$techBuilding[$techBuildingName]>0; } //一旦科技建筑造好了或者被摧毁,调用这个方法,参数$techBuildingName代表建筑名称,$add为布尔值,true表示增加(建造),false代表减少(摧毁) public static function changeTech ($techBuildingName, $add) { //建造 if ($add) { //增加数量 self::$techBuilding[$techBuildingName]++; } else { //减少数量 self::$techBuilding[$techBuildingName]--; } } } //科技站类 class ScienceFacility { //构造方法 public function
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks agoByDDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks agoByDDD
Where to find the Crane Control Keycard in Atomfall
3 weeks agoByDDD
Saving in R.E.P.O. Explained (And Save Files)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment