Wednesday 9 November 2016

Design Patterns In Swift

Today we learn about design patterns used in Swift.There are different types of design pattern used in swift.Before proceeding  forward lets start with design pattern  definition.
Design Pattern:
 In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.
Use of Design Patterns :
 Design patterns can speed up the development process by providing tested, proven development paradigms. Effective software design requires considering issues that may not become visible until later in the implementation. Reusing design patterns helps to prevent subtle issues that can cause major problems and improves code readability for coders and architects familiar with the patterns.

Often, people only understand how to apply certain software design techniques to certain problems. These techniques are difficult to apply to a broader range of problems. Design patterns provide general solutions, documented in a format that doesn't require specifics tied to a particular problem.

In addition, patterns allow developers to communicate using well-known, well understood names for software interactions. Common design patterns can be improved over time, making them more robust than ad-hoc designs.

Types of Design Patterns
There are different types of design patterns implemented in iOS.
  1. Abstract Factory
  2. Adapter
  3. Command
  4. Composite
  5. Decorator
  6. Facade
  7. Mediator
  8. Observer
  9. Proxy
  10. Singleton
  11. MVC
Today we learn about Abstract Patten.

Abstract Pattern :
The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.
Class Cluster
A class cluster is an architecture that groups a number of private concrete subclasses under a public abstract superclass. The abstract superclass declares methods for creating instances of its private subclasses. The superclass dispenses an object of the proper concrete subclass based on the creation method invoked. Each object returned may belong to a different private concrete subclass.
Class clusters in Cocoa can generate only objects whose storage of data can vary depending on circumstances. The Foundation framework has class clusters for NSString, NSData, NSDictionary, NSSet, and NSArray objects. The public superclasses include these immutable classes as well as the complementary mutable classes NSMutableString, NSMutableData, NSMutableDictionary, NSMutableSet, and NSMutableArray


Example:


















protocol AppleProduct {
  
  var name: String {get set}
  var screenSize: Double {get set}
  var price: Double {get set}
  
  func getProduct() -> String
}

// Protocl for "Creator"
protocol AppleProductCreator {
  
  func createProduct() -> AppleProduct
}

// Class for "ConcreteProduct"
class IPhoneProduct: AppleProduct {
  
  var name: String = "iPhone 6"
  var screenSize: Double = 4.7
  var price: Double = 199.00
  
  func getProduct() -> String {
    
    return "Apple product "+name+", with screen size in (screenSize) inch, at proce: $(price)"
  }
}

class IPadProduct: AppleProduct {
  
  var name: String = "iPad Air 3"
  var screenSize: Double = 10.1
  var price: Double = 499.00
  
  func getProduct() -> String {
    
    return "Apple product "+(name)+", with screen size in (screenSize) inch, at proce: $(price)"
  }
}

// Class for "ConcreteCreator"
class IPhoneProductCreator: AppleProductCreator {
  
  static let sharedInstance:IPhoneProductCreator = IPhoneProductCreator()
  
  func createProduct() -> AppleProduct {
    
    return IPhoneProduct()
  }
}

class IPadProductCreator: AppleProductCreator {
  
  static let sharedInstance:IPadProductCreator = IPadProductCreator()
  
  func createProduct() -> AppleProduct {
    
    return IPadProduct()
  }
}

// Client Usage
let product1: AppleProduct = IPhoneProductCreator.sharedInstance.createProduct()
print("Got a new device: " + product1.getProduct())

let product2: AppleProduct = IPadProductCreator.sharedInstance.createProduct()

print("Got a new device: " + product2.getProduct())


Output:
Got a new device: Apple product iPhone 6, with screen size in (screenSize) inch, at proce: $(price)
Got a new device: Apple product iPad Air 3, with screen size in (screenSize) inch, at proce: $(price)


    Conclusion :
    Today we learn about Abstract pattern.Rest of design pattern I will discussed in my upcoming  blogs.
    If you have any questions just leave a comment below and I’ll respond as soon as I can.

No comments:

Post a Comment