由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 讨论:关于一个design 问题
相关主题
设计题目的本质问题A家电面
Bloomberg offer全过程这道设计面试题这样解对吗
1st Amazon phone interview (1hr)材料phd,求内推
问一个interview时候design的general问题java 接口中的方法 疑惑
[合集] 问几道amazon面试题design类题目大家是怎么准备的啊?
我也来写个面经吧帮人po一个programmer职位吧。
请问设计一个虚拟的动物园,用什么设计模式好啊?讨论一道题:deep iterator
OO design 题一般思考的方向?请教operator const char*() 的问题
相关话题的讨论汇总
话题: material话题: class话题: public话题: steel话题: wood
进入JobHunting版参与讨论
1 (共1页)
H*M
发帖数: 1268
1
刚开始看design pattern不久,看到本版的一个设计题目,拿出来讨论一下.不怕版砖,请
尽量批评.
原题如下:
Do you approve the following design?
Class Furniture{
Some functions related to the property of furnitures;
};
4 classes derived from Furniture
Class wood_chair
Class steel_chair
Class wood_table
Class steel_table
What if you need to design a lot of other furnitures like desks.... with oth
er materials like plastics.
版上有牛牛们讲用decorator,我觉得也是.
一种材料一种class我觉得不妥.可以abstract成材料这个概念.
以下是写的class,请说说看有什么不妥的地方.
class Furniture{
virtual v
g***i
发帖数: 408
2
这是一个典型的桥模式,
http://www.netobjectives.com/files/design-patterns-explained-ch10.pdf
The original design has the problems that if you want to add a Desk, then
you have to add too class: WoodDesk and SteelDesk.
If you can use bridge pattern
把 material 和 style作为家具的property,在创建家具的时候这样就可以自由的组合
不同的MATERIAL和STYLE. 这也是对自然世界最确切的建模。
装饰模式用在这里有点霸王硬上攻了。
g*******y
发帖数: 1930
3
bridge不是说把abstration跟imp分离吗?
一层一层的加property,是decorator的特点和作用啊?
我学的不好,可能是我没理解到这两个pattern的精髓,望讲解

【在 g***i 的大作中提到】
: 这是一个典型的桥模式,
: http://www.netobjectives.com/files/design-patterns-explained-ch10.pdf
: The original design has the problems that if you want to add a Desk, then
: you have to add too class: WoodDesk and SteelDesk.
: If you can use bridge pattern
: 把 material 和 style作为家具的property,在创建家具的时候这样就可以自由的组合
: 不同的MATERIAL和STYLE. 这也是对自然世界最确切的建模。
: 装饰模式用在这里有点霸王硬上攻了。

a****n
发帖数: 1887
4
bridge 本质是处理,如果一个对象有正交的行为, 他们会独立演化的情况
对于这个题,我觉得bridge 是没错了, 不过也可以不用bridge, 直接用多接口
(也可以用C++ Template的traits 和 policy 方式)
interfaces:
material
/\
/ \
wood steel ......
furniture
/\
/ \
table chair ......
concrete furniture
wood chair: public wood, public chair...
steel chair: public steel, public chair...
我觉得多接口和bridge 最大的区别在于,bridge 可以在runtime 更换imp
另外使用组合耦合低一些
decor 是为class添加responsibility
g***i
发帖数: 408
5
多接口是design的大忌,这一点GoF讲的很清楚,现实生活中这么玩可能是高手,面试
的时候可千万别
这么回答。
而且多接口照样解决不了加一个STYLE (Desk),要创建很多个子类(WoodDesk,
SteelDesk)的
问题。

【在 a****n 的大作中提到】
: bridge 本质是处理,如果一个对象有正交的行为, 他们会独立演化的情况
: 对于这个题,我觉得bridge 是没错了, 不过也可以不用bridge, 直接用多接口
: (也可以用C++ Template的traits 和 policy 方式)
: interfaces:
: material
: /\
: / \
: wood steel ......
: furniture
: /\

H*M
发帖数: 1268
6
不太懂啊 能不能把用bridge整个设计简单的写一下,就是class和重要func就可以了
另外,假如material和style作为家具的property,在创建家具的时候组合那么如果后来要
加另外一种属性,比如面向的市场客户群,那么这个家具的base class就要修改了啊

【在 g***i 的大作中提到】
: 这是一个典型的桥模式,
: http://www.netobjectives.com/files/design-patterns-explained-ch10.pdf
: The original design has the problems that if you want to add a Desk, then
: you have to add too class: WoodDesk and SteelDesk.
: If you can use bridge pattern
: 把 material 和 style作为家具的property,在创建家具的时候这样就可以自由的组合
: 不同的MATERIAL和STYLE. 这也是对自然世界最确切的建模。
: 装饰模式用在这里有点霸王硬上攻了。

g***i
发帖数: 408
7
其实任何DESIGN PATTERN 都回归于GOF说的3句话
PROGRAMMING TO INTERFACE RATHER THAN CLASS
FAVOR OBJECT COMPOSITION OVER class INHERITANCE
ENCAPSULATE WHAT VARIES
对于有很多年设计经验的人来说,PATTERN不PATTERN都不是那么重要了,
这里变化的就是 STYLE和MATERIAL, 所以必然是要封装的,再加上(PROGRAMMING TO
INTERFACE RATHER THAN CLASS) 于是我们有了这两个interface。
再根据“FAVOR OBJECT COMPOSITION OVER INTERFERENCE” 排除做MULTIPLE
INHERITANCE, 于是我们就做一下composition,把STYLE和material作为家具的属性。
严格的来讲,这里的桥不是那么明显:)
为了让这样桥更明显就是单单把MATERIAL从家具中分离出来
家具有一个property MATERIAL (例如wood,steel,...), 和

