【发布时间】:2023-03-04 23:34:02
【问题描述】:
我正在尝试使用 angular 将一些 java 移植到一个 cordova 应用程序,并且遇到了循环依赖问题 - 经常被问到,但老实说,我仍然没有更聪明。
我有两个对象 - 一个天体和一个可以相互引用的轨道对象。
class Orbit {
double a; // Semi Major axis
double e; // Eccentricity
Body primary; // The primary body around which the orbit is centred
double getPeriod() {
if (a < 0) {
return Double.POSITIVE_INFINITY;
} else {
return 2 * Math.PI * Math.sqrt(a * a * a / primary.mu);
}
}
}
class Body {
double mu; // Gravitational parameter
String name;
double radius; // Surface radius
Orbit orbit;
double getSphereOfInfluence() {
if (orbit.primary == null) {
return Double.POSITIVE_INFINITY;
} else {
return orbit.a * Math.pow(mu / orbit.primary.mu, .4);
}
}
}
如果我们以月球为例,天体“月球”的轨道以“地球”为主要轨道,而地球又以“太阳”为主要轨道,以此类推。
[IMO] 的好处是我可以拥有一组静态对象,因此如果我创建多个轨道对象,它们都使用相同的主体,而不是复制所有属性。这也意味着我可以可靠地通过系统。
我看到各种 cmets 说循环依赖是“坏的”,我读过 Misko's blog about the subject,但我在两件事上苦苦挣扎 - 为什么它们是“坏的”,以及我如何将我的代码重构为“好”的代码,使用他的 A、B、C 方法。
更新 - 我的结果
angular.module('starter.services', [])
.service("Bodies", function(Body, Orbit) {
this.KERBOL = new Body({name: "Kerbol", mu: 1.17233279483249E+18, period: 432000, radius: 261600000});
this.KERBIN = new Body({name: "Kerbin", mu: 3531600000000, period: 21600, radius: 600000, orbit:
new Orbit({a: 13599840256, e: 0, primary: this.KERBOL})
});
this.find = function(name) {
var found = null;
angular.forEach(this, function(item, key) {
if ((found == null) && (item instanceof Body) && (item.name === name)) {
found = item;
}
});
console.log("Found body: " + JSON.stringify(found));
return found;
};
})
.factory("Body", function() {
function Body(params) {
if (params) {
this.mu = params.mu;
this.name = params.name;
this.period = params.period;
this.radius = params.radius;
this.orbit = params.orbit;
}
};
Body.prototype = {
get primary() { return this.orbit.primary; },
getEscapeVelocity: function() {
return Math.sqrt(2 * this.mu / this.radius);
},
getOrderValue: function() {
var v = this.orbit.a;
var body = this;
while ((body = body.orbit.primary) !== null) {
v += body.orbit.a;
}
return v;
},
getSphereOfInfluence: function() {
if (this.orbit.primary == null) {
return Number.POSITIVE_INFINITY;
} else {
return this.orbit.a * Math.pow(this.mu / this.orbit.primary.mu, .4);
}
},
getSurfaceGravity: function() {
return this.mu / (this.radius * this.radius);
},
getSynchronousAlt: function() {
return this.getSynchronousRadius() - this.radius;
},
getSynchronousRadius: function() {
throw "Not implemented";
}
};
return (Body);
})
.factory("Orbit", function(Body) {
function Orbit(params) {
if (params) {
this.a = params.a;
this.e = params.e;
this.epoch = params.epoch;
this.i = params.i;
this.omega = params.omega;
this.lan = params.lan;
this.M0 = params.M0;
this.M = params.M;
this.primary = params.primary;
}
};
Orbit.prototype = {
};
return (Orbit);
})
});
【问题讨论】:
-
惊喜:JavaScript 问题中的 Java 代码 ;) 如果我们能看到一些实际的 javascript 代码,那将很有帮助。
-
公平点 - 我没有发布 JS,因为我想我对如何重构代码更感兴趣,但问题不应该是 javascript。无论如何,仅仅问这个问题就让我想到了,我意识到问题是静态的 Body 声明是导致错误的原因,所以我把它们移到了一个单独的工厂。我会发布生成的代码,看看它是否让任何人哭泣!
-
循环依赖在哪里?循环依赖意味着 instance A 引用实例 B,而实例 B 又引用 A。据我所知,您在此代码中没有这种情况。对我来说,这段代码看起来还不错。
-
嘎——那天我的大脑显然崩溃了,这是我必须回来的第一次机会——我发布了我错误地工作的代码,而不是带有 cdep 错误的代码。基本上我得到了它的工作,但与(IMO)一些妥协。
标签: javascript angularjs factory