Last active
May 26, 2019 16:41
-
-
Save athulmurali/710790552c1a07c4d44b98fa77959fcf to your computer and use it in GitHub Desktop.
Singleton Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import coursesData from './courses' | |
function CourseService() { | |
let courses = coursesData; | |
return { | |
addCourse: (newCourse) => { | |
courses.push(newCourse); | |
}, | |
deleteCourse: (id) => { | |
courses = courses.filter(course => course.id !== id) | |
}, | |
findAllCourses: () => { | |
return courses | |
} | |
} | |
} | |
export default CourseService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CourseService from './Closure' | |
import CourseService1 from './WrapperSingleton' | |
const Comp=()=>{ | |
console.log(CourseService.findAllCourses()) | |
return <div></div> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using a wrapper class | |
class CourseService { | |
constructor(){ | |
this.instance = null; | |
this.courses=[]; | |
} | |
addCourse=(newCourse)=>{ | |
this.courses.push(newCourse); | |
} | |
deleteCourse=(id)=>{ | |
this.courses = this.courses.filter(course=>course.id !== id) | |
} | |
} | |
class SingleTon{ | |
instance = null; | |
static getInstance(){ | |
if (this.courseService) | |
return this.courseService; | |
else | |
this.courseService= new CourseService(); | |
return this.courseService; | |
} | |
} | |
export default SingleTon.getInstance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment