@jonathanfmills
Jonathan Mills
SOLID JavaScript
What’s the Problem?
What is SOLID?
Five design principles intended to make software designs
more understandable, flexible and maintainable.
SOLID
Single responsibility principle
Open/closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
“SOLID principles are a means of
forcing us to stop and think about the
‘right’ way to do something.”
-Me
Disclaimer!
Single Responsibility
Every module or class should have responsibility over a single
part of the functionality provided by the software, and that
responsibility should be entirely encapsulated by the class.
All its services should be narrowly aligned with that
responsibility.
Single Responsibility Principle
“A class should have a single reason
to change”
Robert C Martin
Open Closed Principle
Open / Closed Principle
Software entities (classes, modules, functions, etc.) should be
open for extension, but closed for modification
We can do this in JavaScript?
Inheritance
var Task = function (name) {
this.name = name;
this.completed = false;
}
Task.prototype.complete = function () {
console.log('completing task: ' + this.name);
this.completed = true;
};
Task.prototype.save = function () {
console.log('saving Task: ' + this.name);
};
var myTask = new Task('Legacy Task');
myTask.complete();
myTask.save();
I need this urgently!!!
var urgentTask = new Task('Urgent Task');
urgentTask.priority = 2;
urgentTask.notify = function(){
console.log('notifying important people');
};
urgentTask.complete();
urgentTask.save = function(){
this.notify();
Task.prototype.save.call(this)
};
urgentTask.save();
That’s only half way…
var UrgentTask = function (name, priority) {
Task.call(this, name);
this.priority = priority;
};
UrgentTask.prototype = Object.create(Task.prototype);
UrgentTask.prototype.notify = function () {
console.log('notifying important people');
};
UrgentTask.prototype.save = function () {
this.notify();
console.log('do special stuff before saving');
Task.prototype.save.call(this)
};
var ut = new UrgentTask('This is urgent', 1);
ut.complete();
ut.save();
Open / Closed Principle
Software entities (classes, modules, functions, etc.) should be
open for extension, but closed for modification
Liskov Substitution
Who is Liskov?
What is she trying to
substitute?
Barbara Liskov
American computer scientist who is an Institute Professor at the Massachusetts
Institute of Technology and Ford Professor of Engineering in its School of
Engineering's electrical engineering and computer science department.
She was one of the first women to be granted a doctorate in computer science in
the United States and is a Turing Award winner who developed the Liskov
substitution principle.
Let q(x) be a property provable about objects of x of
type T. Then q(y) should be provable for objects y of
type S where S is a subtype of T.
Liskov Substitution
Every subclass/derived class should be substitutable for
their base/parent class.
Liskov Substitution
What makes a rectangle?
What makes a square?
What happens when I
substitute a rectangle for a
square?
var Task = function (name) {
this.name = name;
this.completed = false;
}
var myTask = new Task('Legacy Task'); //Can I substitute
//Urgent task here?
myTask.complete();
myTask.save();
Interface Segregation
Interface Segregation
No client should be forced to depend on methods it does not
use. ISP splits interfaces that are very large into smaller and
more specific ones so that clients will only have to know
about the methods that are of interest to them.
“Keep your interfaces simple”
The ISP was first used and formulated by Robert C.
Martin when doing some consulting for Xerox.
ISP
Xerox had created a new printer system that could perform a
variety of tasks like stapling a set of printed papers and
faxing. The software for this system was created from the
ground up and performed its tasks successfully.
ISP
As the software grew, making modification became more
and more difficult so that even the smallest change would
take a redeployment cycle to an hour. This was making it
near impossible to continue development.
ISP
The design problem was that one main Job class was used by
almost all of the tasks. Anytime a print job or a stapling job
had to be done, a call was made to some method in the Job
class. This resulted in a huge or 'fat' class with multitudes of
methods specific to a variety of different clients.
ISP
Because of this design, a staple job would know about all
the methods of the print job, even though there was no
use for them.
ISP
How do we solve this?
The solution suggested by Martin is what is called the
Interface Segregation Principle today.
Solution
Applied to the Xerox software, a layer of interfaces
between the Job class and all of its clients was added
using the Dependency Inversion Principle.
Solution
Instead of having one large Job class, a Staple Job
interface or a Print Job interface was created that would
be used by the Staple or Print classes, respectively,
calling methods of the Job class.
Solution
Therefore, one interface was created for each job, which
were all implemented by the Job class.
Solution
A facade is an object that provides a simplified interface
to a larger body of code
Façade Pattern
Dependency Inversion Principle
Dependency Inversion
A. High-level modules should not depend on low-level modules.
Both should depend on abstractions.
B. Abstractions should not depend on details. Details should
depend on abstractions.
Dependency Inversion
A. High-level modules should not depend on low-level modules.
Both should depend on abstractions.
B. Abstractions should not depend on details. Details should
depend on abstractions.
So now what?
Solid js

