Skip to content

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

Notifications You must be signed in to change notification settings

takaakit/design-pattern-examples-in-java

Repository files navigation

Design Pattern Examples in Java

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

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

UML model example:

Java code example:

package structuralpatterns.composite;
// ˅

// ˄

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

    private final String name;

    private final int size;

    public File(String name, int size) {
        // ˅
        this.name = name;
        this.size = size;
        // ˄
    }

    @Override
    public String getName() {
        // ˅
        return name;
        // ˄
    }

    @Override
    public int getSize() {
        // ˅
        return size;
        // ˄
    }

    // Print this element with the "upperPath".
    @Override
    public void print(String upperPath) {
        // ˅
        System.out.println(upperPath + "/" + this);
        // ˄
    }

    // ˅
    
    // ˄
}

// ˅

// ˄

Installation

UML Modeling Tool

Java Development Environment

  • Install Java SDK 11 or higher.
  • Optionally, install an IDE (e.g. IntelliJ).

Usage

Code Generation from UML

  1. Open the Astah file (model/DesignPatternExamplesInJava.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 (if you use IntelliJ)

  1. Open the project root directory (design-pattern-examples-in-java) in IntelliJ.
  2. Open Main.java for the pattern you want to run, and click Run icon in the left gutter.

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, JavaScript, Kotlin, Python, Ruby, Scala, Swift, TypeScript

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages