设计模式-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
- 设计模式-23-2-对象适配器
- c++设计模式--对象适配器模式
- C++设计模式----对象适配器模式
- 【面向对象设计模式】 适配器模式 (二)
- [设计模式](五):适配器模式(Adapter)[类适配器、对象适配器、接口适配器]
- 适配器模式(对象适配器)
- 适配器模式-对象适配器
- 【初学设计模式】Object Adapter (对象适配器)
- 设计模式11:对象适配器【结构型】
- 适配器设计模式---对象适配
- JAVA设计模式-适配器(类适配器、对象适配器)模式-Adapter
- C++设计模式——适配器模式(对象适配器、类适配器)
- 【GOF23设计模式】_适配器模式、对象适配器、类适配器、开发中场景JAVA235
- 设计模式学习--适配器模式(类适配器和对象适配器)
- 设计模式之适配器模式(Adapter):类适配器、对象适配器
- 23种设计模式(2):适配器模式
- 面向对象的设计模式(九),适配器模式
- 设计模式之类对象结构型 — ADAPTER (适配器)模式
- 图---存储方式四
- 关于springboot-actuator监控的401无权限访问
- Java基础拾遗(一)
- process与thread的关系
- 华为机试——字串的连接最长路径查找
- 设计模式-23-2-对象适配器
- Java基础之方法的重写
- 20170729_map 的简单操作
- Activity之间的数据传递
- 数学基础:HUD1406-完数
- 欢迎使用CSDN-markdown编辑器
- hdu4569(优化,数学题,枚举)
- 从保护模式切换到实模式遇到了大问题,求解
- 机器学习资料发布(2017年7月29日第一次)