i'm modeling document class system. document can 1 of 2 types: in or out.
if type in, document has sender. sender can 1 of 2 types: person or company.
if type out, document has receiver. receiver can 1 of 3 types: person, company, department.
i'm not sure if better use property enumeration type of document or use hierarchy document base class , 2 classes each type of document.
for sender , receiver i'm not sure if hierarchy option because 3 types don't have in common (person, company, department) , how avoid invalid sender.
it if can give me advice how model document class or if can tell me design patterns should use.
thanks in advance.
there few differences between in , out, same fields exception of sender , receiver. also, behavior same little changes.
there no behavior sender , receiver, have contain correct object, example sender can contain person or company no department because departments not valid senders. also, if sender contains person, can't contain company because 1 sender accepted.
the main problem how read sender o receiver when document , have read data. example, if have read sender , use enumeration kind of sender have code if sender==person read person , assign person else read company , assign company. if use inheritance how avoid use cast or how know if sender person or company without code or cast. again.
basically boils down this. if there's going if statements like
if(document.isincoming()){ }elseif (document.senttodepartment()) { else }
repeated more couple of places you'd better of kind of polymorphic solution (think abstract class or interface). same thing sender , receiver types.
however, don't need subclass @ top. can have single document class different behavior attached through polymorphism. assume printing behavior different incoming , outgoing documents. create iprinter interface , implement in 2 classes follows.
public class document { documenttype type; iprinter printer; } interface iprinter{ print(); } class incomingprinter :iprinter{} class outgoingprinter :iprinter{}
whenever decided document going incoming (maybe when being created), assign incomingprinter. if there multiple types of behavior needs assigned factory pattern used. way localize if(doc.isincoming()) statements 1 place. benefits of not repeating decision @ different places in code many.
Comments
Post a Comment