由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一道OO设计题
相关主题
问几个在工作公司买杂七杂八保险的问题, 请指点 (转载)好记(但不是最优)的combination算法
请问单耳聋算disability么?one C++ question
悲催地当了G家电面小白鼠C++ object size一问
amazon的那道题目One C++ question
Interview questions, Bloombergone C++ question
C: what is the output?发个题目给大家复习一下marco
一道面试的选择题Why I can't compile this function successfully
一个容易记忆的permutation算法C++: what is the output? How to interpret it?
相关话题的讨论汇总
话题: human话题: disability话题: deaf话题: public话题: blind
进入JobHunting版参与讨论
1 (共1页)
q****x
发帖数: 7404
1
某个虚拟世界里,只有四种:正常人,盲人,聋哑人,还有既盲又聋哑人。85%普通人
,5%纯盲人,5%纯聋哑,5%盲加聋哑。由于存在患病/治愈的可能,残疾人和正常人可
以互相转换。虚拟世界里可能会出现新的疾病,如瘸子。
如何设计OO系统?
最简单想法,一个Person,内含两个指针,分别指向盲和聋哑两个属性对象。但这个方
法显然太粗糙。感觉应该用到设计模式。
大家讨论一下吧。
d********t
发帖数: 9628
2
你越玩越BT啊!

【在 q****x 的大作中提到】
: 某个虚拟世界里,只有四种:正常人,盲人,聋哑人,还有既盲又聋哑人。85%普通人
: ,5%纯盲人,5%纯聋哑,5%盲加聋哑。由于存在患病/治愈的可能,残疾人和正常人可
: 以互相转换。虚拟世界里可能会出现新的疾病,如瘸子。
: 如何设计OO系统?
: 最简单想法,一个Person,内含两个指针,分别指向盲和聋哑两个属性对象。但这个方
: 法显然太粗糙。感觉应该用到设计模式。
: 大家讨论一下吧。

d****o
发帖数: 1055
3
一个人的interface,另外几个类继承
class PersonInterface{
private:
Eye eye;
Ear ear;
Mouth mouth;
Leg leg;
public:
void useEye(Light light);
void useEar(Sound sound);
Sound useMouth()=0;
void disableEye(){
eye.setFunction(false);
}
void ableEye(){
eye.setFunction(true);
}
//其余两个ear和mouth一样的
}
class ordinaryPerson: public PersonInterface{
}
class blindPerson: public PersonInterface{
public:
blindPerson(){
eye.disableEye();
}
}
//比如要把一个blindPerson转化为正常人,可以定义一个医生,或者就让他自己//好
Person* jack = new blindPerson();
jack->ableEye();
//瘸子就看你想怎么实现了

【在 q****x 的大作中提到】
: 某个虚拟世界里,只有四种:正常人,盲人,聋哑人,还有既盲又聋哑人。85%普通人
: ,5%纯盲人,5%纯聋哑,5%盲加聋哑。由于存在患病/治愈的可能,残疾人和正常人可
: 以互相转换。虚拟世界里可能会出现新的疾病,如瘸子。
: 如何设计OO系统?
: 最简单想法,一个Person,内含两个指针,分别指向盲和聋哑两个属性对象。但这个方
: 法显然太粗糙。感觉应该用到设计模式。
: 大家讨论一下吧。

q**q
发帖数: 51
4
Person
{
Eye,
Ear
}

【在 q****x 的大作中提到】
: 某个虚拟世界里,只有四种:正常人,盲人,聋哑人,还有既盲又聋哑人。85%普通人
: ,5%纯盲人,5%纯聋哑,5%盲加聋哑。由于存在患病/治愈的可能,残疾人和正常人可
: 以互相转换。虚拟世界里可能会出现新的疾病,如瘸子。
: 如何设计OO系统?
: 最简单想法,一个Person,内含两个指针,分别指向盲和聋哑两个属性对象。但这个方
: 法显然太粗糙。感觉应该用到设计模式。
: 大家讨论一下吧。

