2

I am wondering if there is a short cut for my current problem.

I have a List abcList.

It contains 3 type of objects/Entity A B C (they did not inherit a common interface or parent, with exception to Object>.

They are hibernate Entity.

I have 3 overloaded method.

process(A a)
process(B b)
process(B C)

I was hoping to loop the

List abcList and just calling process();

for(Object o: abcList) process(o);

is there an easy solution for my current problem? I am implementing a class that contain 3 different type of object List.


Automatically Create Wordpress Posts for Each Photo in my Media Folder

When I'm designing Wordpress sites for clients, many of the theme designs I use consist of several posts, each with one photo and a bit of text. As a result, I end up spending lots of time creating lots of posts with one picture each-- just so the layout works as expected. It seems like there should be a way to bulk add images to the Media folder (I use the Add from Server plugin) and then tell WP to create a bunch of separate posts containing one photo each.

Does anyone know how to do this? It seems like professional theme designers would get a lot of use out of a plugin that did this automatically.

Thanks! Chimera

4

3 回答 3

3

由于绑定是在编译时进行的,因此不可能知道。如果您可以为这些类添加接口,则可以使用访问者模式。

于 2012-08-10T02:48:25.380 回答
1

除了访问者模式之外,要考虑的另一件事是在添加到列表时放置一个间接层。与其直接放入对象,不如放入一个可以process同时引用该对象和外部上下文的对象。

于 2012-08-10T04:33:39.177 回答
0

这是使用访问者模式的好地方。在不诉诸反射的情况下,您需要为列表中的对象定义一个通用接口。让我们从那里开始:

interface Visitable {
    void accept(Visitor v);
}

那么,Visitor 是您为每种具体类型定义流程方法的地方:

interface Visitor {
    void process(A a);
    void process(B b);
    void process(C c);
}

现在,一个具体的 Visitable 能够调用适当的重载,process()因为它当然在编译时知道它自己的具体类型。例如:

class A implements Visitable {
    void accept(Visitor v) {
        v.process(this);
    }
}

BC将做同样的事情。所以现在你结束了你的处理循环:

List<Visitable> abcList = ...;
Visitor visitor = ...;

for (Visitable o : abcList) {
    o.accept(visitor);
}

同样,如果您不能为所有类定义一个通用接口,那么您仍然可以通过使用反射的访问者模式来实现这一点。

于 2012-08-10T03:26:30.583 回答