본문 바로가기
C# 디자인패턴

11# Decorator패턴 Structural Patterns

by NaHyungMin 2019. 6. 3.

 

static void Main(string[] args)

{

    //BuilderExample();

    //FactoryExample();

    //FactoryMethodExample();

    //FactoryMethodExample2();

    //ProtoTypeExample();

    //SingletonExample();

    //AdapterExample();

    //BridgeExample();

    //CompositeExample();

 

    DecoratorExample();

 

    //Console.WriteLine("Hello World!");

    Console.ReadKey();

}

 

static void DecoratorExample()

{

    Book book = new Book("Worley""Inside asp.net"10);

    book.Display();

 

    Video video = new Video("spielberg""jaws"9223);

    video.Display();

 

    Console.WriteLine("\nMaking video borrwable :");

 

    Borrowable borrowable = new Borrowable(video);

    borrowable.BorrowItem("customer #1");

    borrowable.BorrowItem("customer #2");

 

    borrowable.Display();

}

 

main.cs

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Structural_Patterns.Decorator

{

    public abstract class LibraryItem

    {

        private Int32 numCopies;

 

        public Int32 NumCopies

        {

            get => numCopies;

            set => numCopies = value;

        }

 

        public abstract void Display();

    }

}

 

LibraryItem.cs

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Structural_Patterns.Decorator

{

    public class Decorator : LibraryItem

    {

        protected LibraryItem libraryItem;

 

        public Decorator(LibraryItem libraryItem)

        {

            this.libraryItem = libraryItem;

        }

 

        public override void Display()

        {

            libraryItem.Display();

        }

    }

}

 

Decorator.cs

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Structural_Patterns.Decorator

{

    public class Borrowable : Decorator

    {

        private List<string> borrowers = new List<string>();

 

        public Borrowable(LibraryItem libraryItem)

            : base(libraryItem)

        {

 

        }

 

        public void BorrowItem(string name)

        {

            borrowers.Add(name);

            libraryItem.NumCopies--;

        }

 

        public void ReturnItem(string name)

        {

            borrowers.Remove(name);

            libraryItem.NumCopies++;

        }

 

        public override void Display()

        {

            base.Display();

 

            foreach(string borrower in borrowers)

            {

                Console.WriteLine(string.Format("borrower : {0}", borrower));

            }

        }

    }

}

 

Borrowable.cs

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Structural_Patterns.Decorator

{

    public class Book : LibraryItem

    {

        private string author;

        private string title;

 

        public Book(string author, string title, Int32 numCopies)

        {

            this.author = author;

            this.title = title;

            this.NumCopies = numCopies;

        }

 

        public override void Display()

        {

            Console.WriteLine("\nBook ------ ");

            Console.WriteLine(" Author: {0}", author);

            Console.WriteLine(" Title: {0}", title);

            Console.WriteLine(" # Copies: {0}", NumCopies);

        }

    }

}

 

Book.cs

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Structural_Patterns.Decorator

{

    public class Video : LibraryItem

    {

        private string director;

        private string title;

        private Int32 playTime;

 

        public Video(string director, string title, Int32 playTime, Int32 numCopies)

        {

            this.director = director;

            this.title = title;

            this.playTime = playTime;

            this.NumCopies = numCopies;

        }

 

        public override void Display()

        {

            Console.WriteLine("\nVideo ----- ");

            Console.WriteLine(" Director: {0}", director);

            Console.WriteLine(" Title: {0}", title);

            Console.WriteLine(" # Copies: {0}", NumCopies);

            Console.WriteLine(" Playtime: {0}\n", playTime);

        }

    }

}

 

Video.cs

 

참고 사이트

https://www.dofactory.com/net/decorator-design-pattern