Monday 21 November 2016

Adapter Pattern In Swift

Today I will talk about Adapter pattern in swift.What is Adapter pattern and what are its pros and cons.How I will use adapter pattern in my swift code.Lets start now.
When I think about is adapter.Generally it comes to my  mind that adapter is 

a device for connecting pieces of equipment that cannot be connected directly.What in terms of design pattern it can be defined as.

Adapter Design Pattern:
In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.
Adapter pattern works as a bridge between two  interfaces. This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces.
This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces.
Example:
A real life example could be a case of card reader which acts as an adapter between memory card and a laptop. You plugin the memory card into card reader and card reader into the laptop so that memory card can be read via laptop.



Use of Design Pattern:
This pattern is often used when dealing with frameworks, libraries, and APIs to easily "adapt" the old and existing interfaces to the new requirements of a program.

Roles:
The adapter pattern converts the interface of another existing class into an interface waited by the existing clients to allow them to work together. Thanks to this pattern, you can integrate components for which you normally cannot modify the source code and things that often appear with the use of a framework.

The adapter pattern is used to provide a link between two otherwise incompatible types by wrapping the "adaptee" with a class that supports the interface required by the client.

Example:
protocol PersonBioData {
  var fullName: String {get}
  var wholeAddress: String {get}
}
//Adapptee
struct PersonDetails{
  var firstName: String
  var lastName: String
  var pincode: String
  var street : String
  var city : String
  var houseNo : String
  init(firstName:String,lastName:String,pincode:String,street:String,houseNo:String,city:String) {
    self.firstName = firstName
    self.lastName = lastName
    self.pincode = pincode
    self.street = street
    self.houseNo = houseNo
    self.city = city
  }
}

//Adapter
struct EmployeeRecordBook: PersonBioData {
  private let personDetail : PersonDetails
  var fullName: String{
    let fullName = personDetail.firstName + " " + personDetail.lastName
    return fullName
    
  }
  var wholeAddress: String {
   let wholeAddess = personDetail.houseNo+" "+personDetail.street+" "+personDetail.city+" "+personDetail.pincode
    return wholeAddess
  }
  init(personDetail: PersonDetails) {
    self.personDetail = personDetail
  }
}
let personDetails = PersonDetails(firstName: "Ashok", lastName: "Kumar", pincode: "110086", street: "Nehru Road", houseNo: "House # 65", city: "Mumbai")
let employeeRecordBook = EmployeeRecordBook(personDetail: personDetails)
print(employeeRecordBook.fullName)

print(employeeRecordBook.wholeAddress)

Output
Ashok Kumar
House # 65 Nehru Road Mumbai 110086

In this you can see that PersonDetails(adaptee) class is wrapped with EmployeeRecordBook(adapter class). 

Conclusion : 
Today we learn about Adapter pattern used in Swift.If you have any questions just leave a comment below and I’ll respond as soon as I can.


No comments:

Post a Comment