《左耳听风》-ARTS-打卡记录-第十七周

《左耳听风》-ARTS-打卡记录-第十七周

Algorithm

《Head First设计模式》中的策略模式

首先,鴨子类委托两个行为类,用来封装它当中存在变化的量。这两个行为类是两个接口类,他下面的子类去实现他要进行的行为。而鸭子类中可以去定义一个设置器,在运行的时候可以去设置具体的行为对象。比如,一开始鸭子的飞行行为是不会飞,当你给他设置一个像火箭一样飞这样的行为对象时,它就能够按照规定的行为去实现。

这样设计的好处:
将变化的和不变的分离出来,当变化部分有新的需求时,可以通过委托的成员对象来做,而自己类不需要改变。

书中设计的总结

之前的状况:
有一个Duck超类,有quack(),swim(),display()三种方法,具体的鸭子类去继承自它,其中display()是抽象方法,子类自己去实现;而quack()超类中是呱呱叫,有可能子类需要覆盖。
书中需求:
要推出一种会飞的鸭子,要增加fly()的方法,来将竞争者抛在后头。
书中设计的演化过程:
1.一开始采用在超类中增加fly()方法,其他类仍然继承自该超类,出现了橡皮鸭在屏幕上飞来飞去的场景。(超类中加上了新的行为,会使得不适合的子类也增加了该行为)。 -》 他想到了把fly()给覆盖掉,但是每当有新的鸭子子类出现时,他就需要检查并可能需要覆盖quack和fly方法(但我感觉这还好啊,只是需要改变子类而已啊。这里提出疑问 解答:可能不是我想的那样,比如这里的超类把fly()设为正常飞行,则不会飞的需要覆盖掉,像火箭一样飞行也需要覆盖掉;倘若在超类中定义为fly()为不会飞,则每个会飞的子类都需要覆盖该方法)。
2.后来考虑将将fly()从超类中取出来放进Flyable接口类中,会飞的子类去继承并实现该方法。 -》这样一来,每个类都需要修改代码,加上继承Flyable接口类的代码;并且每个子类都必须实现接口的方法,无法复用父类中的方法了
3.后来采取将变化的部分抽离出来,建立一组新类来代表每个行为。
鸭子类实现的UML图

#include <iostream>

//飞行的抽象基类
class FlyBehavior
{
public:
virtual void fly()=0;
};

//叫声的抽象基类
class QuackBehavior
{
public:
virtual void quack() = 0;
};

class FlyWithWings : public FlyBehavior
{
public:
void fly()
{
	std::cout << "I fly with wings" << std::endl;
}
};

class FlyNoWay : public FlyBehavior
{
public:
void fly()
{
	std::cout << "I  cant't fly" << std::endl;
}
};

class Quack : public QuackBehavior
{
public:
void quack()
{
std::cout << "I make voice by quack" << std::endl;
}
};

class Squeak: public QuackBehavior
{
public:
void quack()
{
std::cout << "I make voice by squeak" << std::endl;
}
};

class Duck
{
private:
FlyBehavior * m_pFB;
QuackBehavior  * m_pQB;

public:
void swim()
{
std::cout << "Each duck can swim" << std::endl;
}

virtual void display() = 0;

void setFlyBehavior(FlyBehavior *pFB)
{
m_pFB = pFB;
}

void setQuackBehavior(QuackBehavior *pQB)
{
m_pQB = pQB;
}

void performFly()
{
m_pFB->fly();
}

void performQuack()
{
m_pQB->quack();
}
};

class RedHeadDuck : public Duck
{
public:
void display()
{
std::cout << "My head is red" << std::endl;
}
};

int main()
{
FlyBehavior *pFB = new FlyWithWings();
QuackBehavior *pQB = new Quack();
RedHeadDuck rhDuck;
rhDuck.setFlyBehavior(pFB);
rhDuck.setQuackBehavior(pQB);
rhDuck.performFly();
rhDuck.performQuack();
rhDuck.swim();
rhDuck.display();
delete pFB;
delete pQB;
return 0;
}

运行结果:

I fly with wings
I make voice by quack
Each duck can swim
My head is red

Review

Tact Filters(鉴赏力过滤器理论)

