전체 글56 디자인패턴 - 싱글톤 패턴 Singleton pattern ❓ 하나의 클래스에 오직 하나의 인스턴스만 가지는 패턴 보통 데이터베이스 연결 모듈에 많이 사용 💜 장점 - 하나의 인스턴스를 만들어 놓고 해당 인스턴스를 다른 모듈들이 공유하며 사용하기 때문에 인스턴스 생성 비용이 줄어듬 🖤 단점 - 의존성이 높아짐 Javascript 리터럴 {} 혹은 new Object로 객체를 생성하게 되면 다른 어떤 객체와도 같지 않으므로 싱글톤 패턴 구현 가능 예제 1 const obj = { a : 27 }; const obj2 = { a : 27 } console.log(obj === obj2) // false 예제 2 class Singleton { constructor() { if (!Singleton.instance) { Singleto.. 2023. 3. 28. Dart Mixins Mixins // Mixins : 생성자가 없는 클래스 // 클래스에 프로퍼티들을 추가하거나 할 때 사용 // 상속 개념 아니고 단순히 with 사용하여 mixin 내부의 프로퍼티와 메소드를 가져옴 class Strong { final double strengthLevel = 1500.99; } class QuickRunner { void runQuick() { print("ruuuuuuuun!"); } } class Tall { final double height = 1.99; } // 요런식으로 여러 클래스에 재사용하려고 씀!! class Player with Strong, QuickRunner, Tall { final team; Player({ required this.team, }); } clas.. 2023. 2. 4. Dart Inheritance Inheritance class Human { final String name; Human(this.name); void sayHello() { print("Hi my name is $name"); } } enum Team { blue, red } class Player extends Human { final Team team; Player({ required this.team, required String name, }) : super(name); // 받아온 name을 super에 name을 전달(확장한 부모 클래스) // Human에서 온 sayHello를 직접 만든 메소드로 override(대체) @override void sayHello() { super.sayHello(); print('and.. 2023. 2. 3. Dart Emnums type Emnums type // Emnums type // 개발자가 실수하지 않도록 도와주는 타입 // 생성 enum Team { red, blue } enum XPLevel { beginner, medium, pro } class Player { String name; XPLevel xp; Team team; Player({ required this.name, required this.xp, required this.team, }); } void main() { var siha = Player( name: 'siha', xp: XPLevel.medium, team: Team.red, ); var potato = siha ..name = 'las' ..xp = XPLevel.pro ..team = Team.b.. 2023. 2. 2. Dart Cascade Notation Cascade Notation // Cascade Notation class Player { String name; int xp; String team; Player({ required this.name, required this.xp, required this.team, }); void sayHello() { print('Hello! My name is $name'); } } void main() { // 방법 1 // var siha = Player(name: 'siha', xp: 1200, team: 'red'); // siha.name = 'las'; // siha.xp = 1000; // siha.team = 'blue'; // 방법 2 // Cascade operator var siha = P.. 2023. 1. 31. Dart Named Constructors2(json형태로 받아온 data 활용) json형태로 받아온 정보써보기!!!! // fromJson이라는 named constructor 만들기 class Player { final String name; int xp; String team; // 자신이 사용할 데이터에 맞게 constructor 만들어주는 것이 중요! // string인 키를 가지고 value는 dynamic인 map을 받음 Player.fromJson(Map playerJson) : name = playerJson['name'], xp = playerJson['xp'], team = playerJson['team']; // 값들을 playerJson 내부의 값이라고 정해줌 void sayHello() { print('Hello! My name is $name'); } } .. 2023. 1. 30. Dart Named Countructor Parameter 사용예제(+ 클래스 초기화 문법) Named Countructor Parameters // 클래스가 커질 경우 위치 기준 생성자를 만드는 것은 비효율적 // 이문제를 해결하는 방법 1 -> Named Countructor Parameters class Player { final String name; int xp; String team; int age; Player({ required this.name, required this.xp, required this.team, required this.age, }); void sayHello() { print("Hi! my name is $name"); } } void main() { var player = Player( name: 'siha', xp: 1200, team: 'blue', ag.. 2023. 1. 22. Dart Class, constructors 기본 예제 class // class // 함수안에서는 자료형 명시해줄 필요 없지만 클래스에서는 필요 class Player { String name = 'siha'; // 값을 바꿀 수 없게 하려면 final 써주면 됨 final int xp = 1500; int age = 8; void sayHello() { // this.name 과 name 둘다 사용가능(name 쓰도록 권고!) // this를 쓰는 경우 ? => age와 같이 변수가 겹쳐서 8을 가져오고 싶은 경우 var age = 10; print("Hi my name is $name. I'm ${this.age} years old."); } } void main() { var player = Player(); print(player.name); // .. 2023. 1. 21. Dart QQ operator, QQ assignment operation QQ operator(question question) ?? left ?? right 좌항이 null이면 우항을 return함 String capitalizeName(String? name) => name.toUpperCase(); // null 값을 보내주기 위햐여 String? 달아주기 // null값 처리 방법 1 // String capitalizeName(String? name) { // if (name != null) { // return name.toLowerCase(); // } // return 'ANON'; // } // null값 처리 방법 2 (더 깔곰쓰) // String capitalizeName(String? name) => // name != null ? name.toUppe.. 2023. 1. 20. 이전 1 2 3 4 5 ··· 7 다음