> For the complete documentation index, see [llms.txt](https://yyaozhibo.gitbook.io/qian-duan-xue-xi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yyaozhibo.gitbook.io/qian-duan-xue-xi/she-ji-mo-shi/factory-mode/jian-dan-gong-chang-mo-shi.md).

# 简单工厂模式

## Description

一个类或方法，通过传入一个参数，返回不同类型的对象

## Topology

![简单工厂模式 - 生成按钮](/files/-LujvF44Gg9t2JDzhYY_)

## Code

```jsx
/**
 * @description simple creator for btn
 * @param {string} type button's type
 */
function btnFactory(type) {
  if (type === 'S') {
    return (
      <button class='square'>click</button>
    )
  } else if (type === 'C') {
    return (
      <button class='circle'>click</button>
    )
  }
}
```

## Summary

### 优点

简单工厂模式实现起来非常简单，适合于简单场景的快速创建

### 缺点

如果是复杂的应用场景，简单工厂模式难以适配复杂的情况，会造成整个工厂方法体积庞大，维护困难


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yyaozhibo.gitbook.io/qian-duan-xue-xi/she-ji-mo-shi/factory-mode/jian-dan-gong-chang-mo-shi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
