728x90

일련의 프로세스가 정형화 되어 있고 각 프로세스의 변경사항이 있을 경우

템플릿패턴 형태로 관리하면 효율적일 것 같다.

 

UML

https://www.youtube.com/watch?v=qr7I18Lhsl8

소스코드

using System;

namespace Template
{
    class Program
    {
        static void Main(string[] args)
        {
            //input data
            string clientInfo = "";
            string sendData = "";
            string resultData = "";


            //기본접속
            AbstConnectProcess connectHelper = new DefaultConnectHelper();
            connectHelper.Connect(out resultData, clientInfo, sendData);
            Console.WriteLine("default connect result :: " + resultData);

            //표준접속
            connectHelper = new StandardConnectHelper();
            connectHelper.Connect(out resultData, clientInfo, sendData);
            Console.WriteLine("standard connect result :: " + resultData);


        }
    }
}
namespace Template
{
    public abstract class AbstConnectProcess
    {
        //자식 클래스 외에는 접근하지 못하도록 projected 로!
        protected abstract void CheckServerState(string clinetInfo);
        protected abstract void CheckSecurity(string clientInfo);
        protected abstract void SendData(string sendData);
        protected abstract void ReceiveData(out string resultData);

        //override 되지 않도록 virtual 선언하지 않도록
        public void Connect(out string resultData, string clientInfo, string sendData)
        {
            CheckServerState(clientInfo);
            CheckSecurity(clientInfo);
            SendData(sendData);
            ReceiveData(out resultData);
        }

    }
}
using System;

namespace Template
{
    class DefaultConnectHelper : AbstConnectProcess
    {
        protected override void CheckSecurity(string clientInfo)
        {
            Console.WriteLine("기본 보안 체크");
        }

        protected override void CheckServerState(string clinetInfo)
        {
            Console.WriteLine("기본 서브 체크");
        }

        protected override void ReceiveData(out string resultData)
        {
            Console.WriteLine("기본 데이터 수신");
            resultData = "기본 연결 성공";
        }

        protected override void SendData(string sendData)
        {
            Console.WriteLine("기본 데이터 송신");
        }
    }
}
using System;

namespace Template
{
    class StandardConnectHelper : AbstConnectProcess
    {
        protected override void CheckSecurity(string clientInfo)
        {
            Console.WriteLine("표준 보안 체크");
        }

        protected override void CheckServerState(string clinetInfo)
        {
            Console.WriteLine("표준 서브 체크");
        }

        protected override void ReceiveData(out string resultData)
        {
            Console.WriteLine("표준 데이터 수신");
            resultData = "표준 연결 성공";
        }

        protected override void SendData(string sendData)
        {
            Console.WriteLine("표준 데이터 송신");
        }

       
    }
}

 

출력결과

728x90

'Skills > DesignPattern' 카테고리의 다른 글

[DesignPattern]FlyWeight  (0) 2020.03.12
728x90

https://youtu.be/aEar_3UB65M

데이터를 공유 사용하여 메모리를 절약하는 패턴

일반적으로 공통으로 사용되는 객체는 새로 생성하지 않고 공유하여 자원을 활용

 

C#(https://ko.wikipedia.org/wiki/%ED%94%8C%EB%9D%BC%EC%9D%B4%EC%9B%A8%EC%9D%B4%ED%8A%B8_%ED%8C%A8%ED%84%B4)

using System.Collections;
using System.Collections.Generic;
using System;

class GraphicChar {
    char c;
    string fontFace;
    public GraphicChar(char c, string fontFace) { this.c = c; this.fontFace = fontFace; }
    public static void printAtPosition(GraphicChar c, int x, int y)   {
        Console.WriteLine("Printing '{0}' in '{1}' at position {2}:{3}.", c.c, c.fontFace, x, y);
    }
}

class GraphicCharFactory {
    Hashtable pool = new Hashtable(); // the Flyweights

    public int getNum() { return pool.Count; }

    public GraphicChar get(char c, string fontFace) {
        GraphicChar gc;
        string key = c.ToString() + fontFace;
        gc = pool[key] as GraphicChar;
        if (gc == null) {
            gc = new GraphicChar(c, fontFace);
            pool.Add(key, gc);
        }
        return gc;
    }
}

class FlyWeightExample {
    public static void Main(string[] args) {
        GraphicCharFactory cf = new GraphicCharFactory();

        // Compose the text by storing the characters as objects.
        List<GraphicChar> text = new List<GraphicChar>();
        text.Add(cf.get('H', "Arial"));    // 'H' and "Arial" are called intrinsic information
        text.Add(cf.get('e', "Arial"));    // because it is stored in the object itself.
        text.Add(cf.get('l', "Arial"));
        text.Add(cf.get('l', "Arial"));
        text.Add(cf.get('o', "Times"));

        // See how the Flyweight approach is beginning to save space:
        Console.WriteLine("CharFactory created only {0} objects for {1} characters.", cf.getNum(), text.Count);

        int x=0, y=0;
        foreach (GraphicChar c in text) {             // Passing position as extrinsic information to the objects,
            GraphicChar.printAtPosition(c, x++, y);   // as a top-left 'A' is not different from a top-right one.
        }
    }
}

 

여러 오브젝트에 의해 다양한 데이터가 필요로 하고, 그 데이터는 중복되기도 한다면

flyweight 패턴으로 중복을 없애고 재사용하므로써 메모리를 절약하고 데이터 코드 수정을 일괄적으로 할 수 있다.

 

그런데 게임에서 실제로 어느 상황에서 쓰일까??

기본적인 속성테이블들은 고유 인덱스를 가지고 있고

row가 100개면 인덱스도 100개라서 번거롭게 flyweight패턴 쓸일이 없고.. 그냥 dic이나 hash에 그대로  for문 돌려서 넣어 버리면 되는데...

 

중복 row가 있는 테이블을 읽어서 메모리에 할당 해놓을떄 유용할텐데... 

 

 

728x90

'Skills > DesignPattern' 카테고리의 다른 글

[DesignPattern]Template  (0) 2020.03.14

+ Recent posts