<commit_before>---
layout: post
title: Quick look at a Strategy Pattern using Swift
author: kwysocki
tags : [iOS, swift, design patterns, strategy pattern]
comments: true
hidden: true
---

![Image Title](/images/quick-look-on-a-strategy-pattern-using-swift/strategy.jpg){: .center-image}


Let's take a quick look at one of the design patterns that should help us to write a good Object-Oriented code.
The basic assumption of Strategy Pattern is that you can define many implementations that will conform to the protocol.

Take a look at a simple example that can be used on iOS applications.


Firstly, create a `protocol` which contains a method. In our case it will be:

## Define protocol

<script src="https://gist.github.com/k8mil/51c2bef4373a063aeafc9d9cb605d9e9.js"></script>

## Create strategies

Ok, most of the iOS apps use an `UIImage` to represent images in applications. The `UIImage` instance can be used to produce two different data representations of image `UIImagePNGRepresentation` and  `UIImageJPEGRepresentation`. Let's create classes that handle this stuff.
<commit_msg>Change visibility of Strategy pattern post<commit_after>---
layout: post
title: Quick look at a Strategy Pattern using Swift
author: kwysocki
tags : [iOS, swift, design patterns, strategy pattern]
comments: true
hidden: false
---

![Image Title](/images/quick-look-on-a-strategy-pattern-using-swift/strategy.jpg){: .center-image}


Let's take a quick look at one of the design patterns that should help us to write a good Object-Oriented code.
The basic assumption of Strategy Pattern is that you can define many implementations that will conform to the protocol.

Take a look at a simple example that can be used on iOS applications.


Firstly, create a `protocol` which contains a method. In our case it will be:

## Define protocol

<script src="https://gist.github.com/k8mil/51c2bef4373a063aeafc9d9cb605d9e9.js"></script>

## Create strategies

Ok, most of the iOS apps use an `UIImage` to represent images in applications. The `UIImage` instance can be used to produce two different data representations of image `UIImagePNGRepresentation` and  `UIImageJPEGRepresentation`. Let's create classes that handle this stuff.

{'fraction_non_alphanumeric': 0.0757439134355275, 'fraction_numerical': 0.016230838593327322, 'mean_word_length': 4.748704663212435, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5935941', 'n_tokens_mistral': 645, 'n_tokens_neox': 615, 'n_words': 285}