[JAVA] C # chewed design pattern: Template Method

One of the design patterns taught in the past Template method Output pattern (Maybe wrong)

[Design pattern: Template Method] (https://qiita.com/shoheiyokoyama/items/c2ce16b4f492cd014d50#abstract) The description is made by changing JAVA to C # with reference to.

Class1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Class1
{
    public abstract class AbstractDisplay
    {
        public abstract void Open();
        public abstract void Print();
        public abstract void Close();

        public void Display()
        {
            Open();
            for (int i = 0; i < 3; i++)
            {
                Print();
            }
            Close();
        }
    }
    public class CharDisplay : AbstractDisplay
    {
        char ch;
        public CharDisplay(char ch)
        {
            this.ch = ch;
        }

        public override void Open()
        {
            Console.Write("***");
        }

        public override void Print()
        {
            Console.Write(ch);
        }

        public override void Close()
        {
            Console.WriteLine("***");
        }
    }
    public class StringDisplay : AbstractDisplay
    {
        private string str;
        private int width;
        public StringDisplay(string str)
        {
            this.str = str;
            this.width = str.Length;
        }
        void PrintLine()
        {
            Console.Write("+");
            for (int i = 0; i < width; i++)
            {
                Console.Write("-");
            }
            Console.WriteLine("+");
        }

        public override void Open()
        {
            PrintLine();
        }

        public override void Print()
        {
            Console.WriteLine("|" + str + "|");
        }
        public override void Close()
        {
            PrintLine();
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            AbstractDisplay cd = new CharDisplay('T');
            cd.Display();
            AbstractDisplay sd = new StringDisplay("Design Pattern");
            sd.Display();

        }
    }
}

23/1

Recommended Posts

C # chewed design pattern: Template Method
Design pattern ~ Template Method ~
Ruby design pattern template method pattern memo
Template Method pattern
Template Method Pattern
[Design pattern] Common logic with Template Method
C ++ design pattern (TemplateMethod pattern)
Design pattern ~ Factory Method ~
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
Java beginner design pattern (Factory Method pattern)
Design pattern ~ Builder ~
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Factory Method Pattern
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
Design pattern ~ Command ~
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Factory Method pattern
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
Design pattern ~ Abstract Factory ~
GoF design pattern summary
Java design pattern summary
Item 51: Design method signatures carefully