Skip to main content

Design Patterns in Swift

What is a Design pattern?
A design pattern is a general solution to a real-world problem that can be re-used.
Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.
They are templates designed to help us write efficient code which is easy to understand, also can be re-used.
It is not a finished design that can be transformed directly into source or machine code.

Why do we need Design patterns?

  • Saves Time: Design patterns can speed up the development process by providing tested, proven development paradigms
  • Future bugs: Effective design pattern can solve issues that may not become visible until later in the implementation, which helps while designing software.
  • Re-use: Reusing design patterns helps to prevent subtle issues that can cause major problems.
  • Code Readability: Improves code readability for coders and architects familiar with the patterns.
  • Best practices: Design patterns teaches us some of the best practices for good software design.
  • More work, Less code: Using Design patterns more work can be done by writing less code. Hence they save time & energy.
  • Stop Re-inventing: You don’t need to waste time and reinvent the wheel trying to solve a particular software development problem, as design patterns already provide the best solution and tell you how to implement it.
  • Scaling: By following design patterns we can easily scale our existing system.
  • Testing made easy: Last but not least, A easily Testable software can be produced using design patterns.

What problems does the design patterns solve?
Some common problems are

  • How objects are created & used?
  • What should be an object?
  • How should we design a class?
  • How objects should communicate with each other?
  • What are the relationships between a class and an object?
  • What are the roles & responsibilities of an object?
  • How do we design a system that can be scaled easily?

How does Design Patterns solves Problems?
  • A design pattern names, abstracts and identifies the key aspects of software design by identifying the participating classes, objects and instances, their roles, collaborations, interrelationships and the distribution of responsibilities.

Design patterns solve different problems in their own way, we'll see them later in depth.
Now that we cleared our doubts on what, why and how. We'll discuss most commonly used Design patterns.


Creational design pattern:
Deals with object creation mechanisms which means, how objects can be created in a way suitable for a particular situation.

  • Builder:
    Separates object construction from its representation
  • Abstract Factory:
    Creates an instance of several families of classes
  • Prototype:
    A fully initialized instance to be copied or cloned
  • Singleton:
    A class of which only a single instance can exist

Structural design patterns: Aim to simplify the design by finding an easy way of realizing relationships between classes and objects.
  • Adapter:
    Match interfaces of different classes
  • Facade:
    A simple interface or a class that represents an entire subsystem
  • Decorator:
    Add responsibilities to objects dynamically

Behavioural design pattern: identifies common communication patterns between entities and implement these patterns
  • Observer:
    A way of notifying a change to a number of classes
  • Memento:
    Capture and restore an object's internal state

Stay tuned...

Comments

Popular posts from this blog

UILabel text animation as in UIButton click

Animating UILabel textcolor is not easy as it seems, normal animation won't work on this property. The below code snippet animates the UILabel textcolor as in button click.
Change the duration and UIViewAnimationOptions to play with it. extensionUILabel{ func flashLabel() { func animate(duration:Double,alpha:CGFloat,completion:(()->())?){ UIView.transition(with: self, duration: duration, options: UIViewAnimationOptions.transitionCrossDissolve, animations: { self.textColor = self.textColor?.withAlphaComponent(alpha) }) { _ in completion?() } } animate(duration: 0.1, alpha: 0) { animate(duration: 0.3, alpha: 1, completion: nil) } } } Happy coding...

Confused OS Concepts

Difference between MultiProcessor and Multicore Processor? A multiprocessorsystem contains more than one CPU, allowing them to work in parallel.
multicoreCPU has multiple execution cores on one CPU so that multiple cores can work in parallel on separate operations at chip level. Difference between MultiProgramming, MultiTasking, MultiProcessing,      MultiThreading? MultiProgramming:To minimize the CPU Idle time, Multiprogramming was introduced in older days for single core processor. One or more programs are loaded into main memory which are ready to execute still, only one program is able to get the CPU for execution while all others are waiting for their turn. When currently running process is performing an I/O task, then OS may interrupt that process and give the control to another program(process context switching) loaded in main memory for execution and a running process keeps executing until either it voluntarily releases the CPU or when it blocks for an I/O operation.
With Mutil…

Stack Problem - Balancing of Symbols

Stack Problem - Balancing of Symbols
Problem: Given an expression string exp , write a program to examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp.

Example:
Input: exp = “[()]{}{[()()]()}”
Output: Balanced Input: exp = “[(])”
Output: Not Balanced
Algorithm:
Create a stack.while ( end of input is not reached ) {If the character read is not a symbol to be balanced, ignore it.If the character is an opening delimiter like ( , {  or [ , PUSH it into the stack.If it is a closing symbol like ) , } , ] , then if the stack is empty report an error, otherwise POP the stack.If the symbol POP-ed is not the corresponding delimiter, report an error.At the end of the input, if the stack is not empty report an error.Implementation :
func balancingOfSymbols(string:String)->Bool{ guard string.count > 0else { return false } let symbols:[Character:Character] = ["{":"}","[":"]","(":")",] v…