Skip to content

Instantly share code, notes, and snippets.

@feynon
Created August 24, 2019 20:03
Show Gist options
  • Save feynon/85271e3071c59cd00ddcc13cdd5da706 to your computer and use it in GitHub Desktop.
Save feynon/85271e3071c59cd00ddcc13cdd5da706 to your computer and use it in GitHub Desktop.
A client should never be forced to implement an interface that it doesn’t use or clients shouldn’t be forced to depend on methods they do not use.
const manageShapeInterface = fn => ({
type: "manageShapeInterface",
calculate: () => fn()
});
const circle = radius => {
const proto = {
radius,
type: "Circle",
area: args => Math.PI * Math.pow(args.radius, 2)
};
const basics = shapeInterface(proto);
const abstraccion = manageShapeInterface(() => basics.area());
const composite = Object.assign({}, basics, abstraccion);
return Object.assign(Object.create(composite), { radius });
};
const cubo = length => {
const proto = {
length,
type: "Cubo",
area: args => Math.pow(args.length, 2),
volume: args => Math.pow(args.length, 3)
};
const basics = shapeInterface(proto);
const complex = solidShapeInterface(proto);
const abstraccion = manageShapeInterface(
() => basics.area() + complex.volume()
);
const composite = Object.assign({}, basics, abstraccion);
return Object.assign(Object.create(composite), { length });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment