In article <dd7c3f23-8c16-4cfa-b456-efc7d2f35a25
@r24g2000vbn.googlegroups.com>,
says...
>
> Hi there,
>
> Let say I am writing a word processor. It would be a bad design to
> consider that a document is a std::vector of instances of a class
> MyChar ? Where a MyChar would look like:
>
> struct MyChar
> {
> char TheChar;
> bool Bold;
> bool Italic;
> unsigned char Color[3];
> };
>
> This design is hardcoded (what if I need another attribute later on)
> and it would be very difficult to look for a specify array of char. It
> would also eat a lot of memory...
> Is this something describe in the literature ? If yes, where can I
> find documentation ?
FlyWeight has already been mentioned, but it's really only part of
the answer.
IMO, you should take a look at the general design of CSS and/or ODF.
They're obviously not identical, but they have some characteristics
on common, and give an idea of how this general type of problem has
been solved (with a reasonable degree of success, I might add)
already.
The general idea can be described fairly simply though: a couple of
extra levels of indirection, at least one of them explicitly visible
to the user. Specifically, you want to allow the user to put together
a "style sheet" that describes characteristics of some text (font,
color, spacing, etc.) then you want to let them apply that style to
lots of different text. The user can then format most of his/her
document(s) based on a logical style rather than always separately
editing the individual attributes. For example, you could start by
stealing a few "styles" from HTML, and having H1, H2, H3 and H4.
Much like a browser, give each of these a default style -- but let
the user edit styles, so if the user wants H1 to be 32 point Bodoni
bold, and H2 to be 27.5 point Castellar MT black, s/he can do that.
Then when they change their mind and decide those should both be
Arial bold, in 30 and 20 points respectively, they can easily do that
-- and when they change the style, every instance of H1 or H2
throughout their document will automatically change together.
Likewise, of course, you want to let the user define new styles, so
the styles can and will reflect the logical structure of the document
they're creating, in the way they think about things. The style one
user calls "chemical formula" might happen to have identical
attributes to another user's "algorithm", but they're still separate
styles that happen (at least at some particular moment) to look the
same.
--
Later,
Jerry.