Which design pattern should I use and why?

37 Views Asked by At

Pharmacy sells medicine from different producers. Each medicine has a name, type (antibiotics, anti-inflammatory, stomach), price etc. However, imported medicine must have a certificate from a lab in the current country. Moreover, if customer purchases an imported medicine, the pharmacy worker has got to query the medicine database and see the data about the counterpart medicine from domestic producers (which are usually cheaper).

How do we develop a class structure to handle medicine data in this pharmacy?

1

There are 1 best solutions below

0
On

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
    }
}