<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>设计模式 on 知行 - 大道至简，知易行难；道阻且长，行则将至。</title>
    <link>https://blog.itdn.top/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/</link>
    <description>Recent content in 设计模式 on 知行 - 大道至简，知易行难；道阻且长，行则将至。</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>zh-CN</language>
    <copyright>郑超(Charles·Zheng)</copyright>
    <lastBuildDate>Tue, 03 Jun 2025 18:22:17 +0000</lastBuildDate><atom:link href="https://blog.itdn.top/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>「学习笔记」Python进阶编程（下）</title>
      <link>https://blog.itdn.top/posts/2025/python_advanced_2/</link>
      <pubDate>Tue, 03 Jun 2025 18:22:17 +0000</pubDate>
      
      <guid>https://blog.itdn.top/posts/2025/python_advanced_2/</guid>
      <description>
        
          
            1、设计模式 设计模式 是解决软件设计中常见问题的通用解决方案。设计模式提供了一套经过验证的最佳实践，帮助开发者编写可维护、可扩展和可复用的代码。
1.1 工厂模式 封装对象创建逻辑，通过统一接口创建不同类型的对象。
from abc import ABC, abstractmethod # 抽象产品接口 class LLMProvider(ABC): &amp;#34;&amp;#34;&amp;#34;LLM提供者抽象基类&amp;#34;&amp;#34;&amp;#34; @abstractmethod def generate(self, prompt: str) -&amp;gt; str: &amp;#34;&amp;#34;&amp;#34;生成文本响应&amp;#34;&amp;#34;&amp;#34; pass # 具体产品：OpenAI class OpenAIProvider(LLMProvider): &amp;#34;&amp;#34;&amp;#34;OpenAI LLM提供者&amp;#34;&amp;#34;&amp;#34; def generate(self, prompt: str) -&amp;gt; str: return f&amp;#34;OpenAI: {prompt}&amp;#34; # 具体产品：Anthropic class AnthropicProvider(LLMProvider): &amp;#34;&amp;#34;&amp;#34;Anthropic LLM提供者&amp;#34;&amp;#34;&amp;#34; def generate(self, prompt: str) -&amp;gt; str: return f&amp;#34;Anthropic: {prompt}&amp;#34; # 工厂类 class LLMFactory: &amp;#34;&amp;#34;&amp;#34; LLM提供者工厂类：根据类型创建对应的LLM提供者实例&amp;#34;&amp;#34;&amp;#34; # 注册的提供者映射 _providers = { &amp;#34;openai&amp;#34;: OpenAIProvider, &amp;#34;anthropic&amp;#34;: AnthropicProvider } @classmethod def create(cls, provider_type: str) -&amp;gt; LLMProvider: &amp;#34;&amp;#34;&amp;#34; 创建LLM提供者实例 Args: provider_type: 提供者类型（&amp;#34;openai&amp;#34;或&amp;#34;anthropic&amp;#34;） Returns: LLMProvider实例 Raises: ValueError: 未知的提供者类型 &amp;#34;&amp;#34;&amp;#34; provider_class = cls.
          
          
        
      </description>
    </item>
    
  </channel>
</rss>
