简体   繁体   中英

Best design pattern to implement upload feature

I am working on a web application which is based on spring MVC. We have various screens for adding different domain components(eg. Account details, Employee details etc). I need to implement an upload feature for each of these domain components ie to upload Account, upload employee details etc which will be provided in a csv file (open the file, parse its contents, validate and then persist).

My question is, which design pattern should i consider to implement such a requirement so that upload (open the file, parse its contents, validate and then persist) feature becomes generic. I was thinking about using the template design pattern. Template Pattern

Any suggestions,pointers,links would be highly appreciated.

I am not going to answer your question. That said, let me answer your question! ;-)

I think that design patterns should not be a concern in this stage of development. In spite of their greatness (and I use them all the time), they should not be your primary concern.

My suggestion is for you to implement the first upload feature, then the second and then watching them for what they have that is equal and create a "mother" class. Whenever you come to a third class, repeat the process of generalization. The generic class will come naturally in this process.

Sometimes, I believe that people tend to over engineer and over plan. I am in good company: http://www.joelonsoftware.com/items/2009/09/23.html . Obviouslly, I am not advocating for no design software - that never works well. Nevertheless, looking for similarities after some stuff has been implemented and refactoring them may achieve better results (have you already read http://www.amazon.com/Refactoring-Improving-Design-Existing-Code/dp/0201485672/ref=sr_1_1?ie=UTF8&qid=1337348138&sr=8-1 ? It is old but stiil great!).

You could use an Abstract Factory pattern.

Have an upload interface and then implement it for each of the domain objects and construct it in the factory based on the class passed in.

Eg

Uploader uploader = UploadFactory.getInstance(Employee.class);

A strategy pattern my be useful here for the uploader. The Uploader class would be a sort of container/manager class that would simply contain a parsing attribute and a persistance attribute. Both of these attributes would be defined as an abstract base class and would have multiple implementations. Even though you say it will always be csv and oracle, this approach would be future-proof and would also separate the parsing/verifying from the persistence code.

Here's an example:

class Uploader
{
private:
    Parser parser_;
    Persistence persistence_;

    void upload() {
        parser_.read();
        parser_.parse();
        parser_.validate();
        persistence_.persist(parser_.getData());
    }

public:
    void setParser(Parser parser) {parser_ = parser;}
    void setPersister(Persistence persistence) {persistence_ = persistence;}
};

Class Parser
{
    abstract void read();
    abstract void parse();
    abstract void validate();
    abstract String getData();
};

class Persistence
{
    abstract persist(String data);
};

class CsvParser : public Parser
{
    // implement everything here
};

// more Parser implementations as needed

class DbPersistence : public Persistence
{
    // implement everything here
};

class NwPersistence : public Persistence
{
    // implement everything here
};

// more Persistence implementations as needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM