Skip to content

Instantly share code, notes, and snippets.

@a21ns1g4ts
Last active November 12, 2021 15:58
Show Gist options
  • Save a21ns1g4ts/4e1da15f08a1f51198bc8b63a9c298ef to your computer and use it in GitHub Desktop.
Save a21ns1g4ts/4e1da15f08a1f51198bc8b63a9c298ef to your computer and use it in GitHub Desktop.
factory-functions
<script src="zoologicMonitor.js"></script>
<script lang="javascript">
const dog = makeDog(
{
name:"Laio",
id: 1,
}
);
dog.walk()
const person = makePerson(Person.find(2))
person.sleep()
const cat = makeCat(Cat.find(1))
cat.walk()
cat.fly()
cat.sleep()
</script>
const walkable = {
walk(){
eventSender.emitEvent(events[0], {
name: this.name, id: this.id, type: this.type
})
},
}
const sleepable = {
sleep(){
eventSender.emitEvent(events[1] , {
name: this.name, id: this.id, type: this.type
})
}
}
const flyable = {
fly(){
eventSender.emitEvent(events[2] , {
name: this.name, id: this.id, type: this.type
})
},
}
const logable = {
log(event, agent){
console.log(`
event: ${event.name};
event_id: ${event.id};
agent: ${agent.name};
agent_id: ${agent.id};
agent_type: ${agent.type};
`)
}
}
const Event = {
findEvent(id){
return events[id]
}
}
const Agent = {
find(id){
return agents[id]
},
}
const eventSender = Object.assign({
emitEvent(event , agent){
this.log(event , agent)
}
}, logable);
const Person = Object.assign({
find(id){
let agent = Agent.find(id)
if(agent.type === 'person')
return agent
}
}, walkable)
const Dog = Object.assign({} , walkable, sleepable)
const Cat = Object.assign({
find(id){
let agent = Agent.find(id);
if(agent.type === 'cat')
return agent
}
}, walkable, flyable, sleepable)
const makePerson = function (obj = {}){
return Object.assign({
name: obj.name,
id: obj.id,
type: "person"
}, Person, sleepable)
}
const makeCat = function(obj = {}){
return Object.assign({
name : obj.name,
id : obj.id,
type : "cat"
}, Cat)
}
function makeDog(obj = {}){
return Object.assign({
name: obj.name,
id: obj.id,
type: "dog"
}, Dog, flyable)
}
events = [
{id: 0, name: "walk"},
{id: 1, name: "sleep"},
{id: 1, name: "fly"},
]
agents = [
{id: 0, name: "Escureto", type: "cat"},
{id: 1, name: "Digous", type: 'cat'},
{id: 2, name: "Atila Silva", type: 'person'}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment