I found Applying UML and Patterns by Craig Larman a very good book to learn about design, patterns and implementation.
If you want a quicker solution rather than reading an entire book, just prototype something: Create some classes, figure out what your interface will be etc. Try some stuff out to see what would be easiest to implement/maintain.
A possibility:
You have a ProductService
which has a FindCounterpartMedicines()
. If the case described in your question is the only special case, it can be achieved with just a simple if
: Don't complicate things if it is not necessary but "Do the simplest thing that could possibly work" (link).
if (medicine.IsImported) {
result.CounterPartMedicines = FindCounterpartMedicines(medicine);
}
If each medicine has some different special logic, you can use polymorphism to avoid a list of
if (medicine.SomeProp) doSomePropLogic();
if (medicine.SomeOtherProp) doSomeOtherPropLogic();
Inheritance example
interface Medicine {
BeforeBuyLogic();
}
class Antibiotics : Medicine {
BeforeBuyLogic() {
// check doctor subscription
}
}
class StomachMed : Medicine {
BeforeBuyLogic() {
// check customer allergies
}
}