Please note that some other answers may possibly describe factories, but do not describe the GOF Factory pattern.
Now I want to replace this line with a Factory Sample, although I'm not sure because my TestMode constructor requires an additional object, and I'm not sure where I will need to pass this value.
Well, you could think of it this way: MainMode, not TestMode, is the one that does the special thing. Particularly what he does is to ignore the given number to ensure it is truly random. In this way of thinking about it, MainMode does something extra.
Or, if this is different from randomness, MainMode and TestMode are not different from each other, then you might think that you can take this similarity into account in one class, which is provided with one of two strategies for calculating random numbers. One Strategy would actually be random, and one would be perverse, with a random range of only 1 value.
But suppose there are other differences between MainMode and TestMode - presumably TestMode outputs additional debugging to System.out or something like that.
We can still rule out “how can we put an accident” because we are testing or playing a game for real. ”These are orthogonal problems.
So now we know that in addition to what the “Mode” is still doing, it must adopt the Strategy of chance. Then we could, for example, when you were told that the standard random platform is not random enough, you can replace it with a better random one.
Or you can conduct a test where the range of randomness is limited to only two options or always alternates from one to zero or returns the next value on each call in some Vecrtor or Iterator.
Thus, we use the GOF strategy template to build random strategies:
interface RandomStrategy { public double random(); } public class NotSoRandom implements RandomStrategy { private double r; public NotSoRandom( final double r ) { this.r = r; } public double random() { return r; } } public class PlatformRandom implements RandomStrategy { public double random() { return Math.random(); } }
Now, if your application only ever creates one “mode”, there is no need for a factory; you use Factory when you need to create the same class type again; Factory is actually just a strategy for creating an appropriate type of (sub) class.
In production code, I used factories where I have some kind of general class that creates material, and I need to say how to create the right subclass to create; For this, I pass Factory.
Now we create a Factory template for the mode; it will be surprisingly similar to the Strategy template:
abstract class Mode() { private RandomStrategy r; public Mode( final RandomStrategy r ) { this.r = r; }
So, now you know about the Factory template and strategy template and how they are similar in “form”, but differ in how they are used: Factory Template - Object Creational and returns the object that should be used; The strategy is Object Behavioral, and the instance is usually created explicitly, and the link is placed in the instance to encapsulate the algorithm. But in terms of structure, they are very similar.
Edit: OP asks in a comment: "How do I integrate this into my GUI?"
Well, none of this belongs to the graphical interface of your program, except, possibly, “Mode”. You will create a ConcreteStrategy and transfer it to your preferred Factory in some installation procedure, possibly determining which one to use based on command line arguments or configuration files. basically, you would choose the right Factory very much by choosing the right class in your original post. Again, if you only ever create something, you don't need a factory; factories for mass production (or the creation of families of related specific types, although this is beyond the scope of this issue).
(Suppose we have a game in which the user can choose on the command line whether to fight robots or dragons, then we want to create an instance of OpponentFactory that creates opponents (interface), with derived classes RobotOpponent and DragonOpponent and pass that Factory to the part of the game that spawns NewOpponent (). Likewise, the user can select brave or cowardly opponents that we would install as Strategy. We do not need to create more copies of Strategy, as Strategy is usually idempotent (stateless and single ).)
static int main( String[] args ) {