【在 g*******y 的大作中提到】
: bridge不是说把abstration跟imp分离吗?
: 一层一层的加property,是decorator的特点和作用啊?
: 我学的不好,可能是我没理解到这两个pattern的精髓,望讲解

g***i
发帖数: 408
8
我现在觉得当时的那个考题,主要是为了让你把MATERIAL分出来就行了,STYLES实际上
就是家具,这
样的桥,更明显:)

来要

【在 H*M 的大作中提到】
: 不太懂啊 能不能把用bridge整个设计简单的写一下,就是class和重要func就可以了
: 另外,假如material和style作为家具的property,在创建家具的时候组合那么如果后来要
: 加另外一种属性,比如面向的市场客户群,那么这个家具的base class就要修改了啊

a****n
发帖数: 1887
9
恩改一下,bridge的实现
material
/\
/ \
wood steel ......
myfurniture
/\
/ \
table chair ......
interface furniture{
void furniture_method();
string tostring();
}
interface material{
void material_method();
string tostring();
}
class steel : material()
{
public virtual void material_method(){...};
public virtual string tostring(){return "steel";}
}
abstract class myfurniture: material,

【在 g***i 的大作中提到】
: 多接口是design的大忌,这一点GoF讲的很清楚,现实生活中这么玩可能是高手,面试
: 的时候可千万别
: 这么回答。
: 而且多接口照样解决不了加一个STYLE (Desk),要创建很多个子类(WoodDesk,
: SteelDesk)的
: 问题。

g***i
发帖数: 408
10
好桥,
虽然俺不认你写的代码:) (btw: 你这是C++吗?)

【在 a****n 的大作中提到】
: 恩改一下,bridge的实现
: material
: /\
: / \
: wood steel ......
: myfurniture
: /\
: / \
: table chair ......
: interface furniture{

相关主题
我也来写个面经吧A家电面
请问设计一个虚拟的动物园,用什么设计模式好啊?这道设计面试题这样解对吗
OO design 题一般思考的方向?材料phd,求内推
进入JobHunting版参与讨论
a****n
发帖数: 1887
11
C#直接写的, 可能放到IDE里面一堆错误。。。
C++不直接支持interface,所以design的题我觉得用C#,JAVA比较好
仔细想了一下,多接口的class膨胀问题确实是硬伤,这个还是得用Bridge
不过用C++ template的traits 和 policy 也可以实现, 但是面试应该不会问这个

【在 g***i 的大作中提到】
: 好桥,
: 虽然俺不认你写的代码:) (btw: 你这是C++吗?)

H*M
发帖数: 1268
12
在本版设计牛人们的指教下,算是领会点bridge了
我把asuran的版本改成c++版的,也加深下印象。
ps, 发现design pattern真难啊,有时候是i understand every word, but i dont un
derstand when they are combined 成一句话。呵呵
改了。编译过的。
#include
#include
using namespace std;
class material;
class furniture
{
public:
furniture(material* mat):m(mat){}
virtual void tostring() = 0;
void setMaterial(material* m){this->m = m;}
material* getMaterial(){return m;}
private:
material* m;
};
class material{
public:
v*****t
发帖数: 127
13
基于你的改了一些错误,这里是编译通过的:
class material;
class furniture{
public:
virtual void tostring() = 0;
void setMaterial(material* m){this->m = m;}
protected:
material* m;
};
class material{
protected:
string materialName;
public:
virtual string what() = 0;
};
class steel: public material
{
public:
steel(){materialName = "steel";}
virtual string what(){return "steel";}
};
class wood:public material
{
public:
wood(){this->materialName = "wood";}
virtual string what(
v*****t
发帖数: 127
14
然后在贴一个decorator的做一个比较:
class Fur{
public:
virtual void describeself()=0;
};
class Table:public Fur{
public:
void describeself(){cout<<"I'm a table";}
};
class Material:public Fur{
public:
virtual void describeself()=0;
protected:
Fur *fur;
string material_name;
};
class Wood: public Material{
public:
Wood(Fur *f){fur = f; material_name = "wood";}
void describeself(){
fur->describeself();
cout<<" with "< }
};
class Steel: publi
v*****t
发帖数: 127
15
我觉得decorator的自由度(flexibility)比bridge还更大一些,
用那个pattern我觉得在于你怎么去看这个问题:
decorator:material的性质可以是几种材料的任意组合叠加(appendable property)
bridge:material这个属性具有正交性,不能任意组合叠加(exclusive property)

【在 v*****t 的大作中提到】
: 然后在贴一个decorator的做一个比较:
: class Fur{
: public:
: virtual void describeself()=0;
: };
: class Table:public Fur{
: public:
: void describeself(){cout<<"I'm a table";}
: };
: class Material:public Fur{

1 (共1页)
进入JobHunting版参与讨论
相关主题
请教operator const char*() 的问题[合集] 问几道amazon面试题
面试用c++还是java?我也来写个面经吧
一道OO设计题,有经验的请给些思路,多谢请问设计一个虚拟的动物园,用什么设计模式好啊?
烦死了OO design 题一般思考的方向?
设计题目的本质问题A家电面
Bloomberg offer全过程这道设计面试题这样解对吗
1st Amazon phone interview (1hr)材料phd,求内推
问一个interview时候design的general问题java 接口中的方法 疑惑
相关话题的讨论汇总
话题: material话题: class话题: public话题: steel话题: wood