Solid js

  • 1.
  • 2.
  • 5.
  • 6.
    Five design principlesintended to make software designs more understandable, flexible and maintainable. SOLID
  • 7.
    Single responsibility principle Open/closedprinciple Liskov substitution principle Interface segregation principle Dependency inversion principle
  • 8.
    “SOLID principles area means of forcing us to stop and think about the ‘right’ way to do something.” -Me
  • 9.
  • 10.
  • 12.
    Every module orclass should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility. Single Responsibility Principle
  • 13.
    “A class shouldhave a single reason to change” Robert C Martin
  • 14.
  • 16.
    Open / ClosedPrinciple Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification
  • 17.
    We can dothis in JavaScript? Inheritance
  • 18.
    var Task =function (name) { this.name = name; this.completed = false; }
  • 19.
    Task.prototype.complete = function() { console.log('completing task: ' + this.name); this.completed = true; }; Task.prototype.save = function () { console.log('saving Task: ' + this.name); };
  • 20.
    var myTask =new Task('Legacy Task'); myTask.complete(); myTask.save();
  • 21.
    I need thisurgently!!!
  • 22.
    var urgentTask =new Task('Urgent Task'); urgentTask.priority = 2; urgentTask.notify = function(){ console.log('notifying important people'); }; urgentTask.complete();
  • 23.
  • 24.
  • 25.
    var UrgentTask =function (name, priority) { Task.call(this, name); this.priority = priority; }; UrgentTask.prototype = Object.create(Task.prototype);
  • 26.
    UrgentTask.prototype.notify = function() { console.log('notifying important people'); }; UrgentTask.prototype.save = function () { this.notify(); console.log('do special stuff before saving'); Task.prototype.save.call(this) };
  • 27.
    var ut =new UrgentTask('This is urgent', 1); ut.complete(); ut.save();
  • 28.
    Open / ClosedPrinciple Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification
  • 29.
  • 30.
    Who is Liskov? Whatis she trying to substitute?
  • 31.
    Barbara Liskov American computerscientist who is an Institute Professor at the Massachusetts Institute of Technology and Ford Professor of Engineering in its School of Engineering's electrical engineering and computer science department. She was one of the first women to be granted a doctorate in computer science in the United States and is a Turing Award winner who developed the Liskov substitution principle.
  • 32.
    Let q(x) bea property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T. Liskov Substitution
  • 35.
    Every subclass/derived classshould be substitutable for their base/parent class. Liskov Substitution
  • 38.
    What makes arectangle?
  • 39.
    What makes asquare?
  • 40.
    What happens whenI substitute a rectangle for a square?
  • 41.
    var Task =function (name) { this.name = name; this.completed = false; }
  • 42.
    var myTask =new Task('Legacy Task'); //Can I substitute //Urgent task here? myTask.complete(); myTask.save();
  • 43.
  • 45.
    Interface Segregation No clientshould be forced to depend on methods it does not use. ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them.
  • 46.
  • 47.
    The ISP wasfirst used and formulated by Robert C. Martin when doing some consulting for Xerox. ISP
  • 48.
    Xerox had createda new printer system that could perform a variety of tasks like stapling a set of printed papers and faxing. The software for this system was created from the ground up and performed its tasks successfully. ISP
  • 49.
    As the softwaregrew, making modification became more and more difficult so that even the smallest change would take a redeployment cycle to an hour. This was making it near impossible to continue development. ISP
  • 50.
    The design problemwas that one main Job class was used by almost all of the tasks. Anytime a print job or a stapling job had to be done, a call was made to some method in the Job class. This resulted in a huge or 'fat' class with multitudes of methods specific to a variety of different clients. ISP
  • 51.
    Because of thisdesign, a staple job would know about all the methods of the print job, even though there was no use for them. ISP
  • 52.
    How do wesolve this?
  • 53.
    The solution suggestedby Martin is what is called the Interface Segregation Principle today. Solution
  • 54.
    Applied to theXerox software, a layer of interfaces between the Job class and all of its clients was added using the Dependency Inversion Principle. Solution
  • 55.
    Instead of havingone large Job class, a Staple Job interface or a Print Job interface was created that would be used by the Staple or Print classes, respectively, calling methods of the Job class. Solution
  • 56.
    Therefore, one interfacewas created for each job, which were all implemented by the Job class. Solution
  • 57.
    A facade isan object that provides a simplified interface to a larger body of code Façade Pattern
  • 58.
  • 60.
    Dependency Inversion A. High-levelmodules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend on details. Details should depend on abstractions.
  • 61.
    Dependency Inversion A. High-levelmodules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend on details. Details should depend on abstractions.
  • 62.