Last active
May 8, 2026 09:36
-
-
Save Bodacious/4db5166950823c6ca49074e2c08cac2b to your computer and use it in GitHub Desktop.
Interface segregation principle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module ClientFeatures | |
| def authenticate(username, password) | |
| puts "Authenticating #{username}..." | |
| end | |
| def send_notification(message) | |
| puts "Sending notification: #{message}" | |
| end | |
| def send_email(to, subject, body) | |
| puts "Sending email to #{to}: #{subject}" | |
| end | |
| def log_request(endpoint) | |
| puts "Logging request to #{endpoint}" | |
| end | |
| def track_analytics(event) | |
| puts "Tracking analytics: #{event}" | |
| end | |
| end | |
| # Base class with full CRUD operations | |
| class BaseClient | |
| def create(data) | |
| puts "Creating resource: #{data}" | |
| end | |
| def read(id) | |
| puts "Reading resource: #{id}" | |
| end | |
| def update(id, data) | |
| puts "Updating resource #{id}: #{data}" | |
| end | |
| def delete(id) | |
| puts "Deleting resource: #{id}" | |
| end | |
| def batch_operation(items) | |
| puts "Performing batch operation on #{items.length} items" | |
| end | |
| end | |
| class ReadOnlyClient < BaseClient | |
| include ClientFeatures | |
| def initialize(name) | |
| @name = name | |
| end | |
| def fetch(id) | |
| # Only uses read() from BaseClient | |
| read(id) | |
| # Only uses log_request() from ClientFeatures | |
| log_request("/api/resources/#{id}") | |
| end | |
| end | |
| # Demonstration | |
| client = ReadOnlyClient.new("read-only-api") | |
| # What the client actually uses: | |
| client.fetch(123) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment