0

I came across an interview question asking to design a word processor.

After my research I found Flyweight design pattern as an approach. I came up with below code (ignore syntax). But I am having hard time thinking of what will be my key and what will be my value for word processor. public class Format { public readonly string _fontname; public readonly string _weight; public readonly int _size;

    public Format(string fontname, string weight, string size)
    {
        _fontname = fontname;
        _weight = weight;
        _size = size;
    }
}

public class TextFromatInfo
{
    public _readonly Format _oFormat
    public TextFormatInfo ( Format oformat)
    {
        _oFormat = oFormat;
    }
    public Format GetFormat
    {
        get {return this._oFormat}
    }

    public void ApplyFormat(format Format)
    {
        console.writeline ("apply format fontname: " format.forntname +
           "size: " +  format.size + "weight : " format.weight
    }
}

public class TextFormatFactory
{
    public readonly IDictionary<Format, TextFormatInfo> _cache =
       new Dictionary <Format, TextFormatInfo>

    public TextFormatInfo GetTextFormatInfo(Format oFormat)
    {
        if (_cache.ContainsKey(oFormat)) return _cache[oFormat];
        var OTextFormatInfo= new TextFormatInfo(oFormat);
        _cache.add(OTextFormatInfo.GetFormat, OTextFormatInfo);
        return OTextFormatInfo ;
    }
}

public class TestFlyWeight
{
    private static TextFormatInfo[] formtInfo = new TextFormatInfo[100];
    private static TextFormatFactory ff;

    public void ProcessesWord(char c, string fontname, int size, string weight)
    {
    }
}

How would the above class look like? How can I complete the program by actually processing word?

1 Answers1

0

Honestly I think they were maybe not so much interested in patterns as in architecture. But you might have given sufficient material for them to digest.

I would say MDI is a key topic here: multiple document interface. Multiple tabs with several documents. Having one Document object per file system file (twice opening the same file), and possibly having several DocumentViews (swing: JTextPanes) per Document, bridged by the DocumentListener. In different tabs or in a vertically split single tab, so you may scroll to another spot and still remain at the first spot in split panes. _Specially stress, that this is se

Maybe building swing's EditorKit and StyledDocument.

So to make points of UI knowlegde, creativity, features, existing classes.

This all said, Patterns are an important asset in interviews too, I have experienced.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138