tailieunhanh - Programming Groovy dynamic productivity for the java developer phần 10

Tuy nhiên, cách tiếp cận trọng không thể được sử dụng nếu các lớp học đang được thử nghiệm cuối cùng. Cách tiếp cận danh mục chiếu sáng trong trường hợp này. Bạn không có để tạo ra một lớp học riêng biệt như trong hai cách tiếp cận bạn đã nhìn thấy cho đến nay. Thay vào đó, | Types of DSLs 280 The previous code indicates that the alarm mock should return true on the first call and throw an exception on the second. You can find another good example of a DSL in Grails GORM. For example you can specify data constraints on an object s properties using the following syntax class State String twoLetterCode static constraints twoLetterCode unique true blank false size Grails smartly recognizes this fluent and expressive syntax for expressing the constraints and generates the validation logic both for the front end and for the back end. Groovy builders see Chapter 17 Groovy Builders on page 260 are good examples of DSLs. They re fluent and built on context. Types of DSLs When designing a DSL you have to decide between two types external and internal. An external DSL defines a new language. You have the flexibility to choose the syntax. You then parse the commands in your new language to take actions. When I took my first job the company asked me to maintain a DSL that needed extensive use of lex and The parsing was a lot of fun. You can use languages such as C and Java that do heavyweight lifting for you with the support of extensive parsing capabilities and libraries. For example you can use ANTLR to build dSls Par07 . An internal DSL also called an embedded DSL defines a new language as well but within the syntactical confines of another languages. You don t use any parsers but you have to construe the syntax by tactfully mapping to constructs such as methods and properties in the underlying language. The users of your internal DSL might not realize they re 4. I first thought they asked me to do it because I was good. I later understood they don t ask a new employee to do stuff because they re good but because no one else wants to do it Designing Internal DSLs 281 using syntax of a broader language. However creating the internal DSL takes significant design effort and clever tricks to make the underlying language work for you.

TỪ KHÓA LIÊN QUAN