diff --git a/tech/algorithms.wiki b/tech/algorithms.wiki index 5f52419..50c6f6a 100644 --- a/tech/algorithms.wiki +++ b/tech/algorithms.wiki @@ -64,12 +64,14 @@ Different ways to store and operate on data, with differing efficiency * [[singleton]] * [[prototype]] +* [[builder]] === Structural === +* [[composite]] + === Behavioral === -* builder * factory * facade * proxy diff --git a/tech/builder.wiki b/tech/builder.wiki new file mode 100644 index 0000000..bc0c106 --- /dev/null +++ b/tech/builder.wiki @@ -0,0 +1,25 @@ += Builder = + +This is a design pattern where you create a complex object step by step. The +pattern allows you to produce different types and representations of an object +using the same construction code. + +This is often used when you have an object with a large number of constructor +paramaters due to the complexity of said object. Or, when you have a large +number of subclasses for specific variations on the object. + +What we can do therefore is create a *builder* class who has several functions +that can be called to modify some object to be created, then return the object +once it is in a state to be returned. + +== Implementation == + +For different features of the object, have a function to add that feature or +flag to the object, such as `addGPS`, `addSeats`, and `addWindows` for a car. +Have each return `this` so that they may be chained together. + +== Use case == + +* when code needs to create different representations of the same product, such + as a sports car, truck, and sedan + diff --git a/tech/composite.wiki b/tech/composite.wiki new file mode 100644 index 0000000..6346103 --- /dev/null +++ b/tech/composite.wiki @@ -0,0 +1 @@ + = Composite = diff --git a/tech/prototype.wiki b/tech/prototype.wiki index 1740c42..eeffc54 100644 --- a/tech/prototype.wiki +++ b/tech/prototype.wiki @@ -5,4 +5,22 @@ code dependent on their classes. The general idea is sometimes we need an exact clone of an object. We cant copy the object exactly from the outside, as we dont know the private fields. -Therefore, the object can implement a `clone` method +Therefore, the object can implement a `clone` method to return an exact copy of +the object. Such an object that implements `clone` is a *prototype* + +== Prototype registry == + +Some objects are commonly copied, and therefore we can cache them in a +[[hash_table]] for O(1) lookup, then just call `clone` on the object and return +the new object. + +== Implementation == + +Often it only needs to be a method, `clone()` which returns the same type as +`this`, and just calls `return myObject(this)`. + +== Use case == + +* when code shouldnt depend on the concrete classes of objects that you need to + copy +* Reduce number of subclasses that differ in how their init their subobjects