我正在编写一个包装了另一个库(dependentLibrary)的所有类的库(wrapperLibrary),并且我想强制集成者仅使用wrapperLibrary中的类和dependentLibrary中的 NOT -将其完全包装。
我正在尝试寻找一种方法来包装下面的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。
dependencyLibrary中的其他类还具有AOT / AMOT / COTM作为方法参数或返回类型。因此,我们应该能够在需要时将AOTWrapper隐藏到AOT中。
我们如何实现这一目标?
答案 0 :(得分:0)
这实际上不是通常被称为特定设计模式的包装器。它要求包装器和包装器类实现相同的接口,但是实际上您可以通过所有包装器的抽象父类扩展lib的最后一个子具体类,并覆盖所有相关方法,您意识到在这种情况下所有包装器都将继承所有包装器。 libs方法,但是您可以使用异常抛出来覆盖不必要的方法(例如,可以在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();
}
祝你好运!