How to implement classes that use the same variables

I have 6 classes which are all document types. All types of documents have the same 8 fields. There is one class consisting of only these 8 fields. The rest of the classes have more fields. Some of these classes have the same fields (next to 8 fields).

Example:

class Document: fields 1 to 8

class: fields 1 to 8 and field 9 and 10

WorkInstruction class: fields 1 through 8 and field 9 and 10

class Procedure: fields 1 through 10 and field 11

Hope this makes my point clear.

My question is, what's the best way to implement this? Should I create one or more interfaces or use abstract classes?

Thnx

0


source to share


3 answers


Either make Document a base form type, etc., or make a form, have additional fields and a link to a separate document (i.e. use composition instead of inheritance). In this case, it looks like inheritance is likely to be the best option, but it really depends on what you want to do with it. You have code to work on any document, including Form, WorkInstruction, etc.

EDIT: For example, let's say you have a UI control that can display the common parts of any document, i.e. common 8 fields. This will take a copy of the document. You may want to pass it a Form or WorkInstruction directly - in which case it is best to extract from the document. Alternatively, when rendering a Form or WorkInstruction, you can instantiate the control passing through the document part separately. those. the difference between this (in C # - you did not specify which language you are interested in):

class Form : Document { ... }

// Later on
Controls.Add(new DocumentDisplay(form));

      



and this:

class Form
{
    private Document Document { get; set; }
    // Other stuff
}

// Later on
Controls.Add (new DocumentDisplay(form.Document));

      

Without knowing the rest of your design, it's hard to recommend one or the other. I usually prefer composition over inheritance, but that sounds like a more natural inheritance situation.

+2


source


The old school answer would create a class with shared fields and inherit.

A fashionable answer would be to create a class that has these fiends and have it as a member.



Inheritance creates a lot of dependency between classes if you change the base each, which is considered bad practice.

In the simple case, it really isn't that much different. But if you start adding behavior to those fields, it might be.

0


source


Too little information to give a definitive answer

If the only thing that separates these objects is the data attributes ... I would use something like this.

class Document
{
   List<Field> m_Fields;
   public void AddField(Field f) {... }
   public void AddFields(Field[] f) {... }
}

class DocumentFactory()
{
  public static Document GetDocument()
  {
     Document d = new Document();
     d.AddFields(GetDocumentFields());      // private helper .. returns fields 1-8
     return d; 
  }
  public static Document GetForm()
  {
     Document d = new Document();
     AddDocumentFields(d);
     d.AddField(FIELD_9);
     d.AddField(FIELD_10);
  }
  // and so on..
}

      

Any code that works regardless of the document type goes into the Document class.
If in addition to that you have behavior that is specialized / dependent on a particular type of object, you will need to increase the inheritance type hierarchy (as John says .. this is similar to the IS-A relationship from names.)

0


source







All Articles