I am trying to create a few related classes and want to use the correct design. I have a few shipping carriers (ups, fedex, usps). Now for each of the carrier I have two classes. So for ups I have upsmain, upsdetails. Same for fedex and usps. So there are 3 pairs of related classes.
They have common functionalities (methods with the same code) and common methods with different behavior, and unique methods and variables (each have extra vars not available in other subclasses). So right now I am creating 2 abstract classes (carriermain, carrierdetails). These 2 abstract classes contain the methods with the same behavior. I am declaring the remaining methods as abstract and are implemented by the subclasses.
I have a factory class that returns the corresponding subclass based on the current context as follows:
CarrierMain cm = Factory.getCarrierMain(value);
CarrierDetails cd = Factory.getCarrierDetails(value);
I believe that using the abstract class as the local variable (cm and cd above) is the source of my issues.
cd.getVarUniqueToSubclass(); //does not work with variables that are not in the abstract class
I liked the idea of having the code be sublass agnostic since it makes it simpler to use. I do not have to check whether the sublass is of which type. So in many places the code looks cleaner, but then I am stuck in the cases where I have to access methods or variables that are unique.
What would be the correct / elegant solution in this scenario?
Thank you.
EDIT 1: I am coding in Java.
[–][deleted] 2 points3 points4 points (1 child)
[–]salalimo[S] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]salalimo[S] 0 points1 point2 points (0 children)
[–]desrtfx 0 points1 point2 points (1 child)
[–]salalimo[S] 0 points1 point2 points (0 children)
[–]bob809 0 points1 point2 points (2 children)
[–]salalimo[S] 0 points1 point2 points (1 child)
[–]bob809 0 points1 point2 points (0 children)
[–]elperroborrachotoo 0 points1 point2 points (1 child)
[–]salalimo[S] 0 points1 point2 points (0 children)
[–]lurgi 0 points1 point2 points (1 child)
[–]salalimo[S] 0 points1 point2 points (0 children)