DEV Community

Sergey Leschev
Sergey Leschev

Posted on β€’ Edited on

3 3 1 1 1

πŸ’‘ Strategy Pattern

Strategy Pattern


The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time.

Github https://github.com/sergeyleschev/design-patterns

Example

struct TestSubject {
    let pupilDiameter: Double
    let blushResponse: Double
    let isOrganic: Bool
}

protocol RealnessTesting: AnyObject {
    func testRealness(_ testSubject: TestSubject) -> Bool
}

final class VoightKampffTest: RealnessTesting {
    func testRealness(_ testSubject: TestSubject) -> Bool {
        return testSubject.pupilDiameter < 30.0 || testSubject.blushResponse == 0.0
    }
}
final class GeneticTest: RealnessTesting {
    func testRealness(_ testSubject: TestSubject) -> Bool {
        return testSubject.isOrganic
    }
}
final class BladeRunner {
    private let strategy: RealnessTesting
    init(test: RealnessTesting) { self.strategy = test }
    func testIfAndroid(_ testSubject: TestSubject) -> Bool {
        return !strategy.testRealness(testSubject)
    }
}
Enter fullscreen mode Exit fullscreen mode

Usage

let rachel = TestSubject(pupilDiameter: 30.2,
                         blushResponse: 0.3,
                         isOrganic: false)

// Deckard is using a traditional test
let deckard = BladeRunner(test: VoightKampffTest())
let isRachelAndroid = deckard.testIfAndroid(rachel)

// Gaff is using a very precise method
let gaff = BladeRunner(test: GeneticTest())
let isDeckardAndroid = gaff.testIfAndroid(Rachel)
Enter fullscreen mode Exit fullscreen mode

Sources: Github

Behavioral
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.
Source: wikipedia.org


🐝 Chain Of Responsibility
πŸ‘« Command Pattern
🎢 Interpreter Pattern
🍫 Iterator Pattern
πŸ’ Mediator Pattern
πŸ’Ύ Memento Pattern
πŸ‘“ Observer Pattern
πŸ‰ State Pattern
πŸ’‘ Strategy Pattern
πŸ“ Template Method
πŸƒ Visitor Pattern
🌰 Abstract Factory
πŸ‘· Builder Pattern
🏭 Factory Method
πŸ”‚ Monostate Pattern
πŸƒ Prototype Pattern
πŸ’ Singleton
πŸ”Œ Adapter Pattern
πŸŒ‰ Bridge Pattern
🌿 Composite Pattern
🍧 Decorator Pattern
🎁 Facade Pattern
πŸƒ Flyweight Pattern
β˜” Protection Proxy
🍬 Virtual Proxy


Contacts
I have a clear focus on time-to-market and don't prioritize technical debt. And I took part in the Pre-Sale/RFX activity as a System Architect, assessment efforts for Mobile (iOS-Swift, Android-Kotlin), Frontend (React-TypeScript) and Backend (NodeJS-.NET-PHP-Kafka-SQL-NoSQL). And I also formed the work of Pre-Sale as a CTO from Opportunity to Proposal via knowledge transfer to Successful Delivery.

πŸ›©οΈ #startups #management #cto #swift #typescript #database
πŸ“§ Email: sergey.leschev@gmail.com
πŸ‘‹ LinkedIn: https://linkedin.com/in/sergeyleschev/
πŸ‘‹ LeetCode: https://leetcode.com/sergeyleschev/
πŸ‘‹ Twitter: https://twitter.com/sergeyleschev
πŸ‘‹ Github: https://github.com/sergeyleschev
🌎 Website: https://sergeyleschev.github.io
🌎 Reddit: https://reddit.com/user/sergeyleschev
🌎 Quora: https://quora.com/sergey-leschev
🌎 Medium: https://medium.com/@sergeyleschev
πŸ–¨οΈ PDF Design Patterns: Download

πŸ‘‹ Kindness is contagious

Before you go

Please leave your appreciation by commenting on this post!

It takes one minute and is worth it for your career.

Get started

Top comments (0)

πŸ‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay