Skip to content

Commit

Permalink
feat(ch14): add object Aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
poulzinho committed Feb 18, 2020
1 parent 3932636 commit f470ab4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/ch-14_object_composition/14.1_object-composition.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {assert, expect} from 'chai';
import {expect} from 'chai';

describe("Object Composition", () => {
const objs = [
{a: "a", b: "ab"},
{b: "b"},
{c: "c", b: "cb"},
];

it("should implement basic object composition using a reducer", () => {
const assing = (acum, current) => ({...acum, ...current});
Expand All @@ -11,4 +16,28 @@ describe("Object Composition", () => {

expect(orig[0]).to.deep.equal(composed);
});

it("should describe Aggregation using a reducer", () => {
const collection = (array, e) => array.concat([e]);

const a = objs.reduce(collection, []);

expect(objs).to.deep.equal(a);
});

it("should describe Aggregation using a reducer, linked list example", () => {
const tuple = (a, b) => [b, a];

const linkedList = objs.reduceRight(tuple, []);

expect(linkedList)
.to.deep.equal([
{a: "a", b: "ab"}, [
{b: "b"}, [
{c: "c", b: "cb"}, []
]
]
]
);
})
});
2 changes: 1 addition & 1 deletion src/ch-14_object_composition/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Class inheritance is the strongest kind of coupling in OO Design.
- Duplication by need

## Main forms of Object Composition
- **Aggregation**.
- **Aggregation**. An Object that contains other objects. Each subobject can be destructured from the parent.
- **Concatenation**.
- **Delegation**.

0 comments on commit f470ab4

Please sign in to comment.