Skip to content

Latest commit

 

History

History
120 lines (93 loc) · 4.26 KB

File metadata and controls

120 lines (93 loc) · 4.26 KB

Design Pattern Examples in C#

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

  • To understand GoF Design Pattern examples in C#.
  • To understand the mapping between UML model and C# code.
  • To try model-driven development (MDD) using Astah and m plus plug-in.

UML model example:

C# file example:

// ˅
using System;
using System.Collections.Generic;
using System.Text;

// ˄

namespace StructuralPatterns.Composite
{
    public class File : FileSystemElement
    {
        // ˅

        // ˄

        public override string Name
        {
            // ˅
            get;
            // ˄
        }

        public override int Size
        {
            // ˅
            get;
            // ˄
        }

        public File(string name, int size)
            // ˅
            
            // ˄
        {
            // ˅
            Name = name;
            Size = size;
            // ˄
        }

        // Print this element with the "upperPath".
        public override void Print(string upperPath)
        {
            // ˅
            Console.WriteLine($"{upperPath}/{this}");
            // ˄
        }

        // ˅
        
        // ˄
    }
}

// ˅

// ˄

Installation on Windows

UML Modeling Tool

Visual Studio

  • Download and install Visual Studio.
    Check ".NET desktop development" when installing.

Usage on Windows

Code Generation from UML

  1. Open the Astah file (model/DesignPatternExamplesInCsharp.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 solution file (design-pattern-examples-in-csharp.sln) in Visual Studio.
  2. Set up a startup project, and run it.

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

License

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