'패턴'에 해당되는 글 4건

  1. 2016.10.21 [OCR] 광학문자인식 정보
  2. 2015.11.17 [Singleton 패턴] 기억력 저하로 인한 복습...
  3. 2008.04.14 [PHP]php 로 구현된 singleton 패턴
  4. 2008.01.29 prototype 패턴 과 singleton 패턴

[OCR] 광학문자인식 정보

ITWeb/검색일반 2016. 10. 21. 13:01

조만간 사용을 해야해서 일단 링크 투척


https://github.com/tesseract-ocr/tesseract

https://github.com/jflesch/pyocr



opencv 도 링크 투척


http://docs.opencv.org/2.4.9/modules/refman.html


:

[Singleton 패턴] 기억력 저하로 인한 복습...

ITWeb/개발일반 2015. 11. 17. 16:15

아는 것도 자주 사용하지 않거나 보지 않으면 잊혀지기 마련 입니다.

그래서 이런 간단한 것도 기억하기 위해 글 남겨 봅니다.


한빛미디어)

http://www.hanbit.co.kr/network/view.html?bi_id=466


위키피디아)

https://en.wikipedia.org/wiki/Singleton_pattern


소프트웨어 디자인 패턴에서 싱글턴 패턴(Singleton pattern)을 따르는 클래스는, 생성자가 여러 차례 호출되더라도 실제로 생성되는 객체는 하나이고 최초 생성 이후에 호출된 생성자는 최초의 생성자가 생성한 객체를 리턴한다. 이와 같은 디자인 유형을 싱글턴 패턴이라고 한다.


구현 예제 코드는 위 링크에 자세하게 나와 있습니다.

그러나 복습 하는 차원에서 타이핑 해보겠습니다. ^^;


[Lazy Initialization]

public class SingletonDemo {

    private static volatile SingletonDemo instance;

    private SingletonDemo() { }


    public static SingletonDemo getInstance() {

        if (instance == null ) {

            synchronized (SingletonDemo.class) {

                if (instance == null) {

                    instance = new SingletonDemo();

                }

            }

        }


        return instance;

    }

}

▶ 위 코드에서는 DCL (Double Checked Lock) 기법이 적용된 예제 입니다. 쓰레드 경합 시 instance 가 생성 되지 전에 딱 한번만 locking 하도록 한다는 뭐 그런 이야기 입니다.


public class SingletonDemo {

    private static volatile SingletonDemo instance = null;

    private SingletonDemo() { }


    public static synchronized SingletonDemo getInstance() {

        if (instance == null) {

            instance = new SingletonDemo();

        }


        return instance;

    }

}

▶ 위 코드는 상대적으로 쓰레드 경합이 적은 환경에서 사용하도록 간결하게 작성된 예제 입니다.


[Eagar Initialization]

public class Singleton {

    private static final Singleton INSTANCE = new Singleton();


    private Singleton() {}


    public static Singleton getInstance() {

        return INSTANCE;

    }

}

▶ 일반적으로 간결하게 사용할 수 있는 코드 예제입니다. 하지만 instance 가 생성된 후에라야 쓰레드에서 단일 인스턴스를 사용할 수가 있게 됩니다.


[Static Block Initialization]

public class Singleton {

    private static final Singleton instance;


    static {

        try {

            instance = new Singleton();

        } catch (Exception e) {

            throw new RuntimeException("Darn, an error occurred!", e);

        }

    }


    public static Singleton getInstance() {

        return instance;

    }


    private Singleton() {

        // ...

    }

}

▶ static 블럭으로 구현한 코드 예제 입니다.


[Initialization-on-demand holder idiom]

public class Singleton {

        // Private constructor. Prevents instantiation from other classes.

        private Singleton() { }


        /**

         * Initializes singleton.

         *

         * {@link SingletonHolder} is loaded on the first execution of {@link Singleton#get

Instance()} or the first access to

         * {@link SingletonHolder#INSTANCE}, not before.

         */

