함수와 OOP(3)

minchan jung·2022년 3월 7일
0

함수 와 OOP

목록 보기
4/4

객체 지향 프로그래밍

1. 추상화

  1. 기법
  • Categorize (분류)
  • Modeling (모델링)
  • Grouping (집합)

2. 객체지향 언어 및 프로그램

  1. 대체가능성 : 구현체는 추상클래스 및 인터페이스를 대체할 수 있어야 함
  2. 내적 동질성
  • 태생의 identity를 유지함
  • 부모내 자식, 추상클래스내 구현체 method 호출시, 자식 및 구현체 method 실행되어야 함.
  • 부모 클래서 타입으로 자식 생성시
  • instance of 부모 = True 대체 가능성
  • instance of 자식 = True 내적 동질성
  • method는 모두 override된 자식 method로 실행

3. 객체지향 Analysis

  • 추상화 기법으로 객체(사물) 분류하여 객체 후보군 설정
  • 독립적인 객체로 구분되도록 분류화
  • 단일 권한 및 책임로 설계
  • 의존성은 단방향 simplicity 설계

4. HTML parser

// 재귀 코드 by me
const parser = (input, curParent, prevParent) => {
  input = input.trim();
  if(input.length < 1) return curParent
  if(input[0] === '<'){
    const idx = input.indexOf('>');
    console.log(input)
    if(input[idx -1] === '/'){ 
      curParent.text = input.split('/')[0]
      parser(input.split('>')[1], curParent, prevParent)
    }
    else if(input[input.indexOf('<')+1] === '/'){
        curParent.child.push({ name : input.substring(1, idx-1), child :[] });
        parser(input.substring(idx+1). curParent, prevParent);
    }
    else{
      const newParent = { name : input.substring(1,idx), child : [] };
      curParent.child.push(newParent);
      parser(input.substring(idx+1), newParent, curParent);
    }
  }
  return curParent
}

let res = parser(inp, { name : 'Root', child : [] },{ name : 'Root', child : [] })
console.log(res)
//reference code
let inp = `<div>
        a
        <a>b</a>
    </div>`;


const parser = input => {
  input = input.trim();
  const result = { name : 'ROOT', type : 'node', children : [] }; 
  let i = 0, j = input.length;
  let curr = {  tag: result , parent : 'body'} ; 

  while(i < j){
    const cursor = i ; 
    if(input[cursor] === '<'){
      let name, isClose;
      const idx = input.indexOf('>', cursor);
      name = input[idx - 1]=== '/'  
        ? input.subString(cursor + 1, idx-1)
        : input.subString(cursor )  
    }
    else{
      const idx = input.indexOf('<', cursor);
      curr.text = input.subString(cursor, idx);
      i = idx;      
    }
  }
}

NOTE

javascript는 보통 재귀 호출 100번을 제한하며 stack over flow error를 내 뱉는다.

함수와 OOP-3

0개의 댓글