慣性聚合 高效追讀感興趣之博客、新聞、科技資訊
閱原文 以慣性聚合開啟

推薦訂閱源

Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
博客园 - 叶小钗
MyScale Blog
MyScale Blog
V
Visual Studio Blog
月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
I
InfoQ
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
Jina AI
Jina AI
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
Last Week in AI
Last Week in AI
雷峰网
雷峰网
Stack Overflow Blog
Stack Overflow Blog
博客园 - Franky

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python)
自面向对象至SOLID原则:一文尽览所需之知
Sanudin · 2026-05-25 · via DEV Community

时辰将逝,阅之需半刻。

此文之注记也
此篇乃吾个人研习面向对象编程及SOLID原则之笔记所成。为使此笔记更易读、更实用——既为吾自用,亦为他人所用——吾与人工智能合作,助其拓展并结构化,成此博客之体。其理其途其解,皆出吾心;人工智能则助其文辞与呈现。


佳码非偶然得。凡悦目之码库,必有其理,导其结构,联其脉络。

是文详述其程——自何谓面向对象,物如何相系,劣构何状,终至五SOLID之则,统摄其全。

且行。


卷一:何谓面向对象?

面向对象编程(OOP)者,乃摹拟世情之码也。非列长篇之令,乃聚相干之数与行止于物象中。

🤖 想一机器人玩具。其有属性(色、高、电),又有行止。 (行、言、揖)。在面向对象中,彼机器人乃一物——而筑之之图式名曰

二要义:

  • ——图式(模板)
  • ——自图式所筑之实

四柱

其功用
继承 子类承袭父类之属性与行止
封装 内蕴隐匿——唯受控之出入得允
抽象 繁复内构隐去;惟所需者显之
多态 同举一动作,其效可异于所对之物

第二部:物象相系

物无孤立存焉。物相连接,相协作,相依存,——及其键入论情爱之事。

结社

二物相知,可互应。有三品之别:

  • 一一对应 — 一人一证
  • 一对多 — 一师众生
  • 多对多 — 众学子,众科程

内联之际,一物"拥有"他物,其关系或为:

  • 聚合 (松散) — 子存而父亡。书在包中,然书犹在,纵弃其包。
  • 组合 (紧密) — 子无父则不存。室在屋中,然毁其屋,室亦亡。

依存

暂时的、基于使用的关系。一法中用他法,然不永持之。弱于关联.

普化 & 实现

  • 普化 — 多类共质,汇于一超类(承袭)
  • 实现 — 一类实现接口之物,允诺践履既定之约

第三部:劣构之貌

未习规则之前,先明所御之弊。罗伯特·C·马丁指陈劣构三征:

🪨 刚硬 — 系统难易更易。触一物,众物皆需更迭。开发者畏于变改。

🍪 脆弱 — 更易之际,系统于不期处崩坏。汝修支付之模块,而邮件之通知竟废。

🏗️ 固滞 — 有用之件不可复用。其与周遭纠缠甚深,欲取之,费时逾于另起炉灶。

三者同根,皆源于组件间依存管理之失


第四部:SOLID

SOLID乃五则之要旨,直应前述之弊。每则各攻一弊。


S — 单一职责原则

一模块当专责一役,不兼他务。

每班当有且仅有一一故易之— 专供一角色(一群利益相关者)。

👨‍🍳庖厨兼收银、守卫、配递,实乃祸根。一职稍变,众职皆乱。

分其职任,各司其事。


O — 开闭之则

"器物当开以延展,闭以守旧。"

增新行,宜以之延展,非更易既有之工码也.

🔌 电源排插,使新器得接,而排插本身无需重布线。此即开闭之要义也.

当需求易,新码并置旧码之侧,勿改已行之有效.


L — Liskov代换之理

若S为T之亚型,则T类型之对象,可易以S类型之对象,而程序之正确性不受变。

子类必也可全然替代为其本类。本类所许,子必践之。

若母机善烹,子机亦必善烹,不可致误或默然无为。

若子类有虚置之方法,或仅有桩实现,或抛出"不支"之异常——此乃里氏替换之违犯.


我——接口隔离之原则

"客户不应被迫依赖其未使用之接口。"

勿创臃肿之接口,强令类实现其无需之方法.分拆接口化而為小,專而精之。

勿以汤客索刀、勺、箸、签,非所求也。

小界面即窄依赖,变易自居于一隅。


D — 依赖倒置原则

高层之模,不可倚赖低层之模。二者皆当依凭抽象。

汝之商理,勿固于特定之实。当通乎接口—而其下之细末,则施此诸面。

勿将刮刀焊于机器人臂。宜以标准接驳,使工具可进可出。

此乃使基设(数据库、API、文件系统)可更易,而无需触及业务逻辑之由也。


简要参考:SOLID之要义

原则 所解之困 当问之要义
单一职责原则 诸类过载之责 "此类是否服务多于一方之干系人?"
OCP 修旧码以增新能 "可否作扩展而非修改乎?"
LSP 遗统之阶次崩坏 "可易子以父而无败乎?"
ISP 臃肿之界面多无用之法 "此课是否强求其行非所需之事?"
DIP 商理囿于基构 "吾之高层码,是否倚于一不应倚之具体类?"

当从何处始

若尔初行此道:

  1. 当以单一职责原则为始 — 寻汝过劳之课,而分之
  2. 察LSP之违 — 空法乃警兆
  3. 于基构之界设接口 — 此即DIP之实

此等原则非清单,乃每撰码时当问之疑也


初载于sanudin.dev