Skip to content

UML model and code examples of design patterns for Swift. The model is created with Astah.

License

Notifications You must be signed in to change notification settings

takaakit/design-pattern-examples-in-swift

Repository files navigation

Design Pattern Examples in Swift

Model and code examples of GoF Design Patterns for Swift.
This project is available for the following objectives:

  • To understand GoF Design Pattern examples in Swift.
  • To understand the mapping between UML model and Swift code.
  • To try Model-Driven Development (MDD) using Astah and M PLUS plug-in.

UML model example:

Swift code example:

// ˅

// ˄

public class File: FileSystemElement {
    // ˅
    
    // ˄

    private let name: String

    private let size: Int

    public init(name: String, size: Int) {
        // ˅
        self.name = name
        self.size = size
        // ˄
    }

    // File name
    public override func getName() -> String {
        // ˅
        return name
        // ˄
    }

    // File size
    public override func getSize() -> Int {
        // ˅
        return size
        // ˄
    }

    // Print this element with the "upperPath".
    public override func print(upperPath: String) {
        // ˅
        Swift.print("\(upperPath)/\(self.toString())")
        // ˄
    }

    // ˅
    
    // ˄
}

// ˅

// ˄

Installation on macOS

UML Modeling Tool

Xcode

  • Download and install Xcode.
    The operation has been confirmed in Swift 5.7.1 and Xcode 14.1.

Usage on macOS

Code Generation from UML

  1. Open the Astah file (model/DesignPatternExamplesInSwift.asta).
  2. Select model elements on the model browser of Astah.
  3. Click the Generate Code button.

    The generated code has User Code Area. The User Code Area is the area enclosed by "˅" and "˄". Handwritten code written in the User Code Area remains after a re-generation. View code example.
    For detailed usage of the tools, please see Astah Manual and M PLUS plug-in Tips.

Build and Run

  1. Open the workspace file (design-pattern-examples-in-swift.xcworkspace) in Xcode.
  2. Select the run scheme from the Scheme menu.
  3. Click the Run button.

References

  • Gamma, E. et al. Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley, 1994
  • Hiroshi Yuki. Learning Design Patterns in Java [In Japanese Language], Softbank publishing, 2004

Licence

This project is licensed under the Creative Commons Zero (CC0) license. The model and code are completely free to use.

CC0

Other Language Examples

C++, C#, Crystal, Go, Java, JavaScript, Kotlin, Python, Ruby, Scala, TypeScript

About

UML model and code examples of design patterns for Swift. The model is created with Astah.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages