// with constructor Programmer programmer = new Programmer("first name", "last name", "address Street 39", "ZIP code", "City", "Country", birthDateObject, new String[] {"Java", "PHP", "Perl", "SQL"}, new String[] {"CRM system", "CMS system for government"}); // or with setters Programmer programmer = new Programmer(); programmer.setName("first name"); programmer.setLastName("last name"); // ... multiple lines after programmer.setProjects(new String[] {"CRM system", "CMS system for government"});
publicclassBeanDefinitionBuilder{ /** * The {@code BeanDefinition} instance we are creating. */ private AbstractBeanDefinition beanDefinition; // ... some not important methods for this article // Some of building methods /** * Set the name of the parent definition of this bean definition. */ public BeanDefinitionBuilder setParentName(String parentName){ this.beanDefinition.setParentName(parentName); returnthis; } /** * Set the name of the factory method to use for this definition. */ public BeanDefinitionBuilder setFactoryMethod(String factoryMethod){ this.beanDefinition.setFactoryMethodName(factoryMethod); returnthis; } /** * Add an indexed constructor arg value. The current index is tracked internally * and all additions are at the present point. * @deprecated since Spring 2.5, in favor of {@link #addConstructorArgValue} */ @Deprecated public BeanDefinitionBuilder addConstructorArg(Object value){ return addConstructorArgValue(value); } /** * Add an indexed constructor arg value. The current index is tracked internally * and all additions are at the present point. */ public BeanDefinitionBuilder addConstructorArgValue(Object value){ this.beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue( this.constructorArgIndex++, value); returnthis; } /** * Add a reference to a named bean as a constructor arg. * @see #addConstructorArgValue(Object) */ public BeanDefinitionBuilder addConstructorArgReference(String beanName){ this.beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue( this.constructorArgIndex++, new RuntimeBeanReference(beanName)); returnthis; } /** * Add the supplied property value under the given name. */ public BeanDefinitionBuilder addPropertyValue(String name, Object value){ this.beanDefinition.getPropertyValues().add(name, value); returnthis; } /** * Add a reference to the specified bean name under the property specified. * @param name the name of the property to add the reference to * @param beanName the name of the bean being referenced */ public BeanDefinitionBuilder addPropertyReference(String name, String beanName){ this.beanDefinition.getPropertyValues().add(name, new RuntimeBeanReference(beanName)); returnthis; } /** * Set the init method for this definition. */ public BeanDefinitionBuilder setInitMethodName(String methodName){ this.beanDefinition.setInitMethodName(methodName); returnthis; } // Methods that can be used to construct BeanDefinition /** * Return the current BeanDefinition object in its raw (unvalidated) form. * @see #getBeanDefinition() */ public AbstractBeanDefinition getRawBeanDefinition(){ returnthis.beanDefinition; } /** * Validate and return the created BeanDefinition object. */ public AbstractBeanDefinition getBeanDefinition(){ this.beanDefinition.validate(); returnthis.beanDefinition; } }
publicclassFactoryMethodTest{ @Test publicvoidtest(){ Meal fruit = Meal.valueOf("banana"); Meal vegetable = Meal.valueOf("carrot"); assertTrue("Banana should be a fruit but is "+fruit.getType(), fruit.getType().equals("fruit")); assertTrue("Carrot should be a vegetable but is "+vegetable.getType(), vegetable.getType().equals("vegetable")); } } classMeal{ private String type; publicMeal(String type){ this.type = type; } public String getType(){ returnthis.type; } // Example of factory method - different object is created depending on current context publicstatic Meal valueOf(String ingredient){ if (ingredient.equals("banana")) { returnnew Meal("fruit"); } returnnew Meal("vegetable"); } }