groovy之建造者

groovy 有很多建造者类,这些类都是基于建造者模式实现的。

输出一个html文档,入参为一个闭包。

package dsl

import groovy.xml.MarkupBuilder;

def page=new MarkupBuilder();
page.html{ 
	head{title "HellWorld"} 
	body{
		ul{ for(count in 1..10){
			 li "$count"
			}
		}
	} }

 输出:

<html>
  <head>
    <title>HellWorld</title>
  </head>
  <body>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
      <li>8</li>
      <li>9</li>
      <li>10</li>
    </ul>
  </body>
</html>

 

 

swing builder 让创建Swing界面更加简单

import java.awt.FlowLayout;

import groovy.swing.SwingBuilder;

def builder=new SwingBuilder();
def langs=["Groovy","Ruby","java","C","C++","C#"];
def gui=builder.frame(size:[400,400],title:'Swing Grovy'){
	panel(layout:new FlowLayout()){
		for(lang in langs){
			checkBox(text:lang)
		}
	}
}
gui.show();

 

你可能感兴趣的:(C++,c,swing,Ruby,groovy)