I have a class (as a protobuf) OrderChange, that represents when an order (imagine Amazon.com) changes:
message OrderChange {
Order old_order = 1;
Order new_order = 2;
}
message Order {
OrderType order_type = 1;
OrderCategory order_category = 2;
OrderStatus order_status = 3;
// many more fields
}
enum OrderType {
ORDER_TYPE_RETAIL = 0;
ORDER_TYPE_BUSINESS = 1;
}
enum OrderCategory {
ORDER_CATEGORY_ELECTRONICS = 0;
ORDER_CATEGORY_FOOD = 1;
ORDER_CATEGORY_FURNITURE = 2;
ORDER_CATEGORY_FITNESS = 3;
ORDER_CATEGORY_HOUSEHOLD = 4;
}
enum OrderStatus {
ORDER_STATUS_PAID = 0;
ORDER_STATUS_SHIPPED = 1;
ORDER_STATUS_DELIVERED = 2;
}
For each OrderChange object, I want to trigger some code for each conditional. For example if Order is RETAIL, FURNITURE, and PAID, I want to send a specific email.
OrderType and OrderCategory probably won't change between old_order and new_order, so my code can look at only new_order for these fields. Other fields such as OrderType will change, and my code can compare old_order and new_order to know what changed.
My problem is that Order has many fields, each with many values, so the number of conditional combinations is huge. Using only if/else or switch/case would be unmaintainable code.
So my question is, what pattern can I use to make these conditionals more maintainable?
Maybe I can break the fields into handlers - a handler for each OrderType, then each of these handlers contain a list of handlers for each OrderCategory, and continuing to nest a single field for each level until there are no more fields. My issue would be that beyond maybe the OrderType being the highest level field, there is no clear hierarchical relationship for the other fields. As in, it's not clear that OrderCategory handlers should contain OrderType handlers.
How should I design this?
Aucun commentaire:
Enregistrer un commentaire