学会这个思维,你也能开奶茶店(传统编程经典设计模式 模板方法模式)

344 阅读3分钟

前言

每一天都有人要去喝一杯茶或咖啡来开启每一天的生活,不过你们知道这些是怎么做的吗?这篇文章就让我们用传统编程经典设计模式-模板方法模式 去理解茶是怎么制作的。
本篇文章针对传统编程与AIGC的融合来写面向过程的实际操作。

正文

茶,在生活中人们都注重的是味道,而本次,我们要深入过程
首先,我们直接进入代码 创建一个html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模板方法模式</title>
</head>
<body>
    <script>
var Tea = function (type){
            this.type = type
            console.log('你准备泡一杯'+this.type);
        }
     </script>
</body>
</html>

接下来,就要用分步思维,将过程细化

  • 把水煮沸
  • 用沸水冲泡茶叶
  • 把茶水倒进杯子
  • 加柠檬

既然要创建一个函数去完成上面的任务,不如另辟蹊径,直接将Tea作为一个对象去使用
对比下图的两段代码,第二张图的Tea是可执行对象,构建我们的对象 this指向的这些属性就是实例的属性模板 image.png image.png

既然Tea是可执行对象,就要通过prototype 添加一些方法,都可以以它为原型的对象共享(public)

Tea.prototype.boilWater = function(){
    console.log('把水煮沸');
}
Tea.prototype.steepTeaBag = function(){
    console.log('用沸水浸泡茶叶');
}
Tea.prototype.pourInCup = function(){
    console.log('把茶水倒进杯子');
}
Tea.prototype.addLemon = function(){
    console.log('加柠檬');
}

用传统编程的方法一步步写出过程 最后就是实现代码,

Tea.prototype.init = function(){
        this.boilWater();
        this.steepTeaBag();
        this.pourInCup();
        this.addLemon();
}

下面是传统编程的完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模板方法模式</title>
</head>
<body>
    <script>
        

function Tea(type){
            this.type = type
            console.log('你准备泡一杯'+this.type);
        }
// 对象
Tea.prototype.boilWater = function(){
    console.log('把水煮沸');
}
Tea.prototype.steepTeaBag = function(){
    console.log('用沸水浸泡茶叶');
}
Tea.prototype.pourInCup = function(){
    console.log('把茶水倒进杯子');
}
Tea.prototype.addLemon = function(){
    console.log('加柠檬');
}
Tea.prototype.init = function(){
        this.boilWater();
        this.steepTeaBag();
        this.pourInCup();
        this.addLemon();
}

// 当以new的方式被运行
        var lemonTea = new Tea('柠檬茶');
        Tea.prototype.init();
        // 接口,模板方法接口,
        
    </script>
</body>
</html>

本文既然提到了AIGC 那就理所应当有用大模型的方法来解决这个过程 废话不多说,直接上代码

require('dotenv').config();
const OpenAI = require('openai')
const client = new OpenAI({
    apiKey: process.env.OPENAI_KEY,
    baseURL:'https://api.chatanywhere.tech/v1'
})
const getChatResponse = async function(model,prompt,n){
    const response = await client.chat.completions.create({
        model:model, // 适合聊天的模型   有很多种
        messages: [{
            role : 'user',
            content : prompt
        }]
    })
    return response.choices[0].message.content;
}

const main = async()=>{
    //AIGC 优势就是处理文本,生成内容
    const text= ` 
        泡一杯茶很容易。首先,需要把水烧开。
        在等待期间,拿一个杯子并把茶包放进去。
        一旦水开了,就把它倒在茶包上。
        等待一会儿,让茶叶浸泡,几分钟后,取出茶包。
        如果你愿意,可以加一些糖或牛奶调味。
        就这样,你可以享受一杯美味的茶了。
    `
    const prompt = `
    你将获得由三个引号括起来的文本。
    如果它包含一系列的指令,则需要按照以下格式从新编写这些指令。
    
    第一步 - ...
    第二步 - ...
    ...
    第N步 - ...

    如果文本中不包含一系列的指令,则直接写"未提供步骤"
    """${text}"""
    `
    const result = await getChatResponse('gpt-3.5-turbo',prompt);
    console.log(result);
}

main();

这段代码中的key需要读者自己去找哦,直接将apiKey:后的内容替换就可以了 image.png 以下就是最后呈现出的效果

image.png

结语

传统编程经典设计模式 模板方法模式 , 在大型项目 设计 init 接口来实现分布编程,交给大模型(AIGC适合解决的问题)也一样,流程控制类问题 传统编程和AIGC都可以解决。