When should you really use the visitor pattern

后端 未结 5 1320
谎友^
谎友^ 2021-02-06 10:29

Ok before marking this as a duplicate let me clarify myself. I\'m reading about the visitor pattern and its applicable uses.

I\'ve stumbled upon this post: When should I

5条回答
  •  天命终不由人
    2021-02-06 10:53

    you can use the visitor pattern to design a db saver, like shown in this diagram here: class diagram for an object saver in different dbs

    you can choose what db to use, the client class will look similar to this:

        Visitor mongosaver = new MongodbSaver();
        Visitable user = new User();
        //save the user object using mongodb saver
        user.accept(mongosaver);
        Visitable post = new BlogPost();
        Visitor mysqlsaver = new MysqlSaver();
        //save the BlogPost using Mysql saver
        post.accept(mysqlsaver);
    

    you can refer to this also: https://www.oodesign.com/visitor-pattern.html

提交回复
热议问题