1. Definition
The Adapter pattern, often known as the Wrapper pattern, is a structural design pattern that allows objects with incompatible interfaces to collaborate. It acts as a bridge between two interfaces. The Adapter pattern involves creating a new adapter class that combines the interface of one class with the interface of another.
2. Problem Statement
Imagine a scenario where you have an old component in your software that works perfectly fine but has an outdated interface. Now, you're introducing a new system that expects objects to have a different interface. Directly integrating these systems would be problematic due to interface incompatibilities.
3. Solution
The Adapter pattern suggests creating a separate adapter class that translates calls from the new system to the old component without changing any existing code. The new system communicates with the adapter, and the adapter communicates with the old component.
4. Real-World Use Cases
1. Modern USB-C to old USB-A converters.
2. Adapting new UI components to work with legacy backend systems.
3. Integrating third-party libraries with different methods or properties.
5. Implementation Steps
1. Identify the existing target interface that needs to be adapted.
2. Create a new adapter class that implements the new expected interface.
3. The adapter class will contain a reference to the instance of the old component.
4. The adapter will translate method calls from the new interface to the old component's methods.
6. Implementation in Swift Programming
// Step 1: Old component with a different interface. class OldSystem { func oldRequest() -> String { return "Data from old system." } } // Step 2: New expected interface. protocol NewSystemProtocol { func request() -> String } // Step 3: Adapter class. class Adapter: NewSystemProtocol { private let oldSystem: OldSystem init(oldSystem: OldSystem) { self.oldSystem = oldSystem } // Translate new request to old request. func request() -> String { return oldSystem.oldRequest() } } // Usage: let old = OldSystem() let adapter = Adapter(oldSystem: old) print(adapter.request())
Output:
Data from old system.
Explanation:
1. We have an OldSystem class with a method oldRequest that fetches some data.
2. Our new components expect objects to have a request method based on the NewSystemProtocol protocol.
3. The Adapter class acts as a bridge. It implements the new expected interface and contains a reference to the old component.
4. When the request method of the adapter is called, it translates that call to the oldRequest method of the old system.
7. When to use?
The Adapter pattern is useful when:
1. You want to use an existing class with a different interface.
2. You want to create a reusable class that works with classes with non-compatible interfaces.
3. You want to integrate systems or libraries with different interfaces without changing the source code of either.
Design Pattern Related Swift Examples:
Swift Hello World Program Swift Program to Add Two Numbers Swift Program to Subtract Two Numbers Swift Program to Multiply Two Numbers Swift Program to Divide Two Numbers Swift Program to Find Remainder Swift Program to Check Even or Odd Swift Program to Find Factorial of a Number Swift Program to Generate Fibonacci Series Swift Program to Swap Two Numbers Without Using Temporary Variable Swift Program to Find Largest Among Three Numbers Swift Program to Calculate the Area of a Circle Swift Program to Reverse a Number Swift Program to Make a Simple Calculator Swift Program to Check Palindrome Swift Program to Count Number of Digits in an Integer Swift Program to Sum of Natural Numbers Swift Program to Display Times Table Swift Program to Check Prime Number Swift Program to Find LCM Swift Program to Find GCD Swift Program to Find the Power of a Number Swift Program to Split a String into Words Swift Program to Check Leap Year Swift Program to Join Two Strings Swift Program to Check Armstrong Number Swift Program to Find Sum of Array Elements Swift Program to Find the Largest Element of an Array Swift Program to Perform Matrix Addition Swift Program to Transpose a Matrix Swift Program to Multiply Two Matrices Swift Program to Find Length of a String Swift Program to Copy One String to Another String Swift Program to Concatenate Two Strings Swift Program to Search for a Character in a String Swift Program to Count Frequency of a Character in String Swift Program to Create a Simple Class and Object Swift Program to Implement Inheritance Swift Program to Handle Simple Exceptions Swift Variables and Constants Example Swift Data Types (Int, Double, String) Example Swift Optionals and Optional Binding Example Swift Tuples Example Swift Array Example Swift Dictionary Example Swift Set Example Swift Closures Example Swift Enums Example Swift Structures Example Swift Properties (Stored, Computed) Example Swift Methods (Instance, Type) Example Swift Subscripts Example Swift Inheritance and Overriding Example Swift Protocols Example Swift Extensions Example Swift Generics and Generic Functions Example Swift Error Handling with Do-Catch Example Swift Guard Statement Example Swift Defer Statement Example Swift Type Casting (as, is, as?) Example Swift Access Control Example Swift Attributes (@available, @discardableResult) Example Swift Pattern Matching Example Swift Switch Statement and Cases Example Swift For-In Loop Example Swift While and Repeat-While Loops Example Swift Conditional Statements (If, If-Else, Ternary) Example Swift Operators Example Swift Memory Management Example Swift Strong, Weak, and Unowned References Example Swift Initialization and Deinitialization Example Swift Protocol-Oriented Programming Example Swift Nested Types Example Swift Type Aliases Example Swift Dynamic Member Lookup Example Swift Lazy Stored Properties Example Swift KeyPaths Example Swift String Manipulation and Methods Example Swift Regular Expressions Example Swift
Comments
Post a Comment