Two-way OOP design-pattern -
this kind of general question, let me formulate in particular scenario. coding pricing system. in 1 hand there several "engines", on other, several "instruments". created class hierarchies both of them.
my problem comes want implement pricing mechanism. given specific engine , specific instrument, determine algorithm calculate price. nice code in 2-entries table. both instances contain vital information, none seems more important other. in oop methods belong 1 class, see no decent design deal this: "pricing" method should in instruments class, or engines?
whatever way go have same problem. suppose decide should in engines. then, inside pricing methods should have 'if-then-else' instruction dealing each possible instrumentclass? seems way ugly (and tight coupling!), specially if going have check like
isinstance(inst, instrclass1)
i can imagine similar situations arise in many contexts. best design-pattern deal them?
rephrased vehicle + circuit example
say have, on 1 hand, class hierarchy of vehicles. there motorbikes, cars, etc. particular object may yahama ray, instance of motorbike, vehicle.
on other hand, have circuits. , classes lemans, jerez, lagunaseca, etc.
what want simulate performance of given vehicle in given circuit. instance, suppose output time takes vehicle complete 1 lap. motorbikes , cars physics different, have different algorithms. suppose in circuits need take account factors in other irrelevant. so, when know details of vehicle , circuit, can simulate. cannot ask circuit "simulate itself" , time, ask vehicle "simulate itself" , give time, , cook final answer two.
hopefully sheds light on trying do.
there no universal answer question solution domain-specific.
however, general guideline, if there logic doesn't fit in existing object missing additional concept , concept represented stateless service, not always.
for instance, let's there discount of 15%
when engine1
, instrument1
bought together.
obviously, discount logic doesn't belong either of engine1
or instrument1
, have order
contains both items , order have way apply discounts. discount
s inspect order
know if application policy satisfied , return discount amount specific order.
class order { ... public void applyapplicablediscounts(list<discount> discounts) { (discount discount : discounts) { if (discount.isapplicableto(this)) { this.discounts.add(discount); } } } public price netprice() { return sumallorderlineprices(); } public price totalprice() { money discountamount = sumalldiscountamounts(); return netprice().deduct(discountamount); } } class engine1boughtwithinstrument1discount implements discount { public boolean isapplicableto(order order) { return order.containproducts(/*engine1, instrument1*/); } public money amountfor(order order) { return order.netprice().percentageof(15); } }
now, vehicule + circuit example it's same thing. have stateful simulation
or stateless simulationservice
can run simulation given vehicule, circuit, wheter, physic laws, etc. complex, these various collaborators should allow component performing simulation query them in order gather details needs perform simulation.
unfortunately, not know domain , physic laws, simple example (probably silly), simulation service query circuit know terrain @ specific point in , ask vehicule it's wheel's surface , shape know how friction generated.
Comments
Post a Comment