How to organize my classes / inheritance / implementation?

I am currently working on a school project. In this project, we must first reflect the world from the point of view of the doctor , including his patients, various pharmaceutical preparations (with interfaces such as liniment, tablets and injections), prescriptions for medicines and containers for prescriptions and persons.

Here is what the first part of the assignment says: A pharmaceutical product has a name, a unique number, and a price. A pharmaceutical drug is of type A (a drug with an int associated with it, representing as a drug / potent drug), type B (highly addictive with an associated int representing a drug dependence) or type C ("normal" without an associative int that exist two other types).

Types A, B, and C must be implemented as interfaces. My problem is how I should structure the classes; I strongly disagree that this is the best way to organize classes, but by exploring different approaches, I am puzzled by what is the best implementation .

I want to do something like this. A pharmaceutical can be an abstract class with 3 specific subclasses:

                               PharmaceuticalDrug

                            /           |           \

                       Liniment        Pill       Injection

My problem is how to implement type A, type B, and type C due to the instance variables associated with these types. For types A and B, an additional instance variable is required to represent their "potency", while C is not . I could just include an instance variable and set an instance variable of type C for a non-functional value (e.g. -1) to indicate that this value should not be used, but this approach does not seem like a pretty good solution to me, as I will write recipes with all the information in the files later and would like to easily skip and do it without unnecessary tests and special cases for type C.

. , A, B C , , , , , . , . , . , .

- : (: , / , PharmaceuticalDrug, , , ).

    public class Liniment extends PharmaceuticalDrug {
        ...

        public Liniment( String type ) {
            if ( type.equals( "A" ) ) {
                // make this class implement type A
            } else if ( type.equals( "B" ) {
                // make this class implement type B
            } else if ( type.equals( "C" ) { // type C
                // make this class implement type C **WITHOUT INSTANCE VARIABLE!**
            } else {
                System.out.println( "Error!" );
            }
        }

        ...
    }

, , , , - A, B C. , , , C. , A B, C / .

, , : Liniment, Pill Injection, I.E. LinimentTypeA, LinimentTypeB, LinimentTypeC, PillTypeA,..., InjectionTypeC. , , , .

A, B C , / , A B, , C

, ?

+3
3

C (, -1)

. "" . - "". , - . , Drug. A B int. : int potency A int addictivity B.

, Type A, B C

, Drug - . TypeA, TypeB TypeC , . , , .

.

, Java. , , , : public class Marjuana implements TypeA, TypeB

+1

- :

  • , .
  • A B, , getPotency() -
  • A B , C -
  • , .

LinimentTypeA .. Factory, Liniment (), , :

class DrugFactory {
  public Liniment getLinimentInstance(String type) {
    switch (type) {
    case "A": return new Liniment() { // your overrides and other stuff here } ;
    // etc
    default: return null;
  }

  public Pill getPillInstance(String type) {
    // similar
  }

  // other stuff you might want
}
+1

You might think:

  • Add the potency variable to the drug superclass
  • Define three interfaces (TypeA, TypeB, TypeC)
  • Define interfaces for Liniment, Pill, Injection
  • Then subclass PharmaceuticalDrug types: TypeALiniment, TypeBLiniment, TypeCLimiment, etc.

where do you use public class TypeALiniment extends PharmaceutialDrug implements TypeA, Liniment{xxx}

0
source

All Articles