> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/advanced/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/advanced/3.2.-the-strategy-pattern-ok.md).

# 3.2. The Strategy Pattern (ok)

![](/files/-MTC4oB32uGaZBOQL3oO)

C:\xampp\htdocs\php\js\strategy\init.js

```
define(function(require) {
  'use strict';
  return {
    init: function() {
      var Strategy = require('strategy/strategy'), telValidator = require('strategy/telValidator'), emailValidator = require('strategy/emailValidator'), validator;
      validator = new Strategy();
      console.log(validator.selectValidator(telValidator).validate(12345678911));
      console.log(validator.selectValidator(emailValidator).validate('phamngoctuong@gmail.com'));
    }
  };
});
```

C:\xampp\htdocs\php\js\strategy\strategy.js

```
define(function() {
  'use strict';
  var Validator = function() {};
  Validator.prototype.selectValidator = function(validator) {
    this.validator = validator;
    return this;
  };
  Validator.prototype.validate = function(value) {
    if (this.validator) {
      return this.validator.validate(value);
    }
    throw ('No validator selected');
  };
  return Validator;
});
```

C:\xampp\htdocs\php\js\strategy\emailValidator.js

```
define(function() {
  'use strict';
  return {
    validate: function(value) {
      return value.indexOf('@') !== -1;
    }
  };
});
```

C:\xampp\htdocs\php\js\strategy\telValidator.js

```
define(function() {
  'use strict';
  return {
    validate: function(value) {
      return (/^[0-9]{11}$/g).test(value);
    }
  }
});
```
