您的位置:首页 > 其它

适配器设计模式---对象适配

2013-12-27 12:49 239 查看
package cn.icer.simulation.ObjectAdapter;

/**
* 火箭
* @author icer
* @date 2013-12-27
*
*/
public abstract class SkyRocket {

private double burnTime;
private double mass;
private double thrust;

public SkyRocket(double mass, double thrust, double burnTime)
{
this.mass = mass;
this.thrust = thrust;
this.burnTime = burnTime;
}

public abstract double getMass();

public abstract double getThrust();

public abstract void setSimTime(double time);

}

package cn.icer.simulation.ObjectAdapter;

/**
* 火箭
* @author icer
* @date 2013-12-27
*
*/
public class PhysicalRocket {

private double burnArea;			//燃烧面积
private double burnRate;			//燃烧率
private double initFuelMass;		//燃料
private double totalMass;			//燃料总量

private double totalBurnTime;		//总燃烧时间

//重力加速度
private static double SPECIFIC_IMPULSE = 620;		// Newtons/Kg

//燃料密度
private static double FUEL_DENSITY = 1800;			// Kg / M**3

public PhysicalRocket(double burnArea, double burnRate, double fuelMass, double totalMass) {
this.burnArea = burnArea;
this.burnRate = burnRate;
this.initFuelMass = fuelMass;
this.totalMass = totalMass;

double initialFuelVolume = fuelMass / FUEL_DENSITY;

this.totalBurnTime = initialFuelVolume / (burnArea * burnRate);
}

/**
*
* @return 燃烧时间
*/
public double getBurnTime()
{
return totalBurnTime;
}

/**
*
* @param time 燃烧时间
* @return 现在剩余的燃料
*/
public double getMass(double time)
{
if ( time > totalBurnTime)
return totalMass - initFuelMass;

double burntFuelVolume = burnArea * burnRate * time;

return totalMass - burntFuelVolume * FUEL_DENSITY;
}

/**
*
* @param time 燃烧时间
* @return 点火以来产生的总推力
*/
public double getThrust(double time)
{
if (time > totalBurnTime)
return 0;

return FUEL_DENSITY * SPECIFIC_IMPULSE * burnArea * burnRate;
}
}

package cn.icer.simulation.ObjectAdapter;

/**
* 火箭
* @author icer
* @date 2013-12-27
*
*/
public class IcerRocket extends SkyRocket {

private double time;

private PhysicalRocket rocket;

public IcerRocket(PhysicalRocket rocket) {
super(rocket.getMass(0), rocket.getThrust(0), rocket.getBurnTime());

this.rocket = rocket;
}

@Override
public double getMass() {
return rocket.getMass(time);
}

@Override
public double getThrust() {
return rocket.getThrust(time);
}

@Override
public void setSimTime(double time) {
this.time = time;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  设计模式