I\'ve implemented a Builder Pattern in a project I\'m working. I\'m using this d
ID: 652360 • Letter: I
Question
I've implemented a Builder Pattern in a project I'm working.
I'm using this design pattern to created Fields and Components (div, tables, panel (boostrap)).
So I have a Director Component class. It makes a order for a Builder (in this case InputComponentBuilder)
class @DirectorComponent
@builder = null
@make = (args...)->
@builder.make.apply(@builder, args)
@setResult(result = @builder.getResult())
return result
@makeInput = (name, properties)->
@builder = new InputComponentBuilder()
return @make.apply(@, arguments)
Source of InputComponent Builder
class @InputComponentBuilder extends Builder
constructor : ()->
@input = new InputComponent()
make : (name, properties)->
@input.extend(properties)
@input.setName(name)
obj = $("<input />")
obj.attr("name", name)
obj.val(@input.value)
if @input.placeholder
obj.attr("placeholder", @input.placeholder)
obj = BuilderHelper.applyProperties(@input, obj)
obj = BuilderFieldHelper.applyProperties(@input, obj)
@input.setObject(obj)
@setResult(@input)
return @input
The InputComponentBuilder is for generic input fields. So if I want to create specific field like (EmailField, ZipCodeField.. etc) I will need to create another builder extended a InputComponetBuilder? or in class Director I will need to create another method makeZipCodeField and change the recipe sent to InputBuilder?
Base Class (Base class of Class Builder's result)
class @Component
constructor : ()->
@id = null
@name = ""
@obj = null
@attrs = {}
@classes = []
@css = {}
extend : (data)->
$.extend(@, data)
getObject : ()->
return @obj
setObject : (obj)->
@obj = obj
setName : (name)->
@name = name
getName : ()->
return @name
getPropertiesFromObjectComponent : (args...)->
result = []
console.log(args, arguments)
for key, arg of args
console.log key, arg
result.push(if arg of @ then @[arg] else null)
console.log(result)
return result
Abstract Class for Fields
class @FieldComponentAbstract extends Component
constructor : ()->
super
@label = null
@placeholder = null
@onclick = null
@onchange = null
@onfocus = null
@onkeypressed = null
@onkeyup = null
@onkeydown = null
@oninsertednode = null
@bootstrap = null
@onblur = null
@mask = null
@validate : ()->
return true
InputComponent Class
class @InputComponent extends FieldComponentAbstract
constructor : ()->
super
@type = "text"
@value = ""
Calling makeInput
@cpf_field = DirectorComponent.makeInput('cpf',
placeholder: "CPF",
mask : '999.999.999-99'
bootstrap: true
)
Explanation / Answer
To answer your original question (in title), a Builder is by definition a very stateful object, so after issuing the final "build" and obtaining the constructed object, the Builder is discarded - never re-used.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.