A**u
发帖数: 2458
5
恩 用state吧 和 decorator
先设计 虚拟世界,
里面有 4个指针, 指向 正常人,盲人,聋哑人, 盲人+聋哑人 (此对象可以记数)

这个类可以实现 治愈和患病的转换(通过增减 4个指针所指对象的记数)。
实现 增加新的疾病,可以用decorator 实现
所以 虚拟世界的接口是
class World{
public:
cure( string);
get_sick(string);
}
class person{
public:
name();
increase();
decrease();
private:
string disease_name;
int count;
}
四种人+新病的人 继承于 person 类

【在 q****x 的大作中提到】
: 某个虚拟世界里,只有四种:正常人,盲人,聋哑人,还有既盲又聋哑人。85%普通人
: ,5%纯盲人,5%纯聋哑,5%盲加聋哑。由于存在患病/治愈的可能,残疾人和正常人可
: 以互相转换。虚拟世界里可能会出现新的疾病,如瘸子。
: 如何设计OO系统?
: 最简单想法,一个Person,内含两个指针,分别指向盲和聋哑两个属性对象。但这个方
: 法显然太粗糙。感觉应该用到设计模式。
: 大家讨论一下吧。

H*****g
发帖数: 638
6
这种纯OO设计其实没有实际用处,就是来骗骗学生。
真正工业界的设计应该是:
class Person
{
bool IsBlind();
bool IsDeaf();
...
};
又好用又容易维护。
纯OO设计已经过时了,特别维护是大问题。
没有一个纯OO设计的工业界的系统能活过一年,一年以后一抓肯定能抓出一大堆不符合
OO设计的漏洞。

【在 q****x 的大作中提到】
: 某个虚拟世界里,只有四种:正常人,盲人,聋哑人,还有既盲又聋哑人。85%普通人
: ,5%纯盲人,5%纯聋哑,5%盲加聋哑。由于存在患病/治愈的可能,残疾人和正常人可
: 以互相转换。虚拟世界里可能会出现新的疾病,如瘸子。
: 如何设计OO系统?
: 最简单想法,一个Person,内含两个指针,分别指向盲和聋哑两个属性对象。但这个方
: 法显然太粗糙。感觉应该用到设计模式。
: 大家讨论一下吧。

f****n
发帖数: 208
7
Person can be defined as a abstract class. Normal person is the concrete
entity, so it should be a class. Deaf or blind are the behaviors of the
entity, so they should be interfaces. The benefit of this design is:
1) It support the concept of both deaf and blind, or other future human
disease, what are all interfaces. You can create new concrete classes of
DeafPerson, BlindPerson, DeafBlindPerson...
2) Using interface can abstract the special method/variable for each type of
human disease...
abstract class Person {
}
class NormalPerson extends Person {
}
interface isDeaf {
}
class DeafPerson extends Person implements isDeaf{
}
....
d******e
发帖数: 2265
8
很不幸,你这个真正的工业界的设计就是OO教科书的经典。
新手很容易泛用继承。会导致子类的指数爆炸。
所以搞Oo的人想出很多模式来解决这个问题。
这道题里面的原则可以看 head first pattern?书名记不清了第一张。原则大概是能用
composition就不要用继承。有详细讨论和扩展讨论。

【在 H*****g 的大作中提到】
: 这种纯OO设计其实没有实际用处,就是来骗骗学生。
: 真正工业界的设计应该是:
: class Person
: {
: bool IsBlind();
: bool IsDeaf();
: ...
: };
: 又好用又容易维护。
: 纯OO设计已经过时了,特别维护是大问题。

f*******t
发帖数: 7549
9
看起来decorate pattern最合适
H*****g
发帖数: 638
10
1.composition 不是 OO 专利,OO概念出来之前 C 里面就广泛应用。
2.OO 的 三大支柱是 encapsulation , specialization, and polymorphism。
Inheritance 支撑两个半支柱,所以inheritance 在OO历史上地位是毋庸置疑的。现在
把composition放在inheritance前面就是因为工业界反映inheritance 维护是大问题才
妥协的。OO全盛时期 inheritance 一直是老大。
现在称这composition是OO经典是OO自己往自己脸上贴金。

