某软件公司欲开发一款汽车竞速类游戏,需要模拟长轮胎和短轮胎急刹车时在路面上留下的不同痕迹,并考虑后续能模拟更多种轮胎急刹车时的痕迹。现采用策略(Strategy)设计模式来实现该需求,所设计的类图如图5-1所示。

图5-1类图
【Java代码】
import java.util.*;
interface BrakeBehavior{
public(1);
/*其余代码省略*/
};
class LongWheelBrake implements BrakeBehavior{
public void stop( ){System.out.println("模拟长轮胎刹车痕迹!");}
/*其余代码省略*/
};
class ShortWheelBrake implements BrakeBehavior{
public void stop( ){System.out.println("模拟短轮胎刹车痕迹!");}
/*其余代码省略*/
};
abstract class Car{
protected(2)wheel;
public void brake( ){(3);}
/*其余代码省略*/
}:
class ShortWheelCar extends Car{
public ShortWheelCar(BrakeBehavior behavior){
(4);
}
/*其余代码省略*/
};
class StrategyTest{
public static void main(String[]args){
BrakeBehavior brake=new ShortWheelBrake( );
ShortWheelCar car1=new ShortWheelCar(brake);
car1.(5);
}
}
正确答案及解析
正确答案
解析
(1)void stop()
(2)BrakeBehavior
(3)wheel.stop()
(4)wheel=behavior
(5)brake()
策略模式是定义一系列算法,把他们一个个封装起来,并且使它们之间可相互替换,从而让算法可以独立于使用它的用户而变化。
(1)第一空接口BrakeBehavior有内容缺失,结合其实现类LongWheelBrake代码如下:
class LongWheelBrake implements BrakeBehavior{
public void stop(){System.out.println("模拟长轮胎刹车痕迹!");}
/*其余代码省略*/
};
第一空需要补充stop()方法,即(1)void stop()
(2)(3)第二、三空是抽象类Car缺少属性wheel的类型和brake()方法的方法体。
abstract class Car{
protected(2)wheel;
public void brake(){(3);}
/*其余代码省略*/
}:
根据图示策略模式,Car与BrakeBehavior是整体与部分的关系,因此Car的属性有这一个部分,即(2)BrakeBehavior,这里在类Car中,命名了一个与之联系的部分BrakeBehavior类型wheel。对于BrakeBehavior类,所包含的方法是stop,因此第(3)空填写的方法应该是wheel.stop()。这样就将Car与BrakeBehavior联系起来了。
(4)第四空是实现子类ShortWheelCar缺失ShortWheelCar(BrakeBehavior behavior)此带参构造方法的方法体:
class ShortWheelCar extends Car{
public ShortWheelCar(BrakeBehavior behavior){
(4);
}
/*其余代码省略*/
};
构造方法是对类的构造,带参构造函数一般是对其属性进行参数赋值,第四空将实现子类与其父类联系起来,子类继承父类属性wheel,此处应该是对参数wheel赋值,即(4)wheel=behavior
(5)第五空是实际调用测试过程,缺失方法名:
class StrategyTest{
public static void main(String[]args){
BrakeBehavior brake=new ShortWheelBrake();
ShortWheelCar car1=new ShortWheelCar(brake);
car1.(5);
}
}
由代码可知,car1是ShortWheelCar(brake)实例化的对象,类ShortWheelCar本身没有方法,只有默认继承父类的一个方法brake(),因此此处调用的是brake(),即(5)brake()。
包含此试题的试卷
你可能感兴趣的试题
Advancements in ( )have contributed to the growth of the automotive industry through the creation and evolution of self-driving vehicles.
-
- A.Artificial Intelligence
- B.Cloud Computing
- C.Internet of Things
- D.Big Data
- 查看答案
In project human resource management , ( )is not a source of power for the project manager.
-
- A.referent power
- B.expert power
- C.reward power
- D.audit power
- 查看答案
At the project establishment stage , the feasibility study mainly includes techinical feasibility analysis , ( ), operation environment feasibility analysis and other aspects of feasibility analysis.
-
- A.detail feasibility analysis
- B.opportunity analysis
- C.economic feasibility analysis
- D.risk analysis
- 查看答案
( )is a grid that shows the project resources assigned to each work package.
-
- A.Stakeholder engagement assessment matrix
- B.Requirements traceability matrix
- C.Probability and impact matrix
- D.Responsibility assignment matrix
- 查看答案
Xinhua News Agency reported in January 2022,Chian will further promote the developmet of a digital economy during the 14th Five-Year Plan eriod(2021-2025). The plan also emphasized industrial ( )transformation.
-
- A.digital
- B.networking
- C.intelligentize
- D.informatization
- 查看答案