        private static class SingletonHolder {

                private static final Singleton INSTANCE = new Singleton();

        }


        public static Singleton getInstance() {

                return SingletonHolder.INSTANCE;

        }

}

▶ 이 방법은 DCL 기법을 java의 메모리 모델로 변형한 예제 이고, 별도의 처리 없이 thread safe 하게 이용할 수 있습니다.


[The enum way]

public enum Singleton {

    INSTANCE;

    public void execute (String arg) {

        // Perform operation here

    }

}

▶ 위 코드 예제는 singleton 구현중 제일 우수한 코드라고 합니다.

:

[PHP]php 로 구현된 singleton 패턴

ITWeb/개발일반 2008. 4. 14. 21:19
php 로 구현된 singleton 패턴
- prototype 과 singleton
- javascript object, class & inheritance, sigletons

class singleton
// ensure that only a single instance exists for each class.
{
    function &getInstance ($class, $arg1=null)
    // implements the 'singleton' design pattern.
    {
        static $instances = array();  // array of instance names

        if (array_key_exists($class, $instances)) {
            // instance exists in array, so use it
            $instance =& $instances[$class];
           
        } else {
            // load the class file (if not already loaded)
            if (!class_exists($class)) {
                switch ($class) {
                    case 'date_class':
                        require_once 'std.datevalidation.class.inc';
                        break;

                    case 'encryption_class':
                        require_once 'std.encryption.class.inc';
                        break;

                    case 'validation_class':
                        require_once 'std.validation.class.inc';
                        break;

                    default:
                        require_once "classes/$class.class.inc";
                        break;
                } // switch
            } // if

            // instance does not exist, so create it
            $instances[$class] = new $class($arg1);
            $instance =& $instances[$class];
        } // if

        return $instance;

    } // getInstance
   
} // singleton

ref. http://www.tonymarston.net/php-mysql/singleton.html

뭐 위에 다른 글에서도 설명 되어 있지만 어떤 language 로 구현을 하던 최대 N 개로 제한 되어 지는 극한까지는 1개로 제한 되어 지는 객체를 생성 하는 패턴 방식입니다.

더 쉽게 설명 하자면 그냥 global class ,variable  또는 static 이라고도 할 수도 있겠죠.
물론 의미적으로 통할 수 있다는 설명 입니다.
근데 뭐 쉽게 이해 하는데 이 정도면 되지 않나 싶기도 하구요.. ^^*
전 어려운 용어나 개념은 별로 좋아라 하지 않아서.. ㅎㅎ
:

prototype 패턴 과 singleton 패턴

ITWeb/스크랩 2008. 1. 29. 10:42
짧게 표현 한 prototype 과 singleton 패턴

1. prototype 패턴
이미 생성된 객체를 복제해서 새로운 객체를 생성하는 방법
- 객체 생성 방식이나 구성 형태, 표현 방식 등과 무관하게 객체를 생성 하고 싶을때 유용
- 생성할 객체가 run-time 시에 결정 되어 질때 유용
ref. http://kr.search.yahoo.com/search/web?p=prototype+%C6%D0%C5%CF&ret=1&fr=kr-search_top&subtype=WebDoc
ref. http://kr.search.yahoo.com/search/web?p=prototype+pattern&ret=1&fr=kr-search_top&subtype=WebDoc

2. singleton 패턴
객체가 생성되는 개수를 제한 하는 형태의 설계가 singleton 패턴 이라 하고 극단적으로 제한되는 객체의 개수가 1개일 때를 감안한 패턴 (최대 N개 까지만 객체를 생성 하도록 제한)
- 패턴 구현시 모든 생성자는 protected 영역에 정의 되어야 함.
- 상속 관계에 놓인 클래스들에 대해 전체적으로 생성되는 객체의 최대 개수를 제한 하고자 할때 유용
ref. http://kr.search.yahoo.com/search/web?p=singleton&h=D
: