> For the complete documentation index, see [llms.txt](https://sugar-at.gitbook.io/blog-article/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sugar-at.gitbook.io/blog-article/design/abstract-factory.md).

# 抽象工厂模式

## 实体类

```javascript
class Bird {
    run() {
        console.log('bird');
    }
}

class Dog {
    run() {
        console.log('dog');
    }
}

class Male {
    run() {
        console.log("Male");
    }
}

class Female {
    run() {
        console.log("female");
    }
}
```

## 抽象工厂类与工厂类

```javascript
class AbstractFactory {
    getPerson() {
        return new Error("子类还未实现此接口");
    }

    getAnimal() {
        return new Error("子类还未实现此接口");
    }
}

class PersonFactory extends AbstractFactory {
    getPerson(person) {
        person = person.toLowerCase();
        switch (person) {
            case "male": return new Male();
            case "female": return new Female();
            default: break;
        }
    }

    getAnimal() {
        return null;
    }
}

class AnimalFactory extends AbstractFactory {
    getPerson() {
        return null;
    }

    getAnimal(animal) {
        animal = animal.toLowerCase();
        switch (animal) {
            case "dog": return new Dog();
            case "bird": return new Bird();
            default: break;
        }
    }
}
```

## 超级工厂

```javascript
class Factory {
    constructor(choice) {
        choice = choice.toLowerCase();
        switch (choice) {
            case "person": return new PersonFactory();
            case "animal": return new AnimalFactory();
            default: break;
        }
    }
}
```

## demo测试

```javascript
const personFactory = new Factory("person");
let male = personFactory.getPerson("male"),
    female = personFactory.getPerson("female");

male.run();
female.run();

const animalFactory = new Factory("animal");
let dog=animalFactory.getAnimal("dog"),
bird =animalFactory.getAnimal("bird");

dog.run();
bird.run();
```

## demo2

```javascript
//手机抽象工厂
class MobilePhoneFactory {
    // 提供操作系统接口
    createOs() {
        throw new Error('抽象工厂方法不允许直接调用，你需要将我重写!')
    }

    // 提供硬件接口
    createHardWare() {
        throw new Error('抽象工厂方法不允许直接调用，你需要将我重写!')
    }
}

// 操作系统
class OS {
    controlHardWare() {
        throw new Error('抽象工厂方法不允许直接调用，你需要将我重写!')
    }
}


class AndroidOS extends OS {
    controlHardWare() {
        console.log('我会用安卓的方式去操作硬件')
    }
}

class AppleOS extends OS {
    controlHardWare() {
        console.log('我会用苹果的方式去操作硬件')
    }
}

// 硬件产品
class HardWare {
    operateByOrder() {
        throw new Error('抽象工厂方法不允许直接调用，你需要将我重写!')
    }
}

class QualcommHardWare extends HardWare {
    operateByOrder() {
        console.log("我会用高通的方式去运转");
    }
}

class MiHardWare extends HardWare {
    operateByOrder() {
        console.log('我会用小米的方式去运转');
    }
}

// 山寨安卓手机
class FakeAndroidPhone extends MobilePhoneFactory {
    createHardWare() {
        return new MiHardWare();
    }

    createOs() {
        return new AndroidOS();
    }
}

const myPhone = new FakeAndroidPhone();
//拥有操作系统
const myOs = myPhone.createOs();
//拥有硬件
const myHandWare = myPhone.createHardWare();

// 启动操作系统
myOs.controlHardWare();

// 启动硬件
myHandWare.operateByOrder();
```


---

# 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, and the optional `goal` query parameter:

```
GET https://sugar-at.gitbook.io/blog-article/design/abstract-factory.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
