简体   繁体   中英

What design pattern is good for applying attributes to a class that changes its behavior?

I am trying to find the best way to structure my project. What I would like to do is have a "Structure", which is made of multiple "part". These parts also will have separate attributes, for example it could be spinning, hovering, vibrating, jumping, shaking, growing, accelerating.

So for example, I might have a "structure" called human that is made of 6 "parts". I am going to set each part to one of the available options, so in this case, 2 parts are going to be arms, 2 parts are going to be legs, one part is a torso, and one part is a head. Now, I want to be able to apply the attribute spinning to one of the parts, the arm. So now I should have a human with a spinning arm.

Additionally, I would like other people to be able to create their own cusom attributes, structures, and parts.

I would like to get an idea on how I should structure my code, and apply an attribute to a part. Additionally, what design pattern would be good for this?

The requirement is to have a single interface for a composition of various objects / components.

There are 2 suitable design patterns for this requirement.

  1. Facade ( https://dzone.com/refcardz/design-patterns?chapter=17 )
  2. Composite Object ( https://dzone.com/refcardz/design-patterns?chapter=15 )

Facade pattern can be used when the way in which the internal components are assembled must be abstracted.

Composite object pattern can be used when the assembly of the components must be advertised.

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