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?