John Davidson

php - Design patterns to override attributes of object and validate them

0 comments
Message:


GOAL:


I want to build a "market validation control engine" that validates players' actions and limits on the market.


SUMMARY:


Consider that we have a Character object that can buy only a given number of Item objects, e.g Arrow. The number depends on Race, Tire and Area where a lower layout of hierarchy overrides the upper; if the limit isn't specified lower layout inherits it from the upper:



For example:



  • Pro ELF in Forest can buy up to 2000 arrows

  • Basic Ork in Forest and Basic Ork in Desert can buy 50 arrows.


Additionally, the Character object can Buy and Sell a specific amount of arrows on the market. The amount and action depend on the Area. Otherway to say is every node might have its validation rules.
For example:



  • Basic Elf in Desert can sell 50% arrows

  • Basic Orc in Desert can sell 100% of the arrows.


Lastly, based on the Tier players can Exchange arrows to another item. For example :



  • Pro ELF can exchange 100% of its arrows


STRUGGLES:



  1. Building hierarchy

  2. Validation rules. Every layout of hierarchy (Tier or Area) have rules

  3. How to make 1 and 3 work together

  4. Scalability. Adding a new layout (e.g. Clan) or attaching a new validation rule to Area

  5. Maintenance


I am almost sure that I need multiple different patterns here. Currently, I'm thinking of the Decorator Pattern for creating hierarchy object with all the limits and the Chain of Responsibility for the rule validation. However, I am not sure how well these patterns will work together in the long ran.


UPD: In the example, I used Arrow - one of the hundreds of items on the market and Desert with Forest - two of the 9 areas.



My Answer:
One possible approach to address these struggles is to use the Strategy Pattern in combination with the Decorator Pattern and the Chain of Responsibility Pattern.

1. Strategy Pattern:
- Create a MarketValidationControlEngine class that contains a strategy object for each type of validation rule (e.g. BuyLimitValidationStrategy, SellLimitValidationStrategy, ExchangeLimitValidationStrategy).
- Each strategy object will implement a validate method that takes the Character object and the Item object as parameters and checks if the action is valid based on the rules for that specific type of validation.

2. Decorator Pattern:
- Create a hierarchy of decorator classes for the Character object that add the specific limits based on Race, Tier, and Area.
- Each decorator class will override the attributes of the Character object (e.g. buyLimit, sellLimit, exchangeLimit) based on the rules for that specific Race, Tier, and Area.

3. Chain of Responsibility Pattern:
- Create a chain of responsibility for each type of validation rule (e.g. BuyLimitValidationHandler, SellLimitValidationHandler, ExchangeLimitValidationHandler).
- Each handler in the chain will check if the validation rule applies to the Character object and the Item object. If the rule applies, the handler will validate the action; otherwise, it will pass the request to the next handler in the chain.

By combining these patterns, you can build a flexible and scalable market validation control engine that can handle complex validation rules based on the hierarchy of Race, Tier, and Area. Additionally, using these patterns will make it easier to add new validation rules or modify existing ones in the future.

Rate this post

4 of 5 based on 4026 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us