官术网_书友最值得收藏!

A functional solution

Let's try to be more general: after all, requiring that some function or other be executed only once, isn't that outlandish, and may be required elsewhere! Let's lay down some principles:

  • The original function (the one that may be called only once) should do that thing, and no other
  • We don't want to modify the original function in any way
  • We need to have a new function that will call the original one only once
  • We want a general solution that we can apply to any number of original functions

The first principle listed previously is the single responsibility principle (the S in S.O.L.I.D.), which states that every function should be responsible over a single functionality. For more on S.O.L.I.D., check the article by Uncle Bob (Robert C. Martin, who wrote the five principles) at http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod.

Can we do it? Yes; and we'll write a higher-order function, which we'll be able to apply to any function, to produce a new function that will work only once. Let's see how!

主站蜘蛛池模板: 龙胜| 澄迈县| 黄冈市| 花莲市| 桑日县| 夏津县| 新绛县| 扎囊县| 桐庐县| 吉隆县| 永年县| 溆浦县| 额尔古纳市| 东辽县| 新津县| 洛扎县| 西乌| 铁岭县| 临桂县| 玉田县| 澄城县| 大城县| 怀远县| 来宾市| 遵化市| 孝义市| 金平| 牡丹江市| 古田县| 新乡市| 胶南市| 池州市| 南昌市| 武川县| 清原| 车险| 吐鲁番市| 彭水| 合阳县| 德安县| 秀山|