(感觉翻译的不是很顺畅,没有充分理解作者tact的含义)
I came up with this idea several years ago in a conversation with a friend at MIT, who was regularly finding herself upset by other people who worked in her lab. The analogy worked so well in helping her to understand her co-workers that I decided to write it up and put it on the web. I’ve gotten quite a few email messages since then from other people who have also found it helpful.
我想起这个主意是在很多年前,和一个MIT的朋友交谈的时候,她发现自己总是被同实验室的其他人弄的不开心.我的这一类比在帮助她理解她的同事方面起到了不错的效果,所以我决定把它写下来并发表在网站上.自从其他人也发现他有帮助后,我得到相当多的邮件信息.
All people have a “tact filter”, which applies tact in one direction to everything that passes through it. Most “normal people” have the tact filter positioned to apply tact in the outgoing direction. Thus whatever normal people say gets the appropriate amount of tact applied to it before they say it. This is because when they were growing up, their parents continually drilled into their heads statements like, “If you can’t say something nice, don’t say anything at all!”
所有人都有一个"鉴赏力过滤器",它会对每一个经过它的事情加上鉴赏力.大多数"正常人"有一个对朝向外面的鉴赏力过滤器.因此,普通人无论说什么,都会在他说之前加上一些适当的鉴赏力.这是因为他们成长的过程中,他们的父母教导他们的大概是这样的,“如果你不能够说一些好的东西,你就什么也不要说!”.
“Nerds,” on the other hand, have their tact filter positioned to apply tact in the incoming direction. Thus, whatever anyone says to them gets the appropriate amount of tact added when they hear it. This is because when nerds were growing up, they continually got picked on, and their parents continually drilled into their heads statements like, “They’re just saying those mean things because they’re jealous. They don’t really mean it.”
而"令人厌烦的人",则恰恰相反,有一个方向朝内的鉴赏力过滤器.因此,无论别人说什么,他们听到后都会添加一些鉴赏力.这是因为当令人厌烦的人成长的过程中,他们的父母持续的教导他们,“他们说这些卑鄙的事情是因为他们嫉妒.他们并不是真的言出必行”.
When normal people talk to each other, both people usually apply the appropriate amount of tact to everything they say, and no one’s feelings get hurt. When nerds talk to each other, both people usually apply the appropriate amount of tact to everything they hear, and no one’s feelings get hurt. However, when normal people talk to nerds, the nerds often get frustrated because the normal people seem to be dodging(逃避) the real issues and not saying what they really mean. Worse yet, when nerds talk to normal people, the normal people’s feelings often get hurt because the nerds don’t apply tact, assuming the normal person will take their blunt(直言的) statements and apply whatever tact is necessary.
当正常人之间相互交谈时,他们经常将适当的鉴赏力添加到他们说的每件事情上,没有人感觉受伤.当令人厌烦的人之间交谈时,这些人经常把适当的鉴赏力添加到它听到的每句话中,没有人感觉受伤.然而,当正常人向令人厌烦的人说话时,令人厌烦的人经常感到沮丧,因为普通人似乎在逃避事实,而且不想说他们的本意.更糟糕的是,当令人厌烦的人去向正常人说话时,正常人经常感到受伤害,因为对方不使用圆滑的方式,并且假设正常人都是直言的,添加怎样的鉴赏力都是必要的(这一句感觉不太懂).
So, nerds need to understand that normal people have to apply tact to everything they say; they become really uncomfortable if they can’t do this. Normal people need to understand that despite(尽管) the fact that nerds are usually tactless, things they say are almost never meant personally and shouldn’t be taken that way. Both types of people need to be extra patient when dealing with someone whose tact filter is backwards relative to their own.
因此,令人厌烦的人需要去理解正常人必须在他们说的每件事上加上鉴赏力;他们变得不舒服如果他们不这样做.正常人应该理解尽管令人厌烦的人经常是得罪人的,但是他们所说的事情并不意味他这人真的是这样,而且不应该采取那种方式.两种类型的人都需要多花一些耐心,当和自己鉴赏力相反的人打交道时.

Reflections on this Essay after Ten Years

During the ten years since I wrote up the Tact Filter theory and put it on the (then fledgeling) web, I’ve gotten quite a bit of fan mail about it. I’ve been tempted to make some minor edits (such as substituting “geek” for “nerd”), but I think that’s better left to this addendum.

十年后这篇短文的反应

在我写这篇理论的十年间,我得到了很多有关于这个话题的粉丝邮件.我曾经被劝诱去做一些小小的修改(比如用"geek"来替换"nerd"),但是我觉得它作为一个附录放在这里更加合适.

Related Links

Fanspeak, originally posted to rec.arts.sf.fandom in 1999 by Cally Soukupa, and mirrored on Karl Musser’s website.
Ozark English, posted by Suzette Haden Elgin in her LiveJournal.


Tips

1.选择文件夹重命名,而非直接删除
2.不要轻易相信销售,不要冲动交定金
3.穷忙陷阱:每天只是花时间在工作上,而不做提升自我的努力(比如看书,学习技能);输出的效率要高于输入,所以学完可以给自己出题考考自己,可以写下总结,比如博客。

Share

SVN trunk(主线) branch(分支) tag(标记) 用法详解和详细操作步骤
svn使用方式:
1.有新版本就从trunk上打一个新的tag(只读)
2.新需求在turnk中开发.
3.之前版本存在问题时,可以从当时的tag下拉到branches中,并在branches下修改;修改好后,再打新的tag;接着再将branches中的代码和trunk中的代码合并.开发换再切换到trunk上
4.trunk和branches是并行的.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingLife99

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值