由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一道interview design pattern题 (转载)
相关主题
revision control经常看到一些名词:invariant, covariant, immutable
How does YAHOO calculate RSI? (转载)how to create interface "operator=="
一道算法题,到现在也没弄明白,谁能帮忙看看。non-aggregate type问题
请教template和factory有啥区别?how to reverse a HUGE list?
map是用什么data structure来implement的?swap every second node?
再来讨论一直算法课的作业吧弱问一个
Excel Control得问题c++ private 问题
The end of dynamic languagesVC里怎样主动关闭一个弹出的菜单?
相关话题的讨论汇总
话题: computer话题: design话题: device话题: controller话题: public
进入Programming版参与讨论
1 (共1页)
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) {

1 (共1页)
进入Programming版参与讨论
相关主题
VC里怎样主动关闭一个弹出的菜单?map是用什么data structure来implement的?
用STL map的时候怎么自己定义大小比较的关系再来讨论一直算法课的作业吧
C++ template questionExcel Control得问题
how to write some C/C++ program to enable dual monitor?The end of dynamic languages
revision control经常看到一些名词:invariant, covariant, immutable
How does YAHOO calculate RSI? (转载)how to create interface "operator=="
一道算法题,到现在也没弄明白,谁能帮忙看看。non-aggregate type问题
请教template和factory有啥区别?how to reverse a HUGE list?
相关话题的讨论汇总
话题: computer话题: design话题: device话题: controller话题: public