0

I have a static class (VS 2010/C#) used to generate pdf documents from a template. We use iTextSharp APIs to generate the pdf. The class is static, providing only a public method to generate a specific document.

Now we need to implement a new document class to generate a different kind of document. I would like to create a base class, since some methods will be shared by the two classes, while others will be specific to each document type.

How would be the better approach in order to keep the specific (sub)class static and avoid making the base class public or invokable from outside the specific classes?

I am currently looking into Factory Pattern or composition.

EDIT: Here the current specific class code:

public static class SpecificDocGenerator
{
    /// <summary>
    /// Generates an international certificate.
    /// </summary>
    public static byte[] GenerateItDoc(DocInfo info)
    {
      //Here the document is generated and returned as byte array
    }


    /// <summary>
    /// Gets the resource.
    /// </summary>
    private static byte[] GetResource(string name)
    {
        //Gets a local resource (as example an image)
    }

I would liekt to create a base class for common methods (as example GetResource). But I would like to keep the specific class static or avoid making more instances of it.

Francesco
  • 9,947
  • 7
  • 67
  • 110

2 Answers2

0

Static methods aren't virtual, and can't be overridden, so you can't have a base class with abstract static methods. If you want to have a class hierarchy then you'll need to make the class, and at least some of the methods, non-static. Having said that, it will still be possible to leave a single public static method as the entry point, and to have the instance methods be used entirely from within the scope of that single static method.

Servy
  • 202,030
  • 26
  • 332
  • 449
0

You can use Singleton pattern instead of static class. This will allow you to use inheritance:

public sealed class SpecificDocGenerator : AbstractDocGenerator
{
    private static readonly SpecificDocGenerator _instance;

    static SpecificDocGenerator
    {
       _instance = new SpecificDocGenerator();
    }

    public SpecificDocGenerator Instance
    {
        get { return _instance; }
    }

    private SpecificDocGenerator()
    {
    }

    public byte[] GenerateItDoc(DocInfo info)
    {
       //Here the document is generated and returned as byte array
    }

    private byte[] GetResource(string name)
    {
        //Gets a local resource (as example an image)
    }
}

You can use other Singleton implementation, but this is simplest. Usage:

SpecificDocGenerator.Instance.GenerateItDoc(info);
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459