简体   繁体   中英

composite pattern computer game

For a computer game the rewards for the player as he/she progresses through the game are:

Entry level: Lamp worth n points Intermediate level: magic mat m points Advanced level: treasure chest which can be filled with lamps or mats

Here we apply the composite pattern

Client: computer game

I believe that the component are the levels (entry, intermediate and advanced) Composite: Rewards Leaf: lamp,magic mat,chest Operation: getPoints(), fillChest()

Are these correct?

A Composite is typically a tree/collection of objects. The same operations you can perform on the individual objects can also be performed on the collection. Additionally, the object can typically be stand-alone or contain more objects and operations on the containing object will be performed on the objects is contains.

So if that is how you plan to use your Composite objects, then yes it sounds correct.

I agree with Brady. All depends on how you plan to use your Composite objects. I used the composite design pattern for an RPG attributes system. All you need to do is create different attributes and assign each of them an Attribute instance. After that, it's all about adding and removing bonuses to it through the already created methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM