最近,我遇到了一个问题,要求我使用适当的设计模式进行设计。问题是:
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中可能有更多的密钥
这就是我所能想到的,但不能在这里合并一个设计模式。有指引吗?
发布于 2016-08-11 06:15:27
一个简单的方法是创建一个接口。
interface RemoteControl
{
public void up();
public vois down();
public void forward();
public void back();
} 然后创建将为特定设备实现该接口的特定类。
例如:
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())函数。
发布于 2016-08-11 10:43:35
一些意见:
在这种情况下使用的最简单的模式是Command。您可以创建特定的Command实现,然后将Command分配给按钮:
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配置它:
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)。
发布于 2017-10-13 19:27:58
您应该在这里使用命令模式。通常有调用方、客户端、命令方和接收方。这里是您可能需要的类。
命令
public interface ICommand {
void execute();
}调用者
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();
}
}接收机
public class TV {
private String brand;
public TV(String brand) {
this.brand = brand;
}
@Override
public String toString() {
return brand + " TV";
}
}客户端
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"));
}
}具体命令
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
public enum Key {
UP, DOWN, FORWARD, BACK
}测试
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);
}
}如果向遥控器添加任何附加键,则不需要触摸任何现有命令或调用程序。你只需要把它添加到客户端。这符合开闭原理。如果每个位置有不同的远程,那么将其作为构造函数参数,因此不需要更改任何其他类。
https://stackoverflow.com/questions/38888411
复制相似问题