> 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

### 优点

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

### 缺点

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