【在 d******e 的大作中提到】
: 很不幸,你这个真正的工业界的设计就是OO教科书的经典。
: 新手很容易泛用继承。会导致子类的指数爆炸。
: 所以搞Oo的人想出很多模式来解决这个问题。
: 这道题里面的原则可以看 head first pattern?书名记不清了第一张。原则大概是能用
: composition就不要用继承。有详细讨论和扩展讨论。

相关主题
C: what is the output?好记(但不是最优)的combination算法
一道面试的选择题one C++ question
一个容易记忆的permutation算法C++ object size一问
进入JobHunting版参与讨论
m*******l
发帖数: 12782
11
oo现在已经摔落了么?

【在 H*****g 的大作中提到】
: 1.composition 不是 OO 专利,OO概念出来之前 C 里面就广泛应用。
: 2.OO 的 三大支柱是 encapsulation , specialization, and polymorphism。
: Inheritance 支撑两个半支柱,所以inheritance 在OO历史上地位是毋庸置疑的。现在
: 把composition放在inheritance前面就是因为工业界反映inheritance 维护是大问题才
: 妥协的。OO全盛时期 inheritance 一直是老大。
: 现在称这composition是OO经典是OO自己往自己脸上贴金。

l***y
发帖数: 190
12
how about this?
class Person {
Set disabilities;
boolean isNormal() {
if (disabilities.size()==0)
return true;
}
}
class Disability {
String name; //deaf or blind
}

【在 q****x 的大作中提到】
: 某个虚拟世界里,只有四种:正常人,盲人,聋哑人,还有既盲又聋哑人。85%普通人
: ,5%纯盲人,5%纯聋哑,5%盲加聋哑。由于存在患病/治愈的可能,残疾人和正常人可
: 以互相转换。虚拟世界里可能会出现新的疾病,如瘸子。
: 如何设计OO系统?
: 最简单想法,一个Person,内含两个指针,分别指向盲和聋哑两个属性对象。但这个方
: 法显然太粗糙。感觉应该用到设计模式。
: 大家讨论一下吧。

b******d
发帖数: 1948
13
yeah, agree,
Person is a type, same, Disability is another type.
Person has a property, 'disability', of Collection type.
If collection is empty, this is a normal healthy human being.
Disability could be an Enumeration.

