It is necessary to use a random number generator in the form of an object type type, but the burden of its dependence is not necessary

Currently, I have an immutable type with a name Genethat has only 2 fields:

double value;
Interval intervalOfAllowedValues;

I sometimes need to Generandomly switch its value to some other value while it is still in the range defined in intervalOfAllowedValues.

I made a special method for this

public Gene RandomMutation() { ... }
//it returns a Gene because this class is immutable!

who will take care of this situation using INumberGenerator.GenerateDouble(...). The problem is that it either RandomMutation()takes an argument IRandomNumberGenerator, or Genemust take one by injecting a constructor.

None of the solutions are to your liking:

RandomMutation() , , Gene INumberGenenerator, , .

, , a INumberGenerator Gene, , , . , Gene . , 2 Gene, . , ?

: RandomMutation() Gene. , , Gene, Gene, Interval, . , , , .

4- (!) : (Singleton). , .

?

+3
4

, , INumberGenerator, Gene. , Gene. , .

struct . RandomMutation INumberGenerator, , , new Gene() . struct.

. Gene class, INumberGenerator . - .

- , Gene struct . , (. LINQ), OO, . , .

, , Gene, INumberGenerator, . , Gene, , INumberGenerator , :

public static Gene Mutate(this INumberGenerator generator, Gene gene)
{
    // ...
}

INumberGenerator Gene , , .

+1

, - , , , - , - . , , , . - , . , RandomMutation - , .

, , stubbing.

+3

, GeneMutator, mutate, Gene Gene ( INumberGenerator).

Interval , Gene.

+3

, . ( ) , .

, Gene ( , , ). , , " , , ". , . , .

+2

Source: https://habr.com/ru/post/1776149/


All Articles