如何返回实现列表?

huangapple go评论89阅读模式
英文:

How to return list of implementations?

问题

我正在创建一个应用程序,该应用程序将用于在各个商店下订单。每个商店都有不同的下订单方式。目前看起来是这样的:

@Document("order")
public class Order extends AbstractAggregateRoot<Order> {
    private UUID id;
    private Shop shop;
    private Product product;
    // 其他字段和方法已省略
}
public abstract class Shop {
    private String baseUrl;
    abstract void placeOrder(...);
    // 其他字段和方法已省略
}
public class XYZShop extends Shop {
    @Override
    void placeOrder(...) {
        // 方法的实现
    }
    // 其他字段和方法已省略
}

当客户创建订单时,他应该能够选择在哪个商店下订单。

如何获取可用商店的列表?或者也许我应该以不同的方式实现这样的逻辑?

英文:

I'm creating an app which will be used to place orders in various stores. Every store have different way of placing an order. Currently it looks like this:

@Document(&quot;order&quot;)
public class Order extends AbstractAggregateRoot&lt;Order&gt; {
    private UUID id;
    private Shop shop;
    private Product product;
    // other fields and methods have been skipped
}
public abstract class Shop {
    private String baseUrl;
    abstract void placeOrder(...);
    // other fields and methods have been skipped
}

public class XYZShop extends Shop {
    @Override
    void placeOrder(...) {
        // implementation of method
    }
    // other fields and methods have been skipped
}

When client is creating an order he should be able to select in which store he want to place this order.

How to get list of available shops? Or maybe should I implement such logic in different way?

答案1

得分: 1

我非常不方便的方式是以下这样的

	public Test3()
	{
		Class<?> parentClass = this.getClass().getSuperclass();
		if (Extenders.extendingClasses.containsKey(parentClass.getCanonicalName()))
		{
			Extenders.extendingClasses.get(parentClass.getCanonicalName()).add(this.getClass());
		}
		else
		{
			List<Class<?>> extenders = new ArrayList<>();
			extenders.add(this.getClass());
			Extenders.extendingClasses.put(parentClass.getCanonicalName(), extenders);
		}

	}

	public static void main(String[] args)
	{

		Test3 tmp = new Test3();

		for (Entry<String, List<Class<?>>> entry : Extenders.extendingClasses.entrySet())
		{

			System.out.println("Class " + entry.getKey() + " is extended by: ");

			for (Class<?> clazz : entry.getValue())
			{
				System.out.print(clazz.getCanonicalName() + ", ");
			}

		}

	}


与超类

    public abstract class Test2 {
    
    }

和一个单例


    public class Extenders {
    	
    	
    	public static Map<String, List<Class<?>>> extendingClasses = new TreeMap<>();
    
    }

然而这种方法有一个问题您需要首先创建一个扩展类的实例因此它基本上不会列出所有的扩展类只会列出那些被使用的类

在这种情况下的输出将是

    Class Test2 被扩展 
    Test3, 

我可能会对此有误解但据我所知超类并不知道它被扩展了
英文:

I very inconvenient way to this would be the following:

public Test3()
{
Class&lt;?&gt; parentClass = this.getClass().getSuperclass();
if(Extenders.extendingClasses.containsKey(parentClass.getCanonicalName()))
{
Extenders.extendingClasses.get(parentClass.getCanonicalName()).add(this.getClass());
}
else
{
List&lt;Class&lt;?&gt;&gt; extenders = new ArrayList&lt;&gt;();
extenders.add(this.getClass());
Extenders.extendingClasses.put(parentClass.getCanonicalName(), extenders);
}
}
public static void main(String[] args)
{
Test3 tmp = new Test3();
for(Entry&lt;String,List&lt;Class&lt;?&gt;&gt;&gt; entry : Extenders.extendingClasses.entrySet())
{
System.out.println(&quot;Class &quot;+entry.getKey()+&quot; is extended by: &quot;);
for(Class&lt;?&gt; clazz : entry.getValue())
{
System.out.print(clazz.getCanonicalName()+&quot;, &quot;);
}
}
}

with the super class

public abstract class Test2 {
}

and a singleton

public class Extenders {
public static Map&lt;String,List&lt;Class&lt;?&gt;&gt;&gt; extendingClasses = new TreeMap&lt;&gt;();
}

This method however has the problem that you need to create an instance of the extending class first. So it's basically not listing all extenders, only those who get used.

The output in this case would be:

Class Test2 is extended by: 
Test3, 

I might be wrong on this: But as far as I know, the super class doesn't know about it being extended.

huangapple
  • 本文由 发表于 2020年5月4日 18:55:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61590573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定