【在 l***y 的大作中提到】
: how about this?
: class Person {
: Set disabilities;
: boolean isNormal() {
: if (disabilities.size()==0)
: return true;
: }
: }
: class Disability {
: String name; //deaf or blind

m*p
发帖数: 1331
14
sounds like you don't have formal training of modern OO.
"pointers" are just ugly implementation of OO.
Use interface, abstract class to your design, and yes of course, this
problem is a design pattern problem.

【在 q****x 的大作中提到】
: 某个虚拟世界里,只有四种:正常人,盲人,聋哑人,还有既盲又聋哑人。85%普通人
: ,5%纯盲人,5%纯聋哑,5%盲加聋哑。由于存在患病/治愈的可能,残疾人和正常人可
: 以互相转换。虚拟世界里可能会出现新的疾病,如瘸子。
: 如何设计OO系统?
: 最简单想法,一个Person,内含两个指针,分别指向盲和聋哑两个属性对象。但这个方
: 法显然太粗糙。感觉应该用到设计模式。
: 大家讨论一下吧。

f****n
发帖数: 208
15
Design is relying on the business model, aka, how you are going to use the
classes.
If the project is about curing the disability, I need to have more methods
like these
for deaf,
getDeafLevel(), getDeafReason...
for blind
getEyeVision(Eye eye)...
Where are you going to put these if you just use a collection? An interface
would be more fittable.
Depending on the scope of the project, there are different OO designs. Doesn't necessary mean correct or wrong designs, and industry standard is normally encouraging redundant and complex one to make it looks like over-designed :)

【在 b******d 的大作中提到】
: yeah, agree,
: Person is a type, same, Disability is another type.
: Person has a property, 'disability', of Collection type.
: If collection is empty, this is a normal healthy human being.
: Disability could be an Enumeration.

b******d
发帖数: 1948
16
well, if Disability is too generic, we can define specific Disability types.
EyeDisability, or HearingDisability, the subtype grows.
The design grows as requirement became more and more specific.
DeafReason maybe a third type, Causation, the causation of disability. This
can be property on the Disability.
You are right that there are no right or wrong, over design is a big mistake
but happens again and again.

interface

【在 f****n 的大作中提到】
: Design is relying on the business model, aka, how you are going to use the
: classes.
: If the project is about curing the disability, I need to have more methods
: like these
: for deaf,
: getDeafLevel(), getDeafReason...
: for blind
: getEyeVision(Eye eye)...
: Where are you going to put these if you just use a collection? An interface
: would be more fittable.

h**q
发帖数: 128
17
我觉得你的这个design非常好,而且这个想法就是Bridge模式。

【在 q****x 的大作中提到】
: 某个虚拟世界里,只有四种:正常人,盲人,聋哑人,还有既盲又聋哑人。85%普通人
: ,5%纯盲人,5%纯聋哑,5%盲加聋哑。由于存在患病/治愈的可能,残疾人和正常人可
: 以互相转换。虚拟世界里可能会出现新的疾病,如瘸子。
: 如何设计OO系统?
: 最简单想法,一个Person,内含两个指针,分别指向盲和聋哑两个属性对象。但这个方
: 法显然太粗糙。感觉应该用到设计模式。
: 大家讨论一下吧。

s*****n
发帖数: 2897
18
re
g**********y
发帖数: 14569
19
这个我觉得比较适合Strategy Pattern --
Person class里,定义see, hear, walk, ...等
Java code大致就是:
Public class Person {
SeeBehavior see;
HearBehavior hear;
Public void setSeeBehavior(SeeBehavior see) { this.see = see; }
Public void setHearBehavior(HearBehavior hear) {this.hear = hear; }
Public void watchTV() {
This.see.watchTV();
}
Public void listenMusic() {
This.hear.listenMusic();
}
}
Public interface SeeBehavior {
Public void watchTV();
}
Public interface HearBehavior {
Public void listenMusic();
}
Public class Normal implements SeeBehavior, HearBehavior {
Public void watchTV() {
System.out.println(“watch tv”);
}
Public void listenMusic() {
System.out.println(“listen music”);
}
}
Public class Blind implements SeeBehavior {
Public void watchTV() {
System.out.println(“read tv program with hand.”);
}
}
Public class Deaf implements HearBehavior {
Public void listenMusic() {
// do nothing
}
}
程序部分就是:
Person p1 = new Person();
P1.setSeeBehaivor(new Normal());
P1.setHearBehavior(new Deaf());
P1.watchTV();
P1.listenMusic();
c********n
发帖数: 2
20
enum DisabilityKind // Combine them if multiple kind
{
Normal = 0,
Blind = 1,
Deaf = 2,
Dumb =4,
...
}
abstract class Person
{
DisabilityKind DisabilityKind;
virtual Eye();
virtual Ear();
virtual Mouth();
}
相关主题
One C++ questionWhy I can't compile this function successfully
one C++ questionC++: what is the output? How to interpret it?
发个题目给大家复习一下marcoC++ Q42: (C22)
进入JobHunting版参与讨论
H*****g
发帖数: 638
21
全盛时期啥都想用 OO,不管任何情况,简历只要有OO,OOD,OOP。
前几年要求懂AOP,SOP,ROP...
这两年不知道...

【在 m*******l 的大作中提到】
: oo现在已经摔落了么?
F****n
发帖数: 3271
22
上面的答案基本上都是过时的over-engineering.
Google时代的答案是这样的:
Person {
String typeOfDisability;
// or int typeOfDisability;
}
// defines type of disability as constants.
// regards null or define a special constant as normal;
// you can just set or get the value, no need for "cure methods"
为啥要这样:
1. Simple, minimalist;
2. Open and dynamic, not bound to a pre-defined type.

【在 q****x 的大作中提到】
: 某个虚拟世界里,只有四种:正常人,盲人,聋哑人,还有既盲又聋哑人。85%普通人
: ,5%纯盲人,5%纯聋哑,5%盲加聋哑。由于存在患病/治愈的可能,残疾人和正常人可
: 以互相转换。虚拟世界里可能会出现新的疾病,如瘸子。
: 如何设计OO系统?
: 最简单想法,一个Person,内含两个指针,分别指向盲和聋哑两个属性对象。但这个方
: 法显然太粗糙。感觉应该用到设计模式。
: 大家讨论一下吧。

d**0
发帖数: 984
23
人根据体检表引用适当的"人的实现"。生病或治愈了就换一个"人的实现"
class 人
{
obj 人的实现
status 体检表;
};
class 人的实现
{
看电影;
开车;
}
class 盲人的实现 : class 人的实现

【在 q****x 的大作中提到】
: 某个虚拟世界里,只有四种:正常人,盲人,聋哑人,还有既盲又聋哑人。85%普通人
: ,5%纯盲人,5%纯聋哑,5%盲加聋哑。由于存在患病/治愈的可能,残疾人和正常人可
: 以互相转换。虚拟世界里可能会出现新的疾病,如瘸子。
: 如何设计OO系统?
: 最简单想法,一个Person,内含两个指针,分别指向盲和聋哑两个属性对象。但这个方
: 法显然太粗糙。感觉应该用到设计模式。
: 大家讨论一下吧。

b******d
发帖数: 1948
24
有没有具体一种语言支持你的这种语法?或者说你用Java或者C++来写的话,能不能具
体一点。
比如说,一个person有没有sickness,你可以说String是不是NULL。如果问是什么病,
你可以说,whatever you set, whatever you get, all good. 可是我问有多少person
有Deaf, 你又怎么回答?

【在 F****n 的大作中提到】
: 上面的答案基本上都是过时的over-engineering.
: Google时代的答案是这样的:
: Person {
: String typeOfDisability;
: // or int typeOfDisability;
: }
: // defines type of disability as constants.
: // regards null or define a special constant as normal;
: // you can just set or get the value, no need for "cure methods"
: 为啥要这样:

F****n
发帖数: 3271
25
How many person.sickness == "deaf";

person

【在 b******d 的大作中提到】
: 有没有具体一种语言支持你的这种语法?或者说你用Java或者C++来写的话,能不能具
: 体一点。
: 比如说,一个person有没有sickness,你可以说String是不是NULL。如果问是什么病,
: 你可以说,whatever you set, whatever you get, all good. 可是我问有多少person
: 有Deaf, 你又怎么回答?

H*****g
发帖数: 638
26
当心Google 的人觉得你侮辱了他们的水平,冲出来揍你。
第一句话还像摸象样,但给的答案实在欠缺维护性。
根本没有考虑一个人可能有两种残疾以上,或有新的疾病。
如题所说“虚拟世界里可能会出现新的疾病,如瘸子”,加一个瘸子,你还需定义瘸子
+盲,瘸子+聋哑,瘸子+盲+聋哑…
疾病多的话,越到后来,这些排列组合就越疯狂。光比较就头晕。
竟然还考虑用String,怪不得Java写的这么慢,原来每次都在忙着优化string。
而且用String的话,若一个人有一种被治愈,但还有其他疾病,你如何快速的更新?

【在 F****n 的大作中提到】
: 上面的答案基本上都是过时的over-engineering.
: Google时代的答案是这样的:
: Person {
: String typeOfDisability;
: // or int typeOfDisability;
: }
: // defines type of disability as constants.
: // regards null or define a special constant as normal;
: // you can just set or get the value, no need for "cure methods"
: 为啥要这样:

w*******r
发帖数: 2953
27
LS不少人有语法错误,class 定义后面的分号都忘记了。
class person {
};
b******d
发帖数: 1948
28
still, this is written in English, not in a programming language.

【在 F****n 的大作中提到】
: How many person.sickness == "deaf";
:
: person

w*******r
发帖数: 2953
29
程序和运行结果如下,不考虑瘸子的情况。
#include
using namespace std;
class human
{
public:
human();
human(bool, bool);
~human(){};
bool getEyeState();
bool getMouthState();
char *getHealthDesc();
private:
bool isBlind;
bool isDeaf;
};
human::human()
{
isBlind = 0;
isDeaf = 0;
}
human::human(bool blind, bool deaf)
{
isBlind = blind;
isDeaf = deaf;
}
bool human::getEyeState()
{
return isBlind;
}
bool human::getMouthState()
{
return isDeaf;
}
char *human::getHealthDesc()
{
if (isBlind && isDeaf)
return "blind and deaf";
else if (isBlind)
return "blind";
else if (isDeaf)
return "deaf";
else
return "healthy";
}
int main()
{
human *Tom = new human(1,0); // Tom is blind
human Jack; // Jack is normal
human *Mike = new human (0,1);
human *John = new human (1,1);
cout << "Tom is " << Tom->getHealthDesc() << endl;
cout << "Jack is " << Jack.getHealthDesc() << endl;
cout << "Mike is " << Mike->getHealthDesc() << endl;
cout << "John is " << John->getHealthDesc() << endl;
delete Tom;
Tom = 0;
return 0;
}
Tom is blind
Jack is healthy
Mike is deaf
John is blind and deaf
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.
b******d
发帖数: 1948
30
你这么写是不错的,可问题就是瘸子,如果有瘸子这种情况,这种情况你就要重新定义
Human。
可是如果把Human和Disability分开定义,Human还是Human,你只要define一种新的
sickness而不用改变Human的定义。Human可以有零个或多个Disability。

【在 w*******r 的大作中提到】
: 程序和运行结果如下,不考虑瘸子的情况。
: #include
: using namespace std;
: class human
: {
: public:
: human();
: human(bool, bool);
: ~human(){};
: bool getEyeState();

相关主题
脸书电话面试第一轮代码题面筋请问单耳聋算disability么?
问个c++题悲催地当了G家电面小白鼠
问几个在工作公司买杂七杂八保险的问题, 请指点 (转载)amazon的那道题目
进入JobHunting版参与讨论
w*******r
发帖数: 2953
31
agree, it has to modify human class to support new sickness.
but anyway you are changing the source code to support the
new sickness. if we want to avoid it, then have to bring in
concept of database or write something to a file, then can
expand it without changing source code, but that would take
too much time.

【在 b******d 的大作中提到】
: 你这么写是不错的,可问题就是瘸子,如果有瘸子这种情况,这种情况你就要重新定义
: Human。
: 可是如果把Human和Disability分开定义,Human还是Human,你只要define一种新的
: sickness而不用改变Human的定义。Human可以有零个或多个Disability。

d****o
发帖数: 1055
32
对于OOD,基本原则就是不要改变原有的类,这是一个原则啊。
the open/closed principle states "software entities (classes, modules,
functions, etc.) should be open for extension, but closed for modification"

【在 w*******r 的大作中提到】
: agree, it has to modify human class to support new sickness.
: but anyway you are changing the source code to support the
: new sickness. if we want to avoid it, then have to bring in
: concept of database or write something to a file, then can
: expand it without changing source code, but that would take
: too much time.

b******d
发帖数: 1948
33
well, not really, depends on usecase.
Assume we separate Human, and Disability. And Human could have a collection
of Disability.
If there is callback on Human, Human.hasDisability(). And a new type of
Disability get defined. No change is necessary on Human type.
Logically, Human is a human, Disability is a sickness. No mixing between
each other.
Human, name, gender, DOB, SSN, address
Disability, location, reason, ICD9
1. If this human is healthy? check if disability collection is empty or not
2. If this human has eye problem? iterate through all disabilities, check if
any Disability.location == 'EYE'
3. If this human is deaf and blind? iterate through all disabilities, check
if any location == 'EYE' and location == 'MOUTH'
Yes, when new Disability comes up, we may need refactor some part of code,
but hopefully, I can restrict the changes in Disability itself without
touching other part of class hierarchy.
After all, this is a 'legacy' way of programming.
At GOOGLE time, I cannt think of a way to covert a STRING to a specific
value, 'DEAF' or 'BLIND'. If it is a joke, I think I missed the point, LOL.

【在 w*******r 的大作中提到】
: agree, it has to modify human class to support new sickness.
: but anyway you are changing the source code to support the
: new sickness. if we want to avoid it, then have to bring in
: concept of database or write something to a file, then can
: expand it without changing source code, but that would take
: too much time.

F****n
发帖数: 3271
34
If there are more than one sickness, use a list.
The reason to use string is to facilitate full text search.
The reason not to use objects is because in an open and dynamic world,
you don't want users to define a new object for every new sickness.
If you have a rich set of sickness, you can manage them with a declarative vocabulary set, which can be maintained by an open classifier (e.g. a crawler) and an computational ontology.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
没办法, Google的人就我这水平

【在 H*****g 的大作中提到】
: 当心Google 的人觉得你侮辱了他们的水平,冲出来揍你。
: 第一句话还像摸象样,但给的答案实在欠缺维护性。
: 根本没有考虑一个人可能有两种残疾以上,或有新的疾病。
: 如题所说“虚拟世界里可能会出现新的疾病,如瘸子”,加一个瘸子,你还需定义瘸子
: +盲,瘸子+聋哑,瘸子+盲+聋哑…
: 疾病多的话,越到后来,这些排列组合就越疯狂。光比较就头晕。
: 竟然还考虑用String,怪不得Java写的这么慢,原来每次都在忙着优化string。
: 而且用String的话,若一个人有一种被治愈,但还有其他疾病,你如何快速的更新?

w*******r
发帖数: 2953
35
got it, thanks.

【在 d****o 的大作中提到】
: 对于OOD,基本原则就是不要改变原有的类,这是一个原则啊。
: the open/closed principle states "software entities (classes, modules,
: functions, etc.) should be open for extension, but closed for modification"

