pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://docs.github.com/ko/copilot/tutorials/copilot-cookbook/refactor-code/refactor-design-patterns

-head=""/>
Skip to main content

디자인 패턴을 구현하기 위한 리팩터링

공동 파일럿 채팅 에서는 코드를 개선하는 데 사용할 수 있는 디자인 패턴을 제안할 수 있습니다.

설정된 디자인 패턴(예: 싱글톤, 팩터리, 옵서버)을 따르지 않는 코드는 긴밀한 결합, 유연성 부족, 중복된 코드와 같은 문제를 야기할 수 있습니다.

공동 파일럿 채팅 에서는 적절한 디자인 패턴을 사용하여 코드를 보다 유연하고 유지 관리할 수 있도록 할 수 있습니다. 예를 들어 반복 인스턴스화를 팩터리 패턴으로 변환하거나 관찰자 패턴을 사용하여 긴밀하게 연결된 클래스를 분리하는 것이 좋습니다.

참고

이 문서에 설명된 응답은 예제입니다. 공동 파일럿 채팅 응답은 비결정적이므로 여기에 표시된 응답과 다른 응답을 얻을 수 있습니다.

예제 시나리오

이 JavaScript 코드는 각각 Personname 속성을 사용하여 age 개체 세 개를 만듭니다. 그런 다음, 사람의 이름을 사용하여 나이를 검색하고 인쇄합니다.

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

var people = {};

function addPerson(name, age) {
  people[name] = new Person(name, age);
}

function getAge(name) {
  if (people[name]) {
    console.log(name + " is " + people[name].age + " years old.");
  } else {
    console.log("Person not found.");
  }
}

addPerson("Ana", 30);
addPerson("Mario", 25);
addPerson("Louise", 40);

getAge("Mario");

예제 프롬프트 1

Copilot prompt
What design patterns could improve this code? Don't show me code examples.

예제 응답 1

Copilot 에서는 다음 패턴을 제안합니다.

  • 싱글톤 패턴: people 개체의 인스턴스가 하나만 있는지 확인합니다.
  • 팩터리 패턴: Person 개체를 만들기 위한 팩터리를 만듭니다.
  • 모듈 패턴: 전역 네임스페이스를 오염하지 않도록 모듈 내의 people 개체와 관련 함수를 캡슐화합니다.

예제 프롬프트 2

이제 가장 적절하다고 생각되는 패턴을 구현하도록 요청할 Copilot 수 있습니다.

Copilot prompt
Refactor this code using the module pattern

예제 응답 2

const PersonModule = (function() {
  class Person {
    constructor(name, age) {
      this.name = name;
      this.age = age;
    }
  }

  const people = {};

  function addPerson(name, age) {
    people[name] = new Person(name, age);
  }

  function getAge(name) {
    if (people[name]) {
      console.log(name + " is " + people[name].age + " years old.");
    } else {
      console.log("Person not found.");
    }
  }

  return {
    addPerson,
    getAge
  };
})();

PersonModule.addPerson("Ana", 30);
PersonModule.addPerson("Mario", 25);
PersonModule.addPerson("Louise", 40);

PersonModule.getAge("Mario");

모듈 패턴은 코드 조직을 개선하고, 데이터 개인 정보를 향상하며, 명명 충돌 위험을 줄여 코드를 보다 유지 관리하고 확장성 있게 만듭니다.

추가 읽기

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy