Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suitable design pattern for matrix-like feature

Which design pattern or combination of patterns would be correct or most suitable for the following common scenario:

Let's say I have a method which places an order. I have different order types and different countries. The method's implementation differs based on order type and also country.

My idea is to use the strategy pattern where I could have objects like OrderType1Country1Strategy, OrderType1Country2Strategy, OrderType2Country1Strategy etc.

My problem with this is that any two class can have common code which I'm not sure how best to handle. Any ideas for that or alternative patterns? Could decorator be used somehow?

like image 420
Elz Avatar asked Dec 06 '25 03:12

Elz


1 Answers

You may want to take a look at the Bridge design pattern which provides a way to avoid the explosion of the number of (sub)classes.

Basically, you could separate the hierarchies of order and country classes and compose the two via bridge.

like image 51
Dragan Bozanovic Avatar answered Dec 11 '25 20:12

Dragan Bozanovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!