w*******r
发帖数: 2953
36
改完的程序在下面,这下疾病可以随便定义了,也可以治病了。
想给给个病安个计数器,记得好像有个什么关健词可以让所有的object改动的都是
同一个变量而不是各自的变量(counter),记不起来了,所以这个counter打印得
不对,有知道的告诉我一声。
#include
#include
#include
using namespace std;
class Disability
{
public:
Disability(){counter = 0; };
Disability(string, string);
~Disability(){};
string get_disease_name() const { return name; };
string get_disease_desc() const { return desc; };
void incr_sick_human_counter() {counter++; };
unsigned long get_human_counter() const { return counter; };
private:
string name;
string desc;
unsigned long counter;
};
Disability::Disability(string disease_name, string disease_desc)
{
name = disease_name;
desc = disease_desc;
}
class human
{
public:
human(){};
human(Disability);
~human(){};
void getSick(Disability);
void cureDisease(Disability);
string getHealthDesc();
private:
vector disease;
};
human::human(Disability sickness)
{
disease.push_back(sickness);
sickness.incr_sick_human_counter();
}
void human::getSick(Disability sickness)
{
disease.push_back(sickness);
sickness.incr_sick_human_counter();
}
void human::cureDisease(Disability sickness)
{
for (int i=0; i < disease.size(); i++)
if (disease[i].get_disease_name() == sickness.get_disease_name())
{
disease.erase (disease.begin()+i);
cout << "erase " << disease[i].get_disease_name() << endl;
}
}
string human::getHealthDesc()
{
string sHealthDesc;
if (disease.size() == 0)
sHealthDesc = "healthy";
else
{
sHealthDesc = disease[0].get_disease_name();
for (int i = 1; i < disease.size(); i++)
sHealthDesc = sHealthDesc + " and " + disease[i].get_disease_
name();
}
return sHealthDesc;
}
int main()
{
Disability deaf("deaf", "can not speak and listen\n");
Disability blind("blind", "can not see\n");
human Tom; // Tom is healthy
human Jack(deaf); // Jack is deaf
Jack.getSick(blind); // Jack got blind in additional to deaf
human Mike(deaf);
human John(blind);
cout << "Tom is " << Tom.getHealthDesc() << endl;
cout << "Jack is " << Jack.getHealthDesc() << endl;
cout << "Mike is " << Mike.getHealthDesc() << endl;
cout << "John is " << John.getHealthDesc() << endl;
cout << "after a few years ...\n";
Jack.cureDisease(deaf); // Jack is no longer deaf
Tom.getSick(deaf); // Tom become deaf
cout << "Tom is " << Tom.getHealthDesc() << endl;
cout << "Jack is " << Jack.getHealthDesc() << endl;
cout << "how many people are deaf: " << deaf.get_human_counter() << endl;
cout << "how many people are blind: " << blind.get_human_counter() <<
endl;
return 0;
}
运行结果:
Tom is healthy
Jack is deaf and blind
Mike is deaf
John is blind
after a few years ...
erase blind
Tom is deaf
Jack is blind
how many people are deaf: 6488064
how many people are blind: 4279008
Process returned 0 (0x0) execution time : 0.031 s
Press any key to continue.
b******d
发帖数: 1948
37
大概是这么个意思了。
这个human_counter有待考虑,Disability和具体的count没关系。
画个图凑合看看。不能跟教科书比。

