Hierarchy and Design Issues

I am modeling a document class for a system. A document can be one of two types: in or out .

If type is in , the document has a sender. The sender can be one of two types: a person or a company.

If the type is outside , the document has a receiver. The receiver can be one of three types: person, company, department.

I am not sure if it would be better to use an enumerated property for a document type or use a hierarchy with a document base class and two classes for each document type.

For the sender and receiver, I’m not sure that a hierarchy would be a good option, because the three types have nothing in common (person, company, department) and how to avoid an invalid sender.

It would be nice if you could give me some tips on how to model a document class or if you can tell me about some design templates that I should use.

Thanks in advance.


There are only a few differences inside and outside, the same fields, with the exception of the sender and receiver. In addition, behavior occurs with minor changes.

, , , , , , , . , , , .

, o, , . , , , , , == , . , , , . .

+5
5

. ,

if(document.IsIncoming()){
  do something
}elseif (document.SentToDepartment()) {
  do something else
}

, , - ( ). .

. , . , . IPrinter .

public class Document
{
  DocumentType type;
  IPrinter printer;
}
interface IPrinter{
  print();
}

class IncomingPrinter :IPrinter{}

class OutgoingPrinter :IPrinter{}

, (, ), IncomingPrinter. , , , factory. , if (doc.IsIncoming()) . .

0

, , .

ISender Person and Company. IReceiver , .

, , .

, . , . , , , . ( ) , in/out .

+1

, . ,

.

, (). . , , , Document (in out), (, , ).

.

, , . , , , . ( , , , )

EDIT:

: , . , . , , Java (, ..) , (, Entity ). , , , . - :

public interface Entity{...}
public class Document<T implements Entity>{}
+1

. - , - . Sender and Reciever. , , CompanySender. , , ( , ).

0

, .

, / , , , ( , InDocument, OutDocument, ).

, , , , ( ) . , , (, , , ). , , " ". - , F #, :

type PersonInfo = {Name:string ; Age:int}

type CompanyInfo = {Name:string ; Location:string} 

type sender =
    | Person of PersonInfo
    | Company of CompanyInfo

type DepartmentInfo = {Name:string ; Floor: int}

type receiver =
    | Person of PersonInfo
    | Company of CompanyInfo
    | Department of DepartmentInfo

type documentType =
    | In of sender
    | Out of receiver

type Document = {Type:documentType ; Title:string ; Body:string ; Date:DateTime } with 
    //example of how to process a Document using pattern matching
    override this.ToString() =
        match this.Type with
        | In(s) -> 
            match s with
            | sender.Person(info) -> sprintf "In / Person, extra info: Name = %s ; Age= %i" info.Name info.Age
            | sender.Company(_) -> "In / Company"
        | Out(r) ->
            match r with
            | receiver.Person(_) -> "In / Person"
            | receiver.Company(_) -> "In / Company"
            | receiver.Department(_) -> "In / Department"


//create a list of In and Out documents all mixed up
let documents = 
   [{Type = In(sender.Person({Name="John"; Age=3})); Title="My In Doc"; Body="Hello World"; Date=DateTime.Now}
    {Type = Out(receiver.Department({Name="John"; Floor=20})); Title="My Out Doc"; Body="Testing"; Date=DateTime.MinValue}]

//partition documents into a list of In and Out Types

let inDocuments, outDocuments = 
    documents |> List.partition (function | {Type=In(_)} -> true | {Type=Out(_)} -> false)
0

All Articles