DEV Community

Sergey Leschev
Sergey Leschev

Posted on β€’ Edited on

2 2 1 1 1

πŸ‘“ Observer Pattern

Observer Pattern


The observer pattern is used to allow an object to publish changes to its state. Other objects subscribe to be immediately notified of any changes.

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

Example

protocol PropertyObserver : class {
    func willChange(propertyName: String, newPropertyValue: Any?)
    func didChange(propertyName: String, oldPropertyValue: Any?)
}

final class TestChambers {
    weak var observer:PropertyObserver?

    private let testChamberNumberName = "testChamberNumber"

    var testChamberNumber: Int = 0 {
        willSet(newValue) {
            observer?.willChange(propertyName: testChamberNumberName, newPropertyValue: newValue)
        }
        didSet {
            observer?.didChange(propertyName: testChamberNumberName, oldPropertyValue: oldValue)
        }
    }
}

final class Observer : PropertyObserver {
    func willChange(propertyName: String, newPropertyValue: Any?) {
        if newPropertyValue as? Int == 1 {
            print("Okay. Look.")
        }
    }

    func didChange(propertyName: String, oldPropertyValue: Any?) {
        if oldPropertyValue as? Int == 0 {
            print("Sorry about the mess.")
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Usage

var observerInstance = Observer()
var testChambers = TestChambers()
testChambers.observer = observerInstance
testChambers.testChamberNumber += 1
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

Top comments (0)