【在 w*******r 的大作中提到】
: 改完的程序在下面,这下疾病可以随便定义了,也可以治病了。
: 想给给个病安个计数器,记得好像有个什么关健词可以让所有的object改动的都是
: 同一个变量而不是各自的变量(counter),记不起来了,所以这个counter打印得
: 不对,有知道的告诉我一声。
: #include
: #include
: #include
: using namespace std;
: class Disability
: {

b******d
发帖数: 1948
38
well, 要index/search最好都是String,可是不是所有的问题都要用到search engine
吧。其实我前个回帖就怕你提到search。两码事。。。人问OO跟search似乎没啥关系。

vocabulary set, which can be maintained by an open classifier (e.g. a
crawler) and an computational ontology.

【在 F****n 的大作中提到】
: If there are more than one sickness, use a list.
: The reason to use string is to facilitate full text search.
: The reason not to use objects is because in an open and dynamic world,
: you don't want users to define a new object for every new sickness.
: If you have a rich set of sickness, you can manage them with a declarative vocabulary set, which can be maintained by an open classifier (e.g. a crawler) and an computational ontology.
:
: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: 没办法, Google的人就我这水平

w*******r
发帖数: 2953
39
thanks.
the counter is unnecessary, i planed to add a statistics counter to counter
how many people got blind disease, how many people got deaf etc, but it was
not in the requirement, so it would be better take it out.

【在 b******d 的大作中提到】
: 大概是这么个意思了。
: 这个human_counter有待考虑,Disability和具体的count没关系。
: 画个图凑合看看。不能跟教科书比。

1 (共1页)
进入JobHunting版参与讨论
相关主题
C++: what is the output? How to interpret it?Interview questions, Bloomberg
C++ Q42: (C22)C: what is the output?
脸书电话面试第一轮代码题面筋一道面试的选择题
问个c++题一个容易记忆的permutation算法
问几个在工作公司买杂七杂八保险的问题, 请指点 (转载)好记(但不是最优)的combination算法
请问单耳聋算disability么?one C++ question
悲催地当了G家电面小白鼠C++ object size一问
amazon的那道题目One C++ question
相关话题的讨论汇总
话题: human话题: disability话题: deaf话题: public话题: blind