首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java设计模式测试

Java设计模式测试
EN

Stack Overflow用户
提问于 2016-08-11 06:05:18
回答 3查看 635关注 0票数 0

最近,我遇到了一个问题,要求我使用适当的设计模式进行设计。问题是:

代码语言:javascript
复制
Implement a remote control of TV. 
Any remote control we use, either at home/hotel or at a friend’s place, 
we just pick up the TV remote control and start pressing Up and Down 
or Forward and Back keys to change the channels.
Choose a suitable design pattern for implementation of this problem.

我想不出如何设计这个问题。这就是我想出来的:

场所是一个抽象的类。 家延地 酒店扩建场所 FriendPlace扩展空间 TVRemote是一个类 Place有一个TVRemote 键是一个接口。 键有一个方法press() UpKey,DownKey,ForwardKey,BackKey是实现密钥的类 TVRemote有钥匙 TVRemote中可能有更多的密钥

这就是我所能想到的,但不能在这里合并一个设计模式。有指引吗?

EN

回答 3

Stack Overflow用户

发布于 2016-08-11 06:15:27

一个简单的方法是创建一个接口。

代码语言:javascript
复制
interface RemoteControl 
{
public void up(); 
public vois down();
public void forward();
public void back();
} 

然后创建将为特定设备实现该接口的特定类。

例如:

代码语言:javascript
复制
public class HomeRemote implements RemoteControl {

public void up(){
..
} 

public vois down(){
..
}

public void forward(){
..
}

public void back(){
..
}

}

然而,

经过我们的讨论--在进一步搜索之后,我倾向于认为模式就是这里所要求的。看看这个- http://www.programcreek.com/2011/10/java-design-pattern-bridge/

远程控制的抽象类与基本实现(上、下、前、后)一起使用,然后每个特定的TVRemote扩展抽象类以添加更多/和设备特定的功能。

还请注意,TVs使用的是公共接口,其中描述了(goUp()、goDown()、goForward()、goBack(),可能还有on()、off())函数。

票数 0
EN

Stack Overflow用户

发布于 2016-08-11 10:43:35

一些意见:

  • 不同的遥控器可能有不同的按钮数。
  • 不同的按钮执行不同的操作。
  • 远程控制应该忽略执行操作的细节。
  • 通过给按钮分配不同的操作或支持不同的设备,我们应该能够重新编写远程控制程序。

在这种情况下使用的最简单的模式是Command。您可以创建特定的Command实现,然后将Command分配给按钮:

代码语言:javascript
复制
public interface Command {
    void Execute();
}
public class Button {
    private readonly Command command;
    public Button(Command command) {
        this.command = command;
    }
    public void Press() {
        this.command.Execute();
    }
 }
 public class Remote {
     public Button ButtonPlaceholder1 { get; set; }
     public Button ButtonPlaceholder2 { get; set; }
     public Button ButtonPlaceholder3 { get; set; }
     public Button ButtonPlaceholder4 { get; set; }
 }

那么,拥有Button类有什么好处呢?那么,假设您想要引入一个滑块按钮,它可以向上和向下移动。在本例中,您将使用两个Command配置它:

代码语言:javascript
复制
public class SliderButton {
    public SliderButton(Command up, Command down) {
        this.commandUp = up;
        this.commandDown = down;
    }
    public void Up() {
        this.commandUp.Execute();
    }
    public void Down() {
        this.commandDown.Execute();
    }
}

在这次采访中,有趣的后续问题是,“如何实现一个按钮,取消按下先前的按钮所做的操作?”(例如,我在观看ESPN频道,但比赛之间有一个中断,所以我切换到MTV,但我想偶尔检查一下是否中断了,如果没有,请返回MTV)。

票数 0
EN

Stack Overflow用户

发布于 2017-10-13 19:27:58

您应该在这里使用命令模式。通常有调用方、客户端、命令方和接收方。这里是您可能需要的类。

命令

代码语言:javascript
复制
public interface ICommand {
    void execute();
}

调用者

代码语言:javascript
复制
public class RemoteControl {
    Map<Key, ICommand> commandsByKey;

    public RemoteControl() {
        commandsByKey = new HashMap<>();
    }

    public void setCommand(Key key, ICommand command) {
        commandsByKey.put(key, command);
    }

    public void press(Key key) throws Exception {
        ICommand command = commandsByKey.get(key);
        if(command == null)
            throw new Exception("Invalid Key");
        command.execute();
    }
}

接收机

代码语言:javascript
复制
public class TV {
    private String brand;

    public TV(String brand) {
        this.brand = brand;
    }

    @Override
    public String toString() {
        return brand + " TV";
    }
}

客户端

代码语言:javascript
复制
public abstract class Place {
    private TV tv;
    private RemoteControl remoteControl;

    public Place(TV tv) {
        this.tv = tv;
        this.remoteControl = new RemoteControl();

        remoteControl.setCommand(Key.UP, new UpCommand(this.tv));
        remoteControl.setCommand(Key.FORWARD, new ForwardCommand(this.tv));
        remoteControl.setCommand(Key.DOWN, new DownCommand(this.tv));
        remoteControl.setCommand(Key.BACK, new BackCommand(this.tv));
    }

    public TV getTv() {
        return tv;
    }

    public RemoteControl getRemoteControl() {
        return remoteControl;
    }
}

public class Home extends Place {
    public Home() {
        super(new TV("Sony"));
    }
}

public class Hotel extends Place {

    public Hotel() {
        super(new TV("LG"));
    }
}

具体命令

代码语言:javascript
复制
public class UpCommand implements ICommand {
    private TV tv;

    public UpCommand(TV tv) {
        this.tv = tv;
    }

    @Override
    public void execute() {
        System.out.println("Up Command  - " + tv);
    }

}


public class DownCommand implements ICommand {
    private TV tv;

    public DownCommand(TV tv) {
        this.tv = tv;
    }

    @Override
    public void execute() {
        System.out.println("Down Command  - " + tv);
    }
}


public class ForwardCommand implements ICommand {
    private TV tv;

    public ForwardCommand(TV tv) {
        this.tv = tv;
    }
    @Override
    public void execute() {
        System.out.println("Forward Command  - " + tv);

    }

}


public class BackCommand implements ICommand {
    private TV tv;

    public BackCommand(TV tv) {
        this.tv = tv;
    }

    @Override
    public void execute() {
        System.out.println("Back Command  - " + tv);
    }

}

Keys

代码语言:javascript
复制
public enum Key {
    UP, DOWN, FORWARD, BACK
}

测试

代码语言:javascript
复制
public class RemoteTest {
    public static void main(String[] args) throws Exception {
        Place home = new Home();
        home.getRemoteControl().press(Key.UP);
        home.getRemoteControl().press(Key.DOWN);
        home.getRemoteControl().press(Key.BACK);

        Hotel hotel = new Hotel();
        hotel.getRemoteControl().press(Key.UP);
    }
}

如果向遥控器添加任何附加键,则不需要触摸任何现有命令或调用程序。你只需要把它添加到客户端。这符合开闭原理。如果每个位置有不同的远程,那么将其作为构造函数参数,因此不需要更改任何其他类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38888411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档