[Tutor] What Design Pattern for Document class (NOT URGENT)

Alan Gauld alan.gauld at btinternet.com
Sun Sep 12 21:37:18 CEST 2010


"Karim" <karim.liateni at free.fr> wrote

> My first idea is to create an *docrules* class which holds my 
> document.

Why not create a Document class that is your document.

> This class will use 2 other instances from 2 respectives classes
> reader and writer. The main method of  reader is get().
> The main method of writer is write(). To resume I have:

And then the document could know how to get(why not read?)
and write itself? (Possibly using other helper classes but
we don't need to worry about that yet)


> The *docrules* will do the parsing (*xml.etree* very effective)
> and create the reader and the writer instance.

Maybe the document knows how to parse itself? Maybe using
some supporting rules class, but maybe not.

> I want to create 2 others classes (or more) more specific which have
> more the knowledge about the XML documents structures.
> *SpecificRuleReader*, *SpecificRuleWriter* which are deriving resp.

Maybe. Or are they subclasses of Document for some specific
type of XML? (Either approach is valid depending on how many
different rulesets you need.)

> My purpose is to separate as far as possible XML representation from
> core classes.

Thats a good idea, but creating an abstracted document class is a
good way of hiding your users from the internal details of how the
reading/writing and parsing is done.

>  I don't want necessarily have 2 big classes which
> know everything about XML document because I will have too many
> methods.

That depends on how you build the parsing of the document,
if you focus on what your user wants to do it should not be too many.
But if you expose the XML internals to your users it surely will.
Remember to design your objects interface from the users perspective
not based on the implementation!

Its fine to use helper objects - like rules, parsers etc - but you 
want
to shield your client code from them as much as possible.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list