Skip to content

Instantly share code, notes, and snippets.

@matt-forster
Created January 26, 2016 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-forster/5865a249e1ac1e2d23f3 to your computer and use it in GitHub Desktop.
Save matt-forster/5865a249e1ac1e2d23f3 to your computer and use it in GitHub Desktop.
Framework implementation of the Strategy Design pattern.
import fs from 'fs';
/*
Maintains three different strategies to mix and
match solutions for different and changing manufacturer
requirements
*/
// Abstract Parsing Strategy
class AbstractParse {
parse() {
throw new Error('Not Implemented');
}
}
// Abstract Proccessing Strategy
class AbstractProcess {
process() {
throw new Error('Not Implemented');
}
}
// Abstract Serializing Strategy
class AbstractSerialize {
serialize() {
throw new Error('Not Implemented');
}
}
// Example Implemented Parser
class PDFTextParse extends AbstractParse {
parse() {
// PDF Logic
console.log('Parsing PDF to Text');
}
}
// Example implemented processor
class GMProcess extends AbstractProcess {
process() {
// Process Text - State could belong here until finished
console.log('Processing GM text');
}
}
// Example implemented serializer
class CSVSerial extends AbstractSerialize {
serialize() {
// Serialize text into CSV format
console.log('Serializing data to CSV');
}
}
// The entry class
class Scrape {
constructor(Parse, Process, Serialize) {
this.parser = new Parse();
this.processor = new Process();
this.serializor = new Serialize();
}
run(inputFile) {
// Could pipe if methods returned a common instance
let text = this.parser.parse(inputFile);
let processed = this.processor.process(text);
return this.serializor.serialize(processed);
}
}
// Entry Code - Wrapped in a module, or CLI interface
let GMScrape = new Scrape(PDFTextParse, GMProcess, CSVSerial);
let file = fs.readFileSynv('./example.pdf');
GMScrape.run(file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment