I'm trying to implement the Builder
design pattern in a Jenkins pipeline shared library. Say I have the following file structure and pseudo-code (to only illustrate the concept I'm after)
src/com/company/builders/
/Builder.groovy
/WidgetXBuilder.groovy
/WidgetYBuilder.groovy
Builder.groovy
has
package com.company.builders
abstract class AbstractBuilder {
protected String part0 = null
protected String part1 = null
protected String widget = null
}
// Implement in Widget builder classes
def buildWidget(final String p0, final String p1) {
}
// Used by any Widget concrete class
def getWidget() {
return widget
}
WidgetXBuilder.groovy
has
package com.company.builders
class WidgetXBuilder extends AbstractBuilder {
}
// Overload buildWidget()
def buildWidget(final String p0, final String p1) {
widget = p0 + p1
}
Similarly, WidgetYBuilder.groovy
has
package com.company.builders
class WidgetYBuilder extends AbstractBuilder {
}
// Overload buildWidget()
def buildWidget(final String p0, final String p1) {
widget = p0 - p1
}
Now I simply want a client to do this. Note my-shared-library
is a shared library that I set up in my Jenkins system configuration that has the Git repo of above file structure.
@Library('my-shared-library') _
import com.company.builders.Builder
import com.company.builders.WidgetXBuilder
import com.company.builders.WidgetYBuilder
// Build WidgetX
Builder widgetx = new WidgetXBuilder()
widgetx.buildWidget("part0", "part1") // Build a WidgetX widget
widgetx.getWidget() // get the WidgetX widget
// Build WidgetY
Builder widgety = new WidgetYBuilder()
widgety.buildWidget("part0", "part1") // Build a WidgetY widget
widgety.getWidget() // get the WidgetY widget
Can I achieve this with shared libraries? That is, implement the concept of encapsulation, inheritance, and polymorphism using a shared library? How? TIA