p****o 发帖数: 46 | 1 【 以下文字转载自 JobHunting 讨论区 】
发信人: pepero (pepero), 信区: JobHunting
标 题: 一道interview design pattern题
发信站: BBS 未名空间站 (Fri Feb 12 16:16:46 2010, 美东)
今天有人问我这道题,是关于设计的(他说是用java)。我把愿题帖一下,
Design an electrical controller that can start or turn off any number of
devices (A device could be a Computer, or a TV). Other people have completed
programming those devices and you cannot modify their source code, e.g.,to
ask them all to make a particular interface. Fortunately, each such device
already has two meth | p****o 发帖数: 46 | 2 水木上有人说是adapter + command pattern.
不知道这command pattern指的是什么,怎么加上去呢
我先把我写的adapter pattern代码写上。
请大家帮忙看看
class 3rdPartyComputer{
public void start(){m_status=true;}
public void stop(){m_status=false;}
public bool Computerstatus(){return m_status;}
private bool m_status;
};
class 3rdPartyTV{
public void turnOn(){m_status=true;}
public void turnOff(){m_status=false;}
public bool TVstatus(){return m_status;}
private bool m_status;
};
interface device{
void Up();
void Down();
}
class Computer implements
【在 p****o 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: pepero (pepero), 信区: JobHunting : 标 题: 一道interview design pattern题 : 发信站: BBS 未名空间站 (Fri Feb 12 16:16:46 2010, 美东) : 今天有人问我这道题,是关于设计的(他说是用java)。我把愿题帖一下, : Design an electrical controller that can start or turn off any number of : devices (A device could be a Computer, or a TV). Other people have completed : programming those devices and you cannot modify their source code, e.g.,to : ask them all to make a particular interface. Fortunately, each such device : already has two meth
| g*****g 发帖数: 34805 | 3 This is not how you want to implement it.
class Computer implements device
{
private 3rdPartyComputer adaptee = new 3rdPartyComputer ();
public void Up()
When you have a new Computer device, you end up changing Computer class,
that's a bad design.
Just do
class Device1Adapter implements Device {
public Device1Adapter(Device1 d) {
this.d = d;
}
public void up() {
d.turnOn();
}
}
then you can have a controller class like
class Controller {
List<
【在 p****o 的大作中提到】 : 水木上有人说是adapter + command pattern. : 不知道这command pattern指的是什么,怎么加上去呢 : 我先把我写的adapter pattern代码写上。 : 请大家帮忙看看 : class 3rdPartyComputer{ : public void start(){m_status=true;} : public void stop(){m_status=false;} : public bool Computerstatus(){return m_status;} : private bool m_status; : };
| a****l 发帖数: 8211 | 4 first impression, this is NOT for any "electrical controller", since for any
real electrical controller such design requirement is basically nonsense.
completed
to
【在 p****o 的大作中提到】 : 水木上有人说是adapter + command pattern. : 不知道这command pattern指的是什么,怎么加上去呢 : 我先把我写的adapter pattern代码写上。 : 请大家帮忙看看 : class 3rdPartyComputer{ : public void start(){m_status=true;} : public void stop(){m_status=false;} : public bool Computerstatus(){return m_status;} : private bool m_status; : };
| p****o 发帖数: 46 | 5 我怎么觉得,你要做得和我做的是一样的阿.
你这个Device1Adapter和我这个Computer有什么区别?
我这个computer也是个adapter阿。
是不是我哪块理解错了, 请指教。
【在 g*****g 的大作中提到】 : This is not how you want to implement it. : class Computer implements device : { : private 3rdPartyComputer adaptee = new 3rdPartyComputer (); : public void Up() : When you have a new Computer device, you end up changing Computer class, : that's a bad design. : Just do : class Device1Adapter implements Device { : public Device1Adapter(Device1 d) {
|
|