0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>Using subtype polymorphism doesn't remove conditional constructs, except in some dispatching cases, such as by key or filename.</p>
1
<p>Using subtype polymorphism doesn't remove conditional constructs, except in some dispatching cases, such as by key or filename.</p>
2
<p>More often, the conditional construct is left alone only. When we select a suitable implementation, we use it within the polymorphic function directly, without conditions.</p>
2
<p>More often, the conditional construct is left alone only. When we select a suitable implementation, we use it within the polymorphic function directly, without conditions.</p>
3
<p>In the previous lesson, we looked at an example function that selects the desired way of implementing the strategy based on the user's age and returns it:</p>
3
<p>In the previous lesson, we looked at an example function that selects the desired way of implementing the strategy based on the user's age and returns it:</p>
4
<p>Here we see a function that selects the desired class, creates the object, and then returns it. Programmers call it a<strong>factory method</strong>. We can implement a factory in any way, including all the methods we've learned in this course.</p>
4
<p>Here we see a function that selects the desired class, creates the object, and then returns it. Programmers call it a<strong>factory method</strong>. We can implement a factory in any way, including all the methods we've learned in this course.</p>
5
<p>Generally, we can call a factory anything that creates an object. It doesn't have to use different classes - there could be only one class. But the creation process involves some preliminary calculations. In real projects, factories can be<a>quite large</a>.</p>
5
<p>Generally, we can call a factory anything that creates an object. It doesn't have to use different classes - there could be only one class. But the creation process involves some preliminary calculations. In real projects, factories can be<a>quite large</a>.</p>
6
<p>We often implement factories as classes with a static method called factory.</p>
6
<p>We often implement factories as classes with a static method called factory.</p>
7
<p>Factories themselves don't make objects because it's not an abstraction of data, and it makes no sense to substitute them. Otherwise, you'll get substitutes for substitutes:</p>
7
<p>Factories themselves don't make objects because it's not an abstraction of data, and it makes no sense to substitute them. Otherwise, you'll get substitutes for substitutes:</p>
8
<h2>Class dispatch</h2>
8
<h2>Class dispatch</h2>
9
<p>JavaScript allows you to create objects using a class reference:</p>
9
<p>JavaScript allows you to create objects using a class reference:</p>
10
<p>This syntax gives you great scope for dispatching. For example, in some cases, it'll be possible to get away from the conditional structures completely:</p>
10
<p>This syntax gives you great scope for dispatching. For example, in some cases, it'll be possible to get away from the conditional structures completely:</p>
11
11