Quick Index
Many Options for Object Construction


This is based on lessons learned.

Requirement: Three Objects
Let's say our design needs to include the three objects shown.

NOTE WELL -- these are required objects, not classes (keep that in mind as we move ahead)
Construction Option #1
As coders, the highlighted construction may pop into our head as how we'll construct the objects.

Suggestion: Do not worry about construction, i.e., do not be limited by it -- this will allow creativity during design.

Remember that construction is dynamic.

The construction shown here is only one option of many.
let turtle1 = new CircularTurtle()
let turtle2 = new RectangularTurtle()
let turtle3 = new EllipticalTurtle()
Construction Option #2
Another option.
//Assume turtle is dynamic (probably through component/ivar, e.g., "shape")
let turtle1 = new Turtle();
turtle1.setAsCircular();
let turtle2 = new Turtle();
turtle2.setAsRectangular();
let turtle3 = new Turtle();
turtle3.setAsElliptical();
"
Construction Option #3
Another option -- a more user friendly version of the previous.
//Assume turtle is dynamic (probably through component/ivar, e.g., "shape")
let turtle1 = Turtle.newCircular();
let turtle1 = Turtle.newRectangular();
let turtle1 = Turtle.newElliptical();
Algorithms for Construction Option #2
Potential algorithms to support Construction Option #2
setAsCircular() {
	this.setShape(new Circle());
}
setAsRectangular() {
	this.setShape(new Rectangle());
}
setAsElliptical() {
	this.setShape(new Ellipse());
}
Algorithms for Construction Option #3
Potential algorithms to support Construction Option #3
static newCircular() {
	let turtle = new Turtle();
	turtle.setShape(new Circular());
	return turtle;
}

//Similar for other two


Conclusion 💡


The examples on this page are only to demonstrate that construction is dynamic.

This is the key take-away:



Navigation