gpt4 book ai didi

java - 这是适配器模式的正确实现吗?

转载 作者:行者123 更新时间:2023-12-01 17:52:02 25 4
gpt4 key购买 nike

所以我有两个类:SoccerPlayer 和 IceHockeyPlayer
它们都有自己的接口(interface)和一些方法:ISoccerPlayer 和 IIceHockeyPlayer

足球运动员:

public class SoccerPlayer implements ISoccerPlayer {

public String[] teammembers;

@Override
public void kickFootball(int meters) {
// Kick the football
}

@Override
public void runForward(double speed) {
// Run forward
}

@Override
public void addTeammembers(String[] memberNames) {
// Add the members
}
}

冰球玩家:

public class IceHockeyPlayer implements IIceHockeyPlayer {

public ArrayList<String> teammembers;

@Override
public void hitPuck(int meters) {
// Hit the puck
}

@Override
public void skateForward(double speed) {
// Skate forward
}

@Override
public void addTeammembers(ArrayList<String> memberNames) {
// Add the members
}

}

接下来,我创建了一个类,其中包含实现两个接口(interface)的 SoccerPlayer 和 IceHockeyPlayer,这将是我的适配器。
此类中的方法仅调用 SoccerPlayer 或 IceHockeyPlayer 的正确方法:

public class Adapter implements ISoccerPlayer, IIceHockeyPlayer {

public SoccerPlayer soccerplayer;
public IceHockeyPlayer icehockeyplayer;

public Adapter(SoccerPlayer soccerplayer, IceHockeyPlayer icehockeyplayer) {
this.soccerplayer = soccerplayer;
this.icehockeyplayer = icehockeyplayer;
}

// SoccerPlayer
@Override
public void kickFootball(int meters) {
this.soccerplayer.kickFootball(meters);
}

@Override
public void runForward(double speed) {
this.soccerplayer.runForward(speed);
}

@Override
public void addTeammembers(String[] memberNames) {
this.soccerplayer.addTeammembers(memberNames);
}

// IceHockeyPlayer
@Override
public void hitPuck(int meters) {
this.icehockeyplayer.hitPuck(meters);
}

@Override
public void skateForward(double speed) {
this.icehockeyplayer.skateForward(speed);
}

@Override
public void addTeammembers(ArrayList<String> memberNames) {
this.icehockeyplayer.addTeammembers(memberNames);
}

}

这是适配器模式的正确实现吗?如果不是,我需要改变什么才能使其成为一个?

最佳答案

这更像是一个外观。

对于适配器,你会有类似的东西

interface SportsPlayer {
public void play(int meters);
public void move(double speed);
}

和适配器,例如

class IceHockeyPlayerAdapter implements SportsPlayer {
private IceHockeyPlayer player;
public IceHockeyPlayerAdapter(IceHockeyPlayer p) { player = p; }

public void play(int meters) {
player.playPuck(meters);
}
public void move(double speed) {
player.skateForward(speed);
}
}

这将“适应”曲棍球运动员“成为”SportsPlayer;所以它实际上是一个不同的方法集。

编辑:

这是真实 JDK 的适配器用法。

您知道可以使用 try-with-resources 来自动关闭资源,并且可以在提交任务后关闭 ExecutorService 吗?好吧,try-with-resources 需要一个 AutoCloseable,而 ExecutorService 并没有实现它。救援适配器:

public class AutocloseableExecutorService implements ExecutorService, AutoCloseable {
private ExecutorService delegate;
public AutocloseableExecutorService(ExecutorService d) {
delegate = d;
}
// delegate ExecutorService methods to implement the interface
public void execute(Runnable r) { delegate.execute(r);
// ...

// implement close() for AutoCloseable
public void close() {
delegate.shutdown();
}
}

现在你可以像这样使用它:

public void submitTasks(Runnable... rs) {
try (AutocloseableExecutorService executor = new AutocloseableExecutorService(
Executors.newSingleThreadExecutor())) {
for (Runnable r : rs) executor.submit();
}
}

服务将在方法结束时注册关闭。

关于java - 这是适配器模式的正确实现吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49029874/

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