这是设计模式中最根本的哲学,注重接口,而不是实现,依赖接口,而不是实现。接口是抽象是稳定的,实现则是多种多样的。以后面我们会面向对象的SOLID原则中会提到我们的依赖倒置原则,就是这个原则的的另一种样子。还有一条原则叫Composition over inheritance(喜欢组合而不是继承),这两条是那23个经典设计模式中的设计原则。
迪米特法则(Law of Demeter),又称“最少知识原则”(Principle of Least Knowledge),其来源于1987年荷兰大学的一个叫做Demeter的项目。Craig Larman把Law of Demeter又称作“不要和陌生人说话”。在《程序员修炼之道》中讲LoD的那一章叫作“解耦合与迪米特法则”。关于迪米特法则有一些很形象的比喻:
软件工程大师Robert C. Martin把里氏代换原则最终简化为一句话:“Subtypes must be substitutable for their base types”。也就是,子类必须能够替换成它们的基类。即:子类应该可以替换任何基类能够出现的地方,并且经过替换以后,代码还能正常工作。另外,不应该 在代码中出现if/else之类对子类类型进行判断的条件。里氏替换原则LSP是使代码符合开闭原则的一个重要保证。正是由于子类型的可替换性才使得父类 型的模块在无需修改的情况下就可以扩展。
Do not fear to be eccentric in opinion, for every opinion now accepted was once eccentric.
不要害怕怀有怪念头,因为现在人们接受的所有的观念都曾经是怪念头。
Democracy is the process by which people choose the man who’ll get the blame.
民主,就是挑选那个受批评的人的过程。
Drunkenness is temporary suicide.
喝醉是暂时性的自杀。
I say quite deliberately that the Christian religion, as organized in its Churches, has been and still is the principal enemy of moral progress in the world.
我明确的说,由教会所组织的基督教是道德进步的最大敌人,过去如此,现在依然如此。
A process which led from the amoeba to man appeared to the philosophers to be obviously a progress though whether the amoeba would agree with this opinion is not known.
从阿米巴变形虫到人类的这一过程对哲学家来说,很明显是个进步。但是变形虫怎么想我们就不知道了。
I would never die for my beliefs because I might be wrong.
我永远不会为信仰而死,因为我的信仰可能是错的。
In America everybody is of the opinion that he has no social superiors, since all men are equal, but he does not admit that he has no social inferiors.
在美国,所有人都认为没有什么人比他的社会地位高,因为人人生而平等。但是,他可不承认没有人比他社会地位低。
It has been said that man is a rational animal. All my life I have been searching for evidence which could support this.
据说人是一种理性动物。穷我自己一生,我都在寻找这观点的证据。
Man is a credulous animal, and must believe something; in the absence of good grounds for belief, he will be satisfied with bad ones.
人是轻信的动物,必须得相信点什么。如果这种信仰没有什么好的依据,糟糕的依据也能对付。
Many a man will have the courage to die gallantly, but will not have the courage to say, or even to think, that the cause for which he is asked to die is an unworthy one.
很多人可以勇敢的死去,但是却没有勇气说他为之而死的原因没有意义,甚至连这样想一想的勇气也没有。
Many people when they fall in love look for a little haven of refuge from the world, where they can be sure of being admired when they are not admirable, and praised when they are not praiseworthy.
很多人陷入爱情是为了寻找一个遁世的避难所。在这个避难所里,当他们不值得爱慕的时候,依然有人爱慕他们,当他们不值得赞扬的时候,依然有人赞扬他们。
Many people would sooner die than think; in fact, they do so.
有很多人,让他们思考一下还不如让他们去死。事实上,很多人还没思考过就已经死了。
Men are born ignorant, not stupid. They are made stupid by education.
人生而无知,但还不愚蠢。教育才把他们变蠢。
Conventional people are roused to fury by departure from convention, largely because they regard such departure as a criticism of themselves.
传统的人看到背离传统的行为就大发雷霆,主要是因为他们把这种背离当作对他们的批评。
A hallucination is a fact, not an error; what is erroneous is a judgment based upon it.
幻觉不是你的错,在幻觉中做决定,这就是你的不对了。
A life without adventure is likely to be unsatisfying, but a life in which adventure is allowed to take whatever form it will is sure to be short.
生活中完全没有冒险,这可能是没什么意思的。但是生活中如果不管什么种类的探险都有,那肯定是短暂的。
Against my will, in the course of my travels, the belief that everything worth knowing was known at Cambridge gradually wore off. In this respect my travels were very useful to me.
我曾相信,所有值得知道之事,我在剑桥都知道了。在我旅行的过程之中,这一想法逐渐消失了。这与我本意相反,但是却对我非常有益。
Aristotle maintained that women have fewer teeth than men; although he was twice married, it never occurred to him to verify this statement by examining his wives’ mouths.
亚里士多德说女人比男人的牙齿要少。尽管他结了两次婚,但是他都没想过要检查一下他老婆的牙。
Contempt for happiness is usually contempt for other people’s happiness, and is an elegant disguise for hatred of the human race.
对幸福的轻蔑通常是对其他人幸福的轻蔑,在精巧的伪装之下是对人类的仇恨。
For most people, the real life is long, is the ideal and may compromise between continuous compromise.
Undoubtedly the desire for food has been, and still is, one of the main causes of great political events. But man differs from other animals in one very important respect, and that is that he has desires which are, so to speak, intimate, which can never be fully gratified, and which should keep him restless even in Paradise. The boa constrictor, when he had an adequate meal, goes to sleep, and does not wake until he needs another meal. Human beings, for the most not part are not like this. When the Arabs, who had been used to living sparingly on a few dates, acquired the riches of the Eastern Roman Empire and dwelt in palaces of almost unbelievable luxury, they did not, on that account, become inactive. Hunger could no longer be a motive, for Greek slaves supplied them with exquisite viands at the slightest nod. But other desires kept them active; four in particular, which we can label acquisitiveness, rivalry, vanity and love of power.
Acquisitiveness-the wish to possess as much as possible of goods, or the title to goods-is a motive which, I suppose, has its origin in a combination of fear with the desire for necessaries.
I once befriended two little girls from Esthonia, who had narrowly escaped death from starvation in a famine. They lived in my family, and of course had plenty to eat. But they spent all their leisure visiting neighbouring farms and stealing potatoes, which they hoarded. Rockefeller, who in his infancy had experienced great poverty, spent his adult life in a similar manner. Similarly the Arab chieftains on their silken Byzantine divans could not forget the desert, and hoarded riches far beyond any possible physical need. But whatever the psychoanalysis of acquisitiveness, no one can deny that it is one of the great motives -especially among the more powerful, for ,as I said before, it is one of the infinite motives .However much you may acquire you will always wish to acquire more ;satiety is a dream which will always elude you.
But acquisitiveness, although it is the mainspring of the capitalist system, is by no means the most powerful of the motives that survive the conquest of hunger .Rivalry is a much stronger motive. Over and over again in Muhammadan history, dynasties have come to grief because the sons of a sultan by different mothers could not agree, and in the resulting civil war universal ruin resulted. The same sort of thing happens in modern Europe When the British Government very unwisely allowed the Kaiser to be present at a naval review at Spithead, the thought which arose in his mind was not the one which we had intended. What he thought was.” I must have a Navy as good as Grandmamma’s.”And from this thought pier place than it is if acquisitiveness were always stronger than rivalry. But in fact ,a great many men will cheerfully face impoverishment if they can thereby secure complete ruin for their rivals, Hence the present level of taxation.
Vanity is a motive of immense potency. Anyone who has much to do with children knows how they are constantly performing some antic and saying “Look at me”. “Look at me “is one of the most fundamental desires of the human heart. It can take innumerable forms, from buffoonery to the pursuit of posthumous fame. There was a Renaissance Italian princeling who was asked by the priest on his deathbed if he had anything to repent of “Yes,” he said “There is one thing. On one occasion I had a visit from the Emperor and the Pope simultaneously .I too tem to the top of my tower to see the view, and I neglected the opportunity to throw them both down .which would have given me immortal fame.” history does not relate whether the priest gave him absolution. One of the troubles about vanity is that it grows with what it feeds on .The talked about. The condemned murderer who is allowed to see the account of his trial in the Press is indignant if he finds a newspaper which has reported it inadequately. And the more he finds about himself in other newspapers, the more indignant he will be with those whose reports are meager. Politicians and literary men are in the same case. And the more famous they become, the more difficult the press cutting agency finds it to satisfy them .It is scarcely possible to exaggerate the influence of vanity throughout the range of human life, from the child of three to the potentate at whose frown the world trembles. Mankind have even committed the impiety of attributing similar desires to the deity, whom they imaging avid for continual praise.
But great as is the influence of the motives we have been considering, there is one which out weighs these all……Power, like vanity, is insatiable. Nothing short of omnipotence could satisfy it completely. And as it is especially the vice of energetic men, the casual efficacy of love of power is out of all proportion to its frequency. It is ,indeed, by far the strongest motive in the lives of important men .Love of power is greatly increased by the experience of power ,and this applies to petty power as well as to that of potentates ,In the happy days before 1914,when well-to -do ladies could acquire a host of servants, their pleasure in exercising power over the domestics steadily increased with age .Similarly, in any autocratic regime, the holders of power become increasingly, tyrannical with experience of the delights that power can afford. Since power over human beings is shown in making them do what they would rather not do, the man who is actuated by love of power is more apt to inflict pain than to permit pleasure .If you ask your boss for leave lf absence from the office on some legitimate occasion, his love of power will derive more satisfaction from refusal than from consent .If you require a building permit, the petty official concerned will obviously get more pleasure from saying “No” than from saying “Yes”. It is this sort of thing which makes the love of power such a dangerous motive ,But it has other sides which are more desirable .The pursuit of knowledge is ,I think ,mainly actuated by love lf power ,And so are all advances in scientific technique ,In politics also ,a reformer may have just as strong a love of power as a despot .It would be a complete mistake to decry love of power altogether as a motive ,Whether you will be led by this motive to actions which are useful ,or to actions which are pernicious ,depends upon the social system ,and upon your capacities .
I come now to other motives which ,though in a sense less fundamental than those we have been considering ,are still of considerable importuned ,The first of these is love of excitement .Human beings show their superiority to the brutes by their capacity for boredom ,though I have sometimes thought ,in Examining the apes at the Zoo, that they ,perhaps ,have the rudiments of this tiresome emotion .However that may be, experience shows that escape from boredom is one of the really powerful desires of almost all human beings.
When white men first effect contact with some unspoilt race of savages, they offer them all kinds of benefits, from the light of the Gospel to pumpkin pie. These, however, much as we may regret it, most savages receive with indifference. What they really value among the gifts that we bring to them is intoxicating liquor ,which enables them .for the first time in their lives ,to have the illusion, for a few brief moments ,that it is better to be alive than dead.
Red Indians ,while they were still unaffected by white men ,would smoke their pipes .not calmly as we do ,but orgiastically ,in haling so deeply that they sank into a faint ,And when excitement by means of nicotine failed ,a patriotic orator would stir them up to attack a neighbouring tribe ,which would give them all the enjoyment that we (according to our temperament ) derive from a horse race of a General Election.
With civilized men, as with primitive Red Indian tribes, it is, I think, chiefly love of excitement which makes the populace applaud when war breaks out; the emotion is exactly the same as at a football match, although the results are sometimes somewhat more serious.
It is not altogether easy to decide what is the root cause of the love of excitement. I incline to think that our mental make-up is adapted to the stage when men lived by hunting .When a man spent a long day with very primitive weapons in stalking a deer with the hope of dinner and when ,at the end of the day ,he dragged the carcase triumphantly to his cave ,he sank down in contented weariness, while his wife dressed and cooked the meat ,He was sleepy ,and his bones ached ,and the smell of cooking filled every nook and cranny of his consciousness. At last after eating, he sank into deep sleep. In such a life there was neither time nor energy for boredom. But when he took to agriculture, and made his wife do all the heavy work in the fields, he had time to reflect upon the vanity of human life, to invent mythologies and systems of philosophy, and to dream of the life hereafter in which he would perpetually hunt the wild boar of Valhalla.
Our mental make-up is suited to a life of very severe physical labour , I used ,when I was younger ,to take my holidays walking ,I would cover 25 miles a day ,and when the evening came I had no need of anything to keep me from boredom ,since the delight of sitting amply sufficed .But modern lift cannot be conducted on these physically strenuous principles ,A great deal of work is sedentary and most manual work exercises only a few specialized muscles. When London crowds assemble in Trafalgar Square to cheer to the echo an announcement that the government has decided to have them killed ,they would not do so if they had walked 25 miles that day .This cure for bellicosity is ,however ,impracticable and if the human race is to survive – a thing which is ,perhaps ,undesirable -other means must be found for securing an innocent outlet for the unused physical energy that produces love of excitement.
This is a matter which has been too little considered, both by moralists and by social reformers .The social reformers are of the opinion that they have more serious things to consider , The moralists ,on the other hand ,are immensely impressed with the seriousness of all the permitted outlets of the love of excitement; the seriousness ,however ,in their minds is that of Sin ,Dance halls ,cinemas ,this age of jazz are all ,if we may believe our ears gateways to Hell, and we should be better employed sitting at home contemplating our sins. I find myself unable to be in entire agreement with the grave men who utter these warnings. The devil has many forms, some designed to deceive the young ,some designed to deceive the old and serious .If it is the devil that tempts the young to enjoy themselves ,is it not ,perhaps ,the same personage that persuades the old to condemn their enjoyment? And is not condemnation perhaps merely a form of excitement appropriate to old age? And is it not , perhaps ,a drug which -like opium -has to be taken in continually stronger doses to produce the desired effect? Is it not to be feared that ,beginning with the wickedness of the cinema, we should be led step by step to condemn the opposite political party ,dagoes ,wops, Asiatics ,and ,in short, everybody except the fellow members of our club? And it is from just such condemnations, when widespread, that wars proceed. I never heard of a war that proceeded from dance halls.
What is serious about excitement is that so many of its forms are destructive. It is destructive in those who cannot resist excess in alcohol or gambling. It is destructive when it takes the form of mob violence. And above all it is destructive when it leads to war. It is so deep a need that is will find harmful outlets of this kind unless innocent outlets are at hand. There are such innocent outlets at present in sport, and in politics so long as it is kept in constitutional bounds. But these are not sufficient, especially as the kind of politics that is most exciting is also the kind that does most harm.
Civilized life has grown altogether too tame, and, if it is to be stable, it must provide harmless outlets for the impulses which our remote ancestors satisfied in hunting. In Australia ,where people are few and rabbits are many ,I watched a whole populace satisfying the primitive impulse in the primitive manner by the skilful slaughter of many thousands of rabbits ,But in London or New York, where people are many and rabbits are few ,some other means must be found to gratify primitive impulse ,I think every big town should contain artificial waterfalls that people could descend in very fragile canoes ,and they should contain bathing pools full of mechanical sharks ,Any persons found advocating a preventive war should be condemned to two hours a day with these ingenious monsters.