Skip to main content

Structural Design Patterns - Adapter

Structural Design Patterns in Swift

Adapter: To unify the interfaces of incompatible classes that already exists

Bridge: It decouples abstraction from its implementation, so that we can change them independently. It reduces the impact of changes

Decorator: Used to add new responsibilities to objects without modifying the underlying classes

Composite pattern: Describes how to combine objects so that each of them can be either simple or composite object.

Facade: Allows us to expose functionality of entire sub system via single class

Fly weight pattern: Used to effectively sharing no. of instances of a class by reducing number of object instances at runtime.

Proxy pattern: Proxy objects acts as a surrogate for more complex objects that me expensive for create or use.




Adapter: This pattern maps the existing interface to other interface.


Eg., Two classes A and B are implementing a protocol P, so we use polymorphism to call the methods of A and B classes. Now when a new class C does not implement the protocol P and we need to use the class C along with A and B, we couldn’t do it as the interface is different. In order to add the new method/interface to C (similar to A and B) we need Adapter Pattern

Adapter Pattern can be used in two ways 
  1. Creating new adapter class:
    Creating class D which adopts method protocol P and insod

    protocol P {
         func shareMessage(msg: String)
    }
    class D: P {
         func shareMessage(msg: String) {
               someObjectWithDifferentInterface.differentMethod(message: msg)
         }  
  2. Extending existing class to add new functionality:

    extension C: P {
         func shareMessage(msg: String) {
               someObjectWithDifferentInterface.differentMethod(message: msg)
         }  
    }

Adapter - The problem when adapter is not used



Adapter - The solution with object adapter




Adapter - The solution with adapter via extension  




Comments

Popular posts from this blog

Apple Special Event 12 Sep 2018 Updates

Apple introduced the following devices in todays event. Apple watch series 4 Ability to take an ECG using the watch. ECG data will be available in health app. It is FDA approved. Will be available in US on September 17th It also has fall detection and sends location update to emergency contacts. Larger screen size (30% larger) iPhone Xs and Xs Max (iOS 12) Water proof upto 2 meters for 30 mins Available in 5.8 inch super retina and 6.5 inch super retina display It has A12 Bionic Chip, first 7 nanometer chip, with 6.9 billion transistors, 6-Core CPU, which means it is 50% energy efficient and faster than previous A11 bionic. Use cases of Core ML, We can measure the leg angle and projectile path other metrics using HomeCourt App Ability to adjust the depth effect of already taken photos is another important feature. Dual Sim Capability. (Single physical sim and another eSim) Made with recyclable plastic and tin (for logic board)   iPhone Xr Ava...

Advanced Debugging with Xcode and LLDB - WWDC 2018

Advanced Debugging Tips and Tricks: Injecting Code at runtime We can change values of variables at runtime while debugging (here variable_name is a Bool) expression variable_name = false  Here we are injecting value at runtime, so no need to run again to see the effect. (Other eg.,  expression animator.delegate = self) Under Xcode > Preferences > Behaviours > Running > Pauses > Check Show Tab Named, In order to show new tab while a breakpoint is hit. We can set other behaviours as well. We can add symbolic breakpoints on methods in frameworks. like This is in objective c because UIKit is in objective c. In order to find the values/parameter of a function inside framework, we can use For expression -[UILabel setText:] po $arg1 <UILabel ****> po (SEL)$arg2   "setText:" po $arg3 "0 ft" We can set symbolic breakpoints in 2 ways 1. By clicking plus in bottom left and choosing symbolic (Cons: Sets BP in all places) 2. By adding a breakpoi...

User defined settings iOS

With User defined settings  we can set values specific to build configuration. Creating User Defined Settings Select Target > Build Settings Click Plus icon, present beside the search bar Using User Defined Settings when we need to access the values set in User defined build settings we can do it in two ways  From info.plist $(NEW_SETTING) From Code let value = Bundle.main.infoDictionary?["NEW_SETTING"] as! String