4

I have an object: X, that can be saved or loaded in various formats: TXT, PDF, HTML, etc..

What is the best way to manage this situation? Add a pair of method to X for each format, create a new Class for each format, or exists (as I trust) a better solution?

1
  • In what programming language would you be implementing the solution? I can suggest various methods, but a few of them are facilitated by specific functionalities of a language/platform... delegates in .NET, anonymous classes in Java, etc. Commented Dec 5, 2011 at 17:49

5 Answers 5

3

I'd choose the strategy pattern. For example:

interface XStartegy {
    X load();
    void save(X x);
}

class TxtStrategy implements XStartegy {
    //...implementation...
}

class PdfStrategy implements XStartegy {
    //...implementation...
}

class HtmlStrategy implements XStartegy {
    //...implementation...
}

class XContext {
    private XStartegy strategy;

    public XContext(XStartegy strategy) {
        this.strategy = strategy;
    }

    public X load() {
        return strategy.load();
    }

    public void save(X x) {
        strategy.save(x);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

What do you think if is X responsable of Strategy? I mean that X could have a nested interface XStrategy, an attribute xstrategy, and provide some commons implementations as final static like PDF_STRATEGY, TXT_STRATEGY and more.. ?
I don't really know why you would need constants when you have implementing classes like PdfStartegy, but you could solve this in any way you want, the patterns are just suggestions to a common way of solving known problems, and are not carved in stones. I believe the solution I gave here is the common way to implement it, but feel free to change it anyway you want. You can read more about it in here: oodesign.com/strategy-pattern.html
1

I agree with @DarthVader , though in Java you'd better write

public class XDocument implements IDocument { ...

You could also use an abstract class, if much behavior is common to the documents, and in the common methods of base class call an abstract save(), which is only implemented in the subclasses.

Comments

0

I would go with Factory pattern. It looks like you can use inheritance/polymorphism with generics. You can even do dependency injection if you go with the similar design as follows.

public interface IDocument
{
   void Save();
}

public class Document : IDocument
{


}

public class PdfDocument: IDocument
{
   public void Save(){//...}
}

public class TxtDocument: IDocument
{
   public void Save(){//...}
}

public class HtmlDocument : IDocument
{
  public void Save(){//...}
}

then in another class you can do this:

public void SaveDocument(T document) where T : IDocument
{
   document.save();
}

2 Comments

The problem is that my X object can born as TxtDocument, but the same object may be saved as PDF. With this solution if I load a TxtDocument, it can't saved as PDF, because both are IDocument, but a TxtDocument isn't a PDF.
isnt that the case, you should be able to save a txt document as a pdf document as well. no?
0

It depends on your objects, but it is possible, that visitor pattern (http://en.wikipedia.org/wiki/Visitor_pattern) can be used here. There are different visitors (PDFVisitor, HHTMLVisitor etc) that knows how to serialize parts of your objects that they visit.

Comments

0

I would instead suggest the Strategy pattern. You're always saving and restoring, the only difference is how you do it (your strategy). So you have save() and restore() methods that defer to various FormatStrategy objects you can plug and play with at run time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.