DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Designing Software With Swift (Part III): Type-Based Design Without Protocols

Designing Software With Swift (Part III): Type-Based Design Without Protocols

Look over another design option in Swift, and learn why it's maybe better than you'd expect.

Christopher Lamb user avatar by
Christopher Lamb
CORE ·
Apr. 28, 16 · Mobile Zone · Tutorial
Like (4)
Save
Tweet
2.70K Views

Join the DZone community and get the full member experience.

Join For Free

So let’s take a look at type-based design. First, we'll look at a generic system designed without protocols. This sounds like a mistake, but honestly, generics and protocols don't play together well, so why not? As opposed to using protocol-based reuse, we'll lean on generics instead.

We'll design and Observer and a Subject, where the Subject notifies registered Observers of state changes.

public class Observer<T> {

  let name : String = String(arc4random_uniform(10000))

  public func update(data: T) {
  print("updated: \(data)")
  }
}

public final class Subject<T, S: Hashable> {

  private var observers : [S: Observer<T>] = [:]

  private var data : T?

  public var state : T? {
    get {
      return data
    }
    set(data) {
      self.data = data
      notify()
    }
  }

  public func attach(k: S, o: Observer<T>) {
    observers[k] = o
  }

  public func detach(k: S) {
    observers.removeValueForKey(k)
  }

  private func notify() {
    if let mydata = data {
      for (_, observer) in observers {
        observer.update(mydata)
      }
    }
  }
}


The first system, using generic typing without protocols. This is very simple. A good thing! simple software tends to be underrated. It's easier to test, easier to build, and easier to understand. As software engineers, we're all guilty of overdesigning code at least once in our careers (especially if we've worked with Java, amirite?). Simple software is much cheaper to build and maintain, even if it seems not to be as reusable or flexible. But honestly, is that a big deal? After all, we all know you're not going to need it (yes, YAGNI, worst acronym ever).

Anyway, we run the system like this:

var o = Observer<String>()
var s = Subject<String, String>()
s.attach(o.name, o: o)
s.state = "state change!"


This produces the output you’d expect. Nice, consice and clean. Let's see how we can make this more complex next.

Protocol (object-oriented programming) Software Design Swift (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Use Geofences for Precise Audience Messaging
  • Testing Under the Hood Or Behind the Wheel
  • Suspicious Sortings in Unity, ASP.NET Core, and More
  • 4 Different Ways to Work With Nebula Graph in Apache Spark

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo