{"meta":{"title":"Refactoring to implement a design pattern","intro":"副驾驶聊天 can suggest design patterns that you can use to improve your code.","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/tutorials","title":"教程"},{"href":"/zh/copilot/tutorials/copilot-cookbook","title":"GitHub Copilot Cookbook"},{"href":"/zh/copilot/tutorials/copilot-cookbook/refactor-code","title":"Refactor code"},{"href":"/zh/copilot/tutorials/copilot-cookbook/refactor-code/refactor-design-patterns","title":"Refactor design patterns"}],"documentType":"article"},"body":"# Refactoring to implement a design pattern\n\n副驾驶聊天 can suggest design patterns that you can use to improve your code.\n\nCode that doesn’t follow established design patterns (like Singleton, Factory, or Observer) can lead to issues like tight coupling, lack of flexibility, or duplicated code.\n\n副驾驶聊天 can help you use appropriate design patterns to make your code more flexible and maintainable. For example, it can recommend converting repetitive instantiations into a Factory pattern, or using the Observer pattern to decouple tightly connected classes.\n\n> \\[!NOTE] The responses described in this article are examples. 副驾驶聊天 responses are non-deterministic, so you may get different responses from the ones shown here.\n\n## Example scenario\n\nThis JavaScript code creates three `Person` objects, each with `name` and `age` properties. It then uses the name of a person to retrieve and print their age.\n\n```javascript id=person-module\nclass Person {\n  constructor(name, age) {\n    this.name = name;\n    this.age = age;\n  }\n}\n\nvar people = {};\n\nfunction addPerson(name, age) {\n  people[name] = new Person(name, age);\n}\n\nfunction getAge(name) {\n  if (people[name]) {\n    console.log(name + \" is \" + people[name].age + \" years old.\");\n  } else {\n    console.log(\"Person not found.\");\n  }\n}\n\naddPerson(\"Ana\", 30);\naddPerson(\"Mario\", 25);\naddPerson(\"Louise\", 40);\n\ngetAge(\"Mario\");\n```\n\n## Example prompt 1\n\n```copilot copy prompt ref=person-module\nWhat design patterns could improve this code? Don't show me code examples.\n```\n\n## Example response 1\n\nCopilot suggests the following patterns:\n\n* **Singleton pattern:** to ensure that there is only one instance of the `people` object.\n* **Factory pattern:** to create a factory for creating `Person` objects.\n* **Module pattern:** to encapsulate the `people` object and related functions within a module to avoid polluting the global namespace.\n\n## Example prompt 2\n\nYou can now ask Copilot to implement the pattern that you feel is most appropriate.\n\n```copilot copy prompt ref=person-module\nRefactor this code using the module pattern\n```\n\n## Example response 2\n\n```javascript\nconst PersonModule = (function() {\n  class Person {\n    constructor(name, age) {\n      this.name = name;\n      this.age = age;\n    }\n  }\n\n  const people = {};\n\n  function addPerson(name, age) {\n    people[name] = new Person(name, age);\n  }\n\n  function getAge(name) {\n    if (people[name]) {\n      console.log(name + \" is \" + people[name].age + \" years old.\");\n    } else {\n      console.log(\"Person not found.\");\n    }\n  }\n\n  return {\n    addPerson,\n    getAge\n  };\n})();\n\nPersonModule.addPerson(\"Ana\", 30);\nPersonModule.addPerson(\"Mario\", 25);\nPersonModule.addPerson(\"Louise\", 40);\n\nPersonModule.getAge(\"Mario\");\n```\n\nThe module pattern improves code organization, enhances data privacy, and reduces the risk of naming conflicts, making the code more maintainable and scalable.\n\n## Further reading\n\n* [GitHub Copilot 对话助手的提示设计](/zh/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [使用 GitHub Copilot 的最佳做法](/zh/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}