🕵️ SicherheitslückenCVE-2023-4751 | vim up to 9.0.1247 heap-based overflow(18.09.2026 um 00:34 Uhr)
🕵️ SicherheitslückenCVE-2023-5535 | vim up to 9.0.1969 use after free(18.09.2026 um 00:34 Uhr)
🕵️ SicherheitslückenCVE-2023-4751 | vim up to 9.0.1247 heap-based overflow(18.09.2026 um 00:34 Uhr)
🕵️ SicherheitslückenCVE-2023-5535 | vim up to 9.0.1969 use after free(18.09.2026 um 00:34 Uhr)
🔧 Programmierung 🕛 vor 2 Jahren 3 Min Lesezeit
0

Design Patterns in Ruby | ADAPTER

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




Overview



The adapter is a structural design pattern that allows two objects with incompatible interfaces to work together.



. At the moment, the website is under construction, but more topics will appear there in the future.






Real World Example



Before I explain what it is and how to use the adapter pattern, think about what this name associates you with. If you’ve ever been to a country that uses other electrical sockets, you must have used an adapter similar to the one in the picture below.



So what is the purpose of using an adapter? The adapter allows you to connect a plug*(Adaptee)* to an electrical socket*(Target)* that do not fit together.






Coding Problem



First, we will create a simple User class that has two attributes, first_name and last_name, and a full_name method that returns the full name of the user.




CODE
class User
attr_reader :first_name, :last_name

def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
end

def full_name
"#{first_name} #{last_name}"
end
end






Next, we will create a Greeter class, with a hello method that will allow us to greet the user.




CODE
class Greeter
def self.hello(user)
puts "Hello, #{user.full_name}!"
end
end

mike = User.new('Michael', 'Scott')
Greeter.hello(mike) # => Hello, Michael Scott!






As we can see, everything in the example above works fine. Now let’s assume that we also want to say hello to every team. So let’s create a Team class.




CODE
class Team
attr_reader :name

def initialize(name)
@name = name
end
end

development = Team.new('Development')
Greeter.hello(development) # NoMethodError: undefined method `full_name'






The method will of course raise an error because the Team class doesn’t have a method that returns the full name.






Solution



Before we create a solution to this problem, we need to define three objects that we need in the Adapter pattern.






Target



The object we want to use. In this case, it will be the Greeter class.






Adaptee



The object we want to adapt to work with the target. In this case, it will be the Team class.






Adapter



An object that allows Adaptee to connect to a Target.



If we have already defined Target and Adaptee objects, we can prepare our Adapter class. To create it, just wrap Adaptee(Team) with a new class(Team Adapter) and then create a method(full_name) that will be compatible with the Greeter class interface.




CODE
class TeamAdapter
attr_reader :team

def initialize(team)
@team = team
end

def full_name
"#{team.name} Team"
end
end






An adapter written in such a way can easily be used to work with the Greeter class.




CODE
dev_adapter = TeamAdapter.new(development)
Greeter.hello(dev_adapter) # => "Hello, Development Team!"






. My subscribers will receive a link to my e-book for free immediately after its publication. 🆓

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
CVE-2012-5825 | Horde Kronolith 3.0.17 Portal Blocks input validation (ID 349780 / XFDB-80084)
1 Quelle
Windows-Update: Machine Identity Isolation sperrt Unternehmens-PCs - Börse Express
1 Quelle
CVE-2023-4751 | vim up to 9.0.1247 heap-based overflow
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Design Patterns in Ruby | ADAPTER

Thematisch verwandte Begriffe: Design, Patterns, Ruby, ADAPTER · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...