Do not go the same language - On Grammar of Fun

  This article continues Fun syntax is worth two.

  I / O (Input / Output), input and output is the most prominent feature of the computer, it can be said that most of the core functions of the computer. No I / O, the computer is a pile of scrap copper scrap. Starting from the lowest level of electronic components, computer scientists and engineers, we have been running in the pursuit of excellence in I / O performance on the road. Computer every big leap, is one I / O reborn. From the mechanical age to tubes, to transistors, to integrated circuits, to the future of quantum era, all indicates that the importance of I / O for computer science. But this is only the most basic I / O layer, on top of that, there are more levels of abstraction, such as memory, disk, network; more I / O manifestations, such as files, databases, mouse and keyboard, monitor, printer and many more. "Everything is a file", which is one of the basic philosophy of Unix / Linux, from this point of view, that all are I / O.

  Underlying design is so, then the high-level design is still the case. From various languages ​​for implement I / O point of view, all the I / O placed top priority of place, from the I / O design, you can basically tell a design philosophy and philosophy of language. For example, there are a lot of java class name contains the stream, stressed the concept of flow; and golang is no word, more emphasis is byte, namely byte. From the point of view of all files i.e., the byte corresponds to more intuitively the contents of the file. Java is in turn conducted an abstraction equivalent to the number of bytes considered continuous stream of bytes, while providing more convection operation method, such as flip, mark and the like.

  In Java, I / O related SDK uses a lot of decorative patterns and adapters. Decorator is actually a method to make enhancements to the original class, but adapter sucked unusual types of things become the same type of thing. That is, for example StringReader adapter class that implements a object interface flow Reader. Its function is non-String objects adapted to flow a stream object. There is also a legacy SDK called StringInputStream class, it is also the adapter class. Detailed adapter decorator follows:

item use PARTICIPANTS
Decorator Enhancements Two or more classes of the same type
adapter Type conversion (adapted) Two different types of classes

  In the language go, I / O is also a large number of such a design. such as:

item Related classes Examples
Decorator MultiReader, MultiWriter, bufio.Reader, bufio.Writer, bufio.Scanner, LimitReader, TeeReader -
adapter string.Reader, bytes.Reader ex: strings.NewReader("test")

  Decorated with the use of an adapter, so that the design of the library go greatly simplified, and thus produce unparalleled flexibility and scalability, developers can design their own new decorators, adapter complete development tasks.

  Then, in the actual development process, decorator and adapter What magical it? In the example described earlier, the two are applied to interface with the struct level, in fact, go language, the application is in conjunction with both func Even better, you can bring fresh, unexpected experience. such as:

package main

import "fmt"

func greet(name string) {
    fmt.Printf("Hello %s!\n", name)
}

func decorateGreet(f func(string), name string) {
    fmt.Println("before greet")
    f(name)
    fmt.Println("after greet")
}

func main() {
    decorateGreet(greet, "John")
}

  Although this example a little ugly, but it really is the application of the decorator. Although experience tells us that java, Aspect Oriented Programming should be used in proxy mode (static or dynamic proxy agent), or even the use of reflection, ASM, but in fact the decorator more direct. Of course, in order to install about force, usually do not write so straightforward code above.

package main

import "fmt"

func greet(name string) {
    fmt.Printf("Hello %s!\n", name)
}

func decorateGreet(f func(string)) func(string) {
    return func(name string) {
        fmt.Println("before greet")
        f(name)
        fmt.Println("after greet")
    }
}

func main() {
    decorateGreet(greet)("装逼John")
}

  Called changed a bit, I do not know how many grades at elevated. This seems to make people believe: I really was a decorator. Obviously, decorator enhancements to play a role, from the code point of view, nothing more than to accept an object of type A (also a function of the object), and then return the object of type A, and in the return type of trigger implementation into the Senate, at the same time add some enhancements code.

  For the adapter, the use of which it wants to make a strong turn type, as the following example attempts to greet function adapted to the function hi.

package main

import "fmt"

type hi func()
type greet func(string)

func sayHi() {
    fmt.Printf("Hi")
}

func toGreet(h hi) greet {
    return func(name string) {
        h()
        fmt.Printf(", %s", name)
    }
}

func main() {
    toGreet(sayHi)("John")
}

  When the function is first-class citizens when everything seems to be so natural and smooth, and similar functions using java to achieve, it is necessary to raise the level of the class.

No personal welcome attention to the public

Do not go the same language

Guess you like

Origin www.cnblogs.com/laud/p/grammar_2.html