基本上,通过组合不同的组件来构造对象(struct).每个具体组件都可以被与界面匹配的另一个组件轻松交换(我想特性).
我目前正试图实现让我遇到一些错误的特性,并让我开始思考这是否是Rust中常见的事情.
// usage example
fn main() {
let obj = MainObject::new(Component1::new(), Component2::new(), Component3());
// Where each component is a type(struct) with some well predefined methods.
}
Run Code Online (Sandbox Code Playgroud)
这背后的主要思想是实现游戏中常用的组件模式.基本上游戏将包含许多不同的对象,行为和包含的数据略有不同.对象由标准组件组成,而不是具有大类层次结构,更完整的示例.
pub struct Container
{
input: InputHandlerComponent, // Probably a trait
physics: PhysicsComponent, // Probably a trait
renderer: RendererCompoent // Probably a trait
}
impl Container {
fn new(p: PhysicsComponent, i: InputComponent, r: RenderComponent) -> Container {
Container {input: i, physics: p, renderer: r}
}
}
struct ConcretePhysicsComponent;
impl PhysicsComponent for ConcretePhysicsComponent
{ …Run Code Online (Sandbox Code Playgroud) rust ×1