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

来源:互联网 发布:解救吾先生知乎 编辑:程序博客网 时间:2024/06/07 06:01

对象适配器:
Skyrocket是一个类,他所需要的一组方法在PhysicalRocket类中
通过一个Skyrocket的子类OozinozSkyrocket,在其内部定义一个PhysicalRocket变量,通过函数
调用PhysicalRocket方法或给变量赋值(可以是构造函数或其他)
这种称为对象适配器主要是利用对象来获取功能

代码:

package com.structuralPattern.adapter.edition2;public class OozinozSkyrocket extends Skyrocket{    private PhysicalRocket rocket;    public OozinozSkyrocket(PhysicalRocket p) {        super(            p.getMass(0),            p.getThrust(0),            p.getBurnTime());        rocket = p;    }    public double getMass(){        return rocket.getMass(simTime);    }    public double getThrust(){        return  rocket.getThrust(simTime);    }}


package com.structuralPattern.adapter.edition2;public class PhysicalRocket {    private double burnArea;    private double burnRate;    private double initialFuelMass;    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.initialFuelMass = fuelMass;        this.totalMass = totalMass;        double initialFuelVolume = fuelMass / FUEL_DENSITY;        this.totalBurnTime = initialFuelVolume / (burnRate * burnArea);    }    /**     * @return The remaining mass of the rocket after burning off a portion of     *         its fuel.     * @param time time since ignition     */    public double getMass(double t) {        if (t > totalBurnTime) return totalMass - initialFuelMass;        double burntFuelVolume = burnRate * burnArea * t;        return totalMass - burntFuelVolume * FUEL_DENSITY;    }    public double getThrust(double time) {        if (time > totalBurnTime) return 0;        return FUEL_DENSITY * SPECIFIC_IMPULSE * burnRate * burnArea;    }    /**     * @return the time this rocket's fuel burns.     */    public double getBurnTime() {        return totalBurnTime;    }}


package com.structuralPattern.adapter.edition2;public class Skyrocket {    private double mass;    private double thrust;    private double burnTime;    protected double simTime;    public Skyrocket(double mass, double thrust, double burnTime) {        this.mass = mass;        this.thrust = thrust;        this.burnTime = burnTime;    }    /**     * @return Model mass as reducing linearly from the initial mass down to 0     *         during the life of the fuel.     */    public double getMass() {        if (simTime > burnTime) return 0;        return mass * (1 - (simTime / burnTime));    }    /**     * @return Model thrust as constant for the life of the fuel.     */    public double getThrust() {        if (simTime > burnTime) return 0;        return thrust;    }    /**     * When the simulation updates its clock, hang onto the current time.     * @param t the time in the simulation     */    public void setSimTime(double t) {        simTime = t;    }}
阅读全文
0 0
原创粉丝点击