gpt4 book ai didi

java - 如何在 Java 中包装抽象类层次结构

转载 作者:太空宇宙 更新时间:2023-11-04 09:45:48 28 4
gpt4 key购买 nike

我正在编写一个库(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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com