// class Customer { int inn String name String address String phone } // class Customers { Customer findByInn(inn) void add(Customer customer) } // class Product { String article String name double price } // class Products { Product findByArticle(article) void add(Product product) } // class Order { int num Customer customer List<OrderDetail> details = [] OrderDetail findByPos(pos) void add(OrderDetail detail) } // class OrderDetail { int pos Product product def count = 1 def getSum() { count * product.price } } // class Orders { Order findByNum(num) void add(Order order) }
// - def customers = new Customers() def products = new Products() def orders = new Orders() // customers.add(new Customer(inn: 1234, name: "", address: "", phone: "+74951002030")) // products.add(new Product(article: "a100", name: " 1", price: 100.00)) products.add(new Product(article: "a200", name: " 2", price: 200.00)) // def order = new Order(num: 1, customer: customers.findByInn(1234)) order.add(new OrderDetail(pos: 1, product: products.findByArticle("a100"), count: 1)) order.add(new OrderDetail(pos: 2, product: products.findByArticle("a200"), count: 1)) orders.add(order)
AddCustomer(inn: 1234, name: "", address: "", phone: "+74951002030") AddProduct(article: "a100", name: " 1", price: 100.00) AddProduct(article: "a200", name: " 2", price: 200.00) AddOrder(num: 1, customer: 1234) { Detail(pos: 1, product: "a100", count: 1) Detail(pos: 2, product: "a200", count: 1) }
public class MyBuilder extends BuilderSupport { public Customers customers public Products products public Orders orders // protected void setParent(Object parent, Object child) { } // protected Object createNode(Object name) { if (name != "call") throw new Exception("Node required parameters") new Node(null, name); } // protected Object createNode(Object name, Object value) { throw new Exception("Node required parameters") } // protected Object createNode(Object name, Map attributes) { // Node parent = getCurrent() def result // switch (name) { case "AddCustomer": result = addCustomer(attributes) break case "AddProduct": result = addProduct(attributes) break case "AddOrder": result = addOrder(attributes) break case "Detail": if (parent == null || parent.name() != "AddOrder") throw new Exception( "Detail must be specified with only AddOrder") result = addOrderDetail(parent.value(), attributes) break defailt: throw new Exception("Unknown node ${name}") } new Node(null, name, attributes, result); } // protected Object createNode(Object name, Map attributes, Object value) { throw new Exception("Node ${name} can not support objects") } // def addCustomer(Map params) { def customer = new Customer(inn: params.inn, name: params.name, address: params.address, phone: params.phone) customers.add(customer) println "Added customer ${customer.inn}: ${customer.name}" customer } // def addProduct(Map params) { def product = new Product(article: params.article, name: params.name, price: params.price) products.add(product) println "Added product ${product.article}: ${product.name}" product } // def addOrder(Map params) { def order = new Order(num: 1, customer: customers.findByInn(params.customer)) orders.add(order) println "Added order ${order.num} from customer ${order.customer.name}" order } // def addOrderDetail(Order order, Map params) { def count = params.count?:1 def detail = new OrderDetail(pos: params.pos, product: products.findByArticle(params.product), count: count) order.add(detail) println "Added into order ${order.num} detail pos ${detail.pos} " + "with product ${detail.product.name}" detail } }
// - def customers = new Customers() def products = new Products() def orders = new Orders() // def myApi = new MyBuilder(customers: customers, products: products, orders: orders) // myApi { AddCustomer(inn: 1234, name: "", address: "", phone: "+74951002030") AddProduct(article: "a100", name: " 1", price: 100.00) AddProduct(article: "a200", name: " 2", price: 200.00) AddOrder(num: 1, customer: 1234) { Detail(pos: 1, product: "a100", count: 1) Detail(pos: 2, product: "a200", count: 1) } } // println "\n*** Result ***" println "Customers:" println customers println "Products:" println products println "Orders:" println orders
Added customer 1234: Customer
Added product a100: Item 1
Added product a200: Item 2
Added order 1 from customer Customer
Added into order 1 detail pos 1 with product Item 1
Added into order 1 detail pos 2 with product Item 2
*** Result ***
Customers:
{inn = 1234, name = Client, address = Russia, phone = + 74951002030}
Products:
{article = a100, name = Item 1, price = 100.0}
{article = a200, name = Item 2, price = 200.0}
Orders:
{num = 1, customer = Customer,
detail = {pos = 1, product = Item 1, count = 1, sum = 100.0};
{pos = 2, product = Item 2, count = 1, sum = 200.0}}
Source: https://habr.com/ru/post/174683/
All Articles