Flyweight vs object pool patterns: When is each useful?

前端 未结 3 524
抹茶落季
抹茶落季 2020-12-29 23:54

As far as I know the object pool is a creational pattern and the flyweight is a structural pattern, but actually I can´t see very much difference between the two. Could some

3条回答
  •  灰色年华
    2020-12-30 00:19

    One difference in that flyweights are commonly immutable instances, while resources acquired from the pool usually are mutable.

    So you create flyweights to avoid the cost of repeatedly create multiple instances of objects containing the same state (because they are all the same, you just create only one and reuse it throughout all places in your app), while resources in a pool are particular resources that you want to control individually and possibly have different state, but you don't want to pay the cost of creation and destruction because they are all initialized in the same state.

提交回复
热议问题