简体   繁体   English

像Obj-C这样的Java类别?

[英]Java Categories like Obj-C?

When programming using other people's classes, you often find that they didn't quite anticipate your needs and you have to use a sequence of several steps all over for what should be a one step operation. 当使用其他人的类进行编程时,您常常会发现他们并没有完全预期您的需求,因此您必须使用一系列的多个步骤来完成一个步骤的操作。

IE, you're using a class that indexes objects, and so has a getObjectAtIndex method and a getNameOfObjectAtIndex method, but you want to access objects by name instead of index, so you have to write a loop that uses the getNumberOfIndexes method and loops over each, checks the name against the name you want, returns the index, and then you use access the object at that index. IE,您正在使用为对象建立索引的类,因此具有getObjectAtIndex方法和getNameOfObjectAtIndex方法,但是您想通过名称而不是索引来访问对象,因此必须编写一个使用getNumberOfIndexes方法的循环并遍历每一个都根据所需名称检查名称,返回索引,然后使用该索引处的对象进行访问。

If you need to do this frequently, to keep your code tidy you should refactor. 如果需要经常执行此操作,请保持代码整洁,然后进行重构。

In Obj-C, the solution is to write a category. 在Obj-C中,解决方案是编写一个类别。 You can just add a new getObjectWithName method to that class (and all of its subclasses) without having to subclass or encapsulate it. 您可以只向该类(及其所有子类)添加一个新的getObjectWithName方法,而不必子类化或封装它。

In Java, I haven't yet found a particularly great solution. 在Java中,我还没有找到一个特别好的解决方案。 Instead I've been writing helper functions that aren't invoked on any object but require the object to be passed in. Basically I have a method with function notation, which can be difficult to read. 相反,我一直在编写未在任何对象上调用但需要传递该对象的辅助函数。基本上,我有一个带有函数符号的方法,该方法可能很难阅读。

To be clear, this isn't a class I wrote and I don't have the source code to it. 要明确的是,这不是我编写的类,并且没有源代码。 Some other code is creating and returning it, so making a subclass doesn't help as I can't change that code that returns it to return my subclass instead. 其他一些代码正在创建并返回它,因此创建子类无济于事,因为我无法更改返回它的代码以返回我的子类。 I could encapsulate it, but that seems even less elegant than what I'm already doing. 我可以封装它,但这似乎比我已经做的还要优雅。

Does anyone know of a way I can achieve functionality similar to Obj-C's Categories in Java? 有谁知道我可以实现类似于Java中Obj-C类别的功能的方法吗?

In general, Java developers most commonly use helper methods for this sort of thing. 通常,Java开发人员最常使用辅助方法进行此类操作。 It's flexible and relatively modular, and really not so bad as you think. 它是灵活的并且相对模块化,实际上还没有您想象的那么糟糕。

A related technique is to use a wrapper object ; 一种相关的技术是使用包装对象 ; a class that contains an instance of the class of interest, implements the same interfaces, and delegates most calls through to the contained instance, perhaps adding some methods along the way. 一个包含感兴趣的类的实例的类,实现相同的接口,并将大多数调用委派给所包含的实例,并可能在此过程中添加一些方法。 Some IDEs (IntelliJ IDEA, certainly) have built-in tools that make creating this sort of delegating wrapper painless. 某些IDE(当然是IntelliJ IDEA)具有内置工具,可以轻松创建这种委派包装器。 Then whenever you receive a reference to an instance of the class of interest, you construct an instance of the delegator around it before doing any further processing. 然后,每当收到对所关注类的实例的引用时,便在进行任何进一步处理之前,围绕它构造一个委托者的实例。

If you really want something more interesting, there is always Aspect-Oriented Programming (AOP) , which refers to tools and techniques for injecting new code into existing code based on various criteria at runtime. 如果您真的想要更有趣的东西,总会有面向方面的编程(AOP) ,它是指在运行时根据各种标准将新代码注入到现有代码中的工具和技术。 It is possible that you could use AOP to do something like this; 您可能可以使用AOP来执行类似的操作; for example, by adding an interface to a class, and a method that implements it. 例如,通过向类添加接口以及实现该接口的方法。 You'd have to cast the objects to the interface at compile time to use the method. 您必须在编译时将对象强制转换为接口才能使用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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