user: 프로토타입 패턴에 대해 설명해 주세요.

gpt: **프로토타입 패턴**은 소프트웨어 디자인 패턴 용어로, 생성할 객체들의 타입이 프로토타입인 인스턴스로부터 결정되도록 하며, 인스턴스는 새 객체를 만들기 위해 자신을 복제하게 된다.

패턴을 구현하려면, 우선  메소드 내의 코드를 구현한다.

user: 예제는?

gpt: ## C++
using namespace std;

enum RECORD_TYPE_en
 CAR,
 BIKE,
 PERSON

/**

class Record
 public :

 Record 

 virtual ~Record 

 virtual Record* Clone const=0;

 virtual void Print const=0;

/**

class CarRecord: public Record
 private :
 string m_oStrCarName;

 uint32_t m_ui32ID;

 public :

 CarRecord
 m_ui32ID

 CarRecord
 m_oStrCarName = _oCarRecord.m_oStrCarName;
 m_ui32ID = _oCarRecord.m_ui32ID;

 ~CarRecord 

 CarRecord* Clone const
 return new CarRecord;

 void Print const
 cout << "Car Record" << endl
 << "Name : " << m_oStrCarName << endl
 << "Number: " << m_ui32ID << endl << endl;

/**

class BikeRecord: public Record
 private :
 string m_oStrBikeName;

 uint32_t m_ui32ID;

 public :
 BikeRecord
 m_ui32ID

 BikeRecord
 m_oStrBikeName = _oBikeRecord.m_oStrBikeName;
 m_ui32ID = _oBikeRecord.m_ui32ID;

 ~BikeRecord 

 BikeRecord* Clone const
 return new BikeRecord;

 void Print const
 cout << "Bike Record" << endl
 << "Name : " << m_oStrBikeName << endl
 << "Number: " << m_ui32ID << endl << endl;

/**

class PersonRecord: public Record
 private :
 string m_oStrPersonName;

 uint32_t m_ui32Age;

 public :
 PersonRecord
 m_ui32Age

 PersonRecord
 m_oStrPersonName = _oPersonRecord.m_oStrPersonName;
 m_ui32Age = _oPersonRecord.m_ui32Age;

 ~PersonRecord 

 Record* Clone const
 return new PersonRecord;

 void Print const
 cout << "Person Record" << endl
 << "Name: " << m_oStrPersonName << endl
 << "Age : " << m_ui32Age << endl << endl ;

/**

class RecordFactory
 private :
 map m_oMapRecordReference;

 public :
 RecordFactory
 m_oMapRecordReference = new CarRecord;
 m_oMapRecordReference = new BikeRecord;
 m_oMapRecordReference = new PersonRecord;

 ~RecordFactory
 delete m_oMapRecordReference;
 delete m_oMapRecordReference;
 delete m_oMapRecordReference;

 Record* CreateRecord
 return m_oMapRecordReference->Clone;

int main
 RecordFactory* poRecordFactory = new RecordFactory;

 Record* poRecord;
 poRecord = poRecordFactory->CreateRecord;
 poRecord->Print;
 delete poRecord;

 poRecord = poRecordFactory->CreateRecord;
 poRecord->Print;
 delete poRecord;

 poRecord = poRecordFactory->CreateRecord;
 poRecord->Print;
 delete poRecord;

 delete poRecordFactory;
 return 0;

## C#
public enum RecordType
 Car,
 Person

/// 
public abstract class Record
 public abstract Record Clone;

/// 
public class PersonRecord: Record
 string name;
 int age;

 public override Record Clone
 return this.MemberwiseClone; // default shallow copy

/// 
public class CarRecord: Record
 string carname;
 Guid id;

 public override Record Clone
 CarRecord clone = this.MemberwiseClone; // default shallow copy
 clone.id = Guid.NewGuid; // always generate new id
 return clone;

/// 
public class RecordFactory
 private static Dictionary _prototypes =
 new Dictionary;

 /// 
 public RecordFactory
 _prototypes.Add;
 _prototypes.Add;

 /// 
 public Record CreateRecord
 return _prototypes.Clone;

## PHP
PHP 5에서는, 객체는 레퍼런스 형태로 전달된다.

값 형태로 전달하기 위해서는, "매직 펑션" __clone을 써야한다.
이러한 까닭으로 PHP 5에서는 프로토타입 패턴을 구현하기 매우 쉽다.

<?php
abstract class BookPrototype {
 protected $title;
 protected $topic;
 abstract function __clone;
 function getTitle {
 return $this->title;
 function setTitle {
 $this->title = $titleIn;
 function getTopic {
 return $this->topic;

class PHPBookPrototype extends BookPrototype {
 function __construct {
 $this->topic = 'PHP';
 function __clone {

class SQLBookPrototype extends BookPrototype {
 function __construct {
 $this->topic = 'SQL';
 function __clone {

 writeln;
 writeln;

 $phpProto = new PHPBookPrototype;
 $sqlProto = new SQLBookPrototype;

 $book1 = clone $sqlProto;
 $book1->setTitle;
 writeln;
 writeln;
 writeln;

 $book2 = clone $phpProto;
 $book2->setTitle;
 writeln;
 writeln;
 writeln;

 $book3 = clone $sqlProto;
 $book3->setTitle;
 writeln;
 writeln;
 writeln;

 writeln;

 function writeln {
 echo $line_in."";
?>

출력은 다음과 같다.