希望使用函数式编程来解决重复代码

我有一些代码经常以相同的模式重复,我将其作为伪代码包含在下面。


我有许多方法具有相同的事务代码(见 A.),然后是 DB 代码,每种方法总是不同的(见 B.)。B处的代码需要在匿名类中执行。


public static Foo doMyDatabaseStuff(ApplicationContext context, WebSite webSite) {


    //A. Do a bunch of boilerplate Transaction stuff


    TransactionUtils transactionUtils = getTransactionUtils(context);


    Foo foo = (Foo) transactionUtils.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus ts) {


            //B. Do a bunch of use case specific DB stuff


            FooDao fooDao = (FooDao) context.getBean("FooDao");

            Foo foo = fooDao.create(webSite);

            {

                foo.setName("Administratoren");

                foo.setSite(webSite);

                //call a bunch of other setters

                fooDao.update(foo);


                return foo;

            }

        }

    });

    return foo;

}

我觉得这是函数式编程的完美候选者。我有一堆要执行的代码,但我想将它包装在其他一些函数中(即使用高阶函数)。


我天真地尝试使用 Function 接口产生了这个:


public Foo doMyDatabaseStuff(ApplicationContext context, WebSite webSite) {


    Function<ApplicationContext, Foo> func = new Function<ApplicationContext, Foo>() {


        public Foo apply(ApplicationContext context) {

            FooDao fooDao = (FooDao) context.getBean("FooDao");


            Foo foo = fooDao.create(webSite);

            {

                foo.setName("Administratoren");

                foo.setSite(webSite);

                //call a bunch of other setters

                fooDao.update(foo);


                return foo;

            }


        }

    };


    return DefaultContents.executeTransaciton(context, func);

}

我觉得这走错了路,因为


'executeTransaciton' 方法被锁定为使用 'Foo' 对象。我猜它应该返回Object。

函数泛型有点奇怪。我想从 doMyDatabaseStuff 方法(即封闭方法范围)的参数中传入我的所有变量,而不是通过apply(param)方法传入。

看起来我在样板代码中并没有节省那么多,我仍然需要将这个庞大的“函数”匿名类代码添加到每个方法中。

我在这里是在正确的轨道上吗?......还是完全偏离了?


RISEBY
浏览 136回答 2
2回答

蝴蝶刀刀

如果所有不同的是您在 中所做的TransactionCallback,只需将TransactionCallback作为参数传递给执行“样板事务”的方法,然后执行TransactionUtils:private static Foo doMyDatabaseStuff(ApplicationContext context, WebSite webSite, TransactionCallback cb) {&nbsp; &nbsp; //A. Do a bunch of boilerplate Transaction stuff&nbsp; &nbsp; TransactionUtils transactionUtils = getTransactionUtils(context);&nbsp; &nbsp; return (Foo) transactionUtils.execute(cb);}并从您现有的方法调用此方法:public static Foo doMyDatabaseStuff(ApplicationContext context, WebSite webSite) {&nbsp; &nbsp; return doMyDatabaseStuff(context, webSite, new TransactionCallback() {&nbsp; &nbsp; &nbsp; &nbsp; public Object doInTransaction(TransactionStatus ts) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //B. Do a bunch of use case specific DB stuff&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FooDao fooDao = (FooDao) context.getBean("FooDao");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Foo foo = fooDao.create(webSite);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foo.setName("Administratoren");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foo.setSite(webSite);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //call a bunch of other setters&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fooDao.update(foo);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return foo;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java