- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写一个库(wrapperLibrary),它包装另一个库(dependentLibrary)的所有类,并且我想强制集成器仅使用wrapperLibrary中的类,而不来自dependentLibrary - 彻底包装它。
我正在尝试找到一种方法来包装 dependentLibrary 的以下抽象层次结构:
public abstract class AOT{
// Has few abstract methods and few default method implementation
}
public abstract class AMOT extends AOT {
// Has a new abstract method and few overridden methods
}
public class COTM extends AMOT{
// Implementation class of the abstract hierarchy
}
包装器应如下所示:
public abstract class AOTWrapper{
// Has the default method implementation of AOT
}
public abstract class AMOTWrapper extends AOTWrapper {
// Has the default method implementation of AMOT
}
public class COTMWrapper extends AMOTWrapper{
// Implementation class of the abstract hierarchy
}
wrapperLibrary 的集成商或消费者不应有权访问 AOT/AMOT/COTM。
dependentLibrary 中的其他类也将 AOT/AMOT/COTM 作为方法参数或返回类型。因此,我们应该能够在需要时将 AOTWrapper 转换为 AOT。
我们如何才能实现这一目标?
最佳答案
这实际上并不是通常称为特定设计模式的包装器。它要求包装器和包装类实现相同的接口(interface),但实际上您可以通过所有包装器的抽象父级来扩展库的最后一个子具体类并覆盖所有相关方法,您意识到在这种情况下所有包装器将继承所有库方法,但您可以通过抛出异常来覆盖不必要的方法(例如,您可以在 java.util.AbstractList 的某些方法中找到类似的技巧)。然后,可能好的解决方案是使用一个工厂,它将正确创建您的 lib 实例,放入包装器并在客户喜欢的任何类容器中提供给客户。会是这样的:
public class Main {
public static void main(String[] args) {
WrapperFactory wrapperFactory = new WrapperFactory();
COTMWrapper cotmWrapper = wrapperFactory.getWrapper(COTMWrapper.class);
cotmWrapper.doCOTMStuff();
cotmWrapper.doSomethingElse();
AMOTWrapper amotWrapper = cotmWrapper;
amotWrapper.doAOTStuff();
AOTWrapper aotWrapper = amotWrapper;
aotWrapper.doAOTStuff();
AOT likeNotWrapped = cotmWrapper;
likeNotWrapped.doSomething();
}
}
class WrapperFactory {
public <T extends AOTWrapper> T getWrapper(Class<T> type){
COTM cotm = new COTM();
return type.cast(new COTMWrapper(cotm));
}
}
abstract class AOT {
// Has few abstract methods and few default method implementation
public abstract void doSomething();
public void doAOTStuff() {
// AOT stuff is doing
}
}
abstract class AMOT extends AOT {
// Has a new abstract method and few overridden methods
public abstract void doSomethingElse();
public void doSomething() {
// AMOT stuff is doing
}
}
class COTM extends AMOT {
// Implementation class of the abstract hierarchy
public void doSomethingElse() {
doCOTMStuff();
}
public void doCOTMStuff() {
// COTM stuff is doing
}
}
abstract class AOTWrapper extends COTM {
private AOT container;
public AOTWrapper(AOT container) {
this.container = container;
}
@Override
public void doAOTStuff() {
container.doAOTStuff();
}
@Override
public void doCOTMStuff() {
throw new UnsupportedOperationException();
}
@Override
public void doSomething() {
throw new UnsupportedOperationException();
}
@Override
public void doSomethingElse() {
throw new UnsupportedOperationException();
}
// Has the default method implementation of AOT
}
abstract class AMOTWrapper extends AOTWrapper {
private AMOT container;
public AMOTWrapper(AMOT container) {
super(container);
this.container = container;
}
@Override
public void doSomething() {
container.doSomething();
}
// Has the default method implementation of AMOT
}
class COTMWrapper extends AMOTWrapper {
private COTM container;
public COTMWrapper(COTM container) {
super(container);
this.container = container;
}
@Override
public void doSomethingElse() {
container.doSomethingElse();
}
@Override
public void doCOTMStuff() {
container.doCOTMStuff();
// COTM stuff is doing
}
// Implementation class of the abstract hierarchy
}
另一种方法:
public class Main {
public static void main(String[] args) {
COTMWrapper cotmWrapper = new COTMWrapper();
AMOTInter amotWrapper = cotmWrapper;
amotWrapper.doSomethingElse();
AOT likeNotWrapped = cotmWrapper;
likeNotWrapped.doSomething();
}
}
abstract class AOT {
// Has few abstract methods and few default method implementation
public abstract void doSomething();
public void doAOTStuff() {
// AOT stuff is doing
}
}
abstract class AMOT extends AOT {
// Has a new abstract method and few overridden methods
public abstract void doSomethingElse();
public void doSomething() {
// AMOT stuff is doing
}
}
class COTM extends AMOT {
// Implementation class of the abstract hierarchy
public void doSomethingElse() {
doCOTMStuff();
}
public void doCOTMStuff() {
// COTM stuff is doing
}
}
abstract class AOTWrapper extends AOT implements AOTInterface {
@Override
public void doAOTStuff() {
super.doAOTStuff();
}
// Has the default method implementation of AOT
}
abstract class AMOTWrapper extends AMOT implements AMOTInter {
@Override
public void doSomething() {
super.doSomething();
}
// Has the default method implementation of AMOT
}
class COTMWrapper extends COTM implements COTMInter {
@Override
public void doSomethingElse() {
super.doSomethingElse();
}
@Override
public void doCOTMStuff() {
super.doCOTMStuff();
}
// Implementation class of the abstract hierarchy
}
interface AOTInterface {
void doSomething();
void doAOTStuff();
}
interface AMOTInter extends AOTInterface {
void doSomethingElse();
void doSomething();
}
interface COTMInter extends AMOTInter {
void doSomethingElse();
void doCOTMStuff();
}
祝你好运!
关于java - 如何在 Java 中包装抽象类层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55443716/
我正在尝试将多个水平链接的 Button 和 TextView 垂直链接为 View 集,但仍保持平面 View 层次结构。这是我的初始布局和代码:
到目前为止,我已经在Google BigQuery上训练了几种模型,目前我需要查看模型的外观(即架构,损失函数等)。 有没有办法获取这些信息? 最佳答案 仔细阅读文档后,我可以说该功能尚不存在。我什至
本文实例讲述了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)。分享给大家供大家参考,具体如下: 前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个
我是一名优秀的程序员,十分优秀!