# 组件模式

“允许一个单一的实体跨越多个不同域而不会导致耦合”

在ECS框架中非常常见，是最基本的东西了。

总之就像这样类似的写法

```cpp
class GameObject
{
public:
    // ...

    GameObject(AComponent* aCmpt, BComponent* bCmpt, CComponent* cCmpt)
    : aCmpt(aCmpt), bCmpt(bCmpt), cCmpt(cCmpt)
    {}
    void update()
    {
        aCmpt->update();
        bCmpt->update();
        cCmpt->update();
    }
private:
    AComponent *aCmpt; 
    BComponent *bCmpt;
    CComponent *cCmpt;
};
```

完全可以用 `BaseComponent* arr[]` 数组存，update的遍历所有Cmpt调用其update。

## 对象如何获得组件

* 类自己创建自己的组件，比如 Init 时，一个一个AddCmpt 到数组内，而且还能把GameObject自己的this指针传给各个Cmpt，然后各Cmpt下就能访问到自己属于哪个GameObject，直接访问调用其他Cmpt
* 由外部代码提供。

## 组件之间如何传递信息

* 通过修改容器对象的状态
* 直接互相引用，通过GameObject的指针
* 通过消息传递
