소개하다
복합재는 객체를 트리 구조로 결합하여 "부분적"계층을 나타냅니다. 조합 모드는 사용자가 개별 객체 및 결합 된 객체의 사용과 일치하게 만듭니다.
일반적인 시나리오에는 ASP.NET의 제어 메커니즘 (즉, 제어에는 어린이 제어를 포함 할 수 있으며, 이는 자식 제어를 재귀 적으로 작동, 추가 및 삭제할 수 있음)가 포함되며, 마찬가지로 DOM 메커니즘이 있습니다. DOM 노드에는 자식 노드가 포함될 수 있습니다. 상위 노드이든 하위 노드이든, 자식 노드를 추가, 삭제 및 가로 지르는 일반적인 기능이 있습니다. 따라서 조합 패턴의 핵심은 아동 요소와 부모 요소를 모두 나타내는 추상 클래스를 갖는 것입니다.
텍스트
예를 들어, 다양한 요리를 제공하는 식당이 있습니다. 각 테이블에는 메뉴가 있습니다. 메뉴에는 아침 식사 페이스트리, 점심, 저녁 식사 등을 포함하여 식당이 모든 요리를 나열합니다. 각 식사에는 다양한 메뉴 항목이 있습니다. 메뉴 항목과 전체 메뉴를 모두 인쇄해야한다고 가정하고 새로운 요리와 같은 하위 항목을 추가 할 수 있으며 점심을 위해 설탕을 추가 할 수 있으며 커피와 같은 메뉴 항목에는 설탕을 추가 할 수 있습니다.
이 경우 조합을 사용하여 이러한 내용을 계층으로 표현할 수 있습니다. 구현 단계를 하나씩 분류합시다.
첫 번째 단계는 "추상 클래스"기능 메뉴 컴포넌트를 구현하는 것입니다.
코드 사본은 다음과 같습니다.
var menucomponent = function () {
};
menucomponent.prototype.getName = function () {
새 오류를 던지십시오 ( "이 방법을 다시 작성해야합니다!");
};
menucomponent.prototype.getDescription = function () {
새 오류를 던지십시오 ( "이 방법을 다시 작성해야합니다!");
};
menucomponent.prototype.getPrice = function () {
새 오류를 던지십시오 ( "이 방법을 다시 작성해야합니다!");
};
menucomponent.prototype.isvegetarian = function () {
새 오류를 던지십시오 ( "이 방법을 다시 작성해야합니다!");
};
menucomponent.prototype.print = function () {
새 오류를 던지십시오 ( "이 방법을 다시 작성해야합니다!");
};
menucomponent.prototype.add = function () {
새 오류를 던지십시오 ( "이 방법을 다시 작성해야합니다!");
};
menucomponent.prototype.remove = function () {
새 오류를 던지십시오 ( "이 방법을 다시 작성해야합니다!");
};
menucomponent.prototype.getchild = function () {
새 오류를 던지십시오 ( "이 방법을 다시 작성해야합니다!");
};
이 기능은 두 가지 유형의 방법을 제공합니다. 하나는 가격, 이름 등과 같은 정보를 얻는 것입니다. 다른 하나는 인쇄, 추가, 삭제 및 하위 메뉴 획득과 같은 일반적인 작업 방법입니다.
두 번째 단계는 기본 요리를 만드는 것입니다.
코드 사본은 다음과 같습니다.
var menuitem = function (sname, sdescription, bvegetarian, nprice) {
menucomponent.apply (this);
this.sname = sname;
this.sdescription = sdescription;
this.bvegetarian = bvegetarian;
this.nprice = nprice;
};
menuitem.prototype = new menucomponent ();
menuitem.prototype.getName = function () {
이 is.sname;
};
menuitem.prototype.getDescription = function () {
이를 반환하십시오 .Sdescription;
};
menuitem.prototype.getPrice = function () {
this.nprice;
};
menuitem.prototype.isvegetarian = function () {
이것을 반환하십시오.
};
menuitem.prototype.print = function () {
console.log (this.getName () + ":" + this.getDescription () + "," + this.getPrice () + "Euros");
};
코드에서 볼 수 있듯이 정보 및 인쇄 방법을 얻기위한 4 가지 방법 만 다시 보호되었으며 기본 요리에는 하위 디쉬를 추가, 삭제 및 얻는 방법이 포함되어 있지 않기 때문에 다른 3 가지 작동 방법에 과부하가 걸리지 않았습니다.
3 단계 : 요리 만들기 :
코드 사본은 다음과 같습니다.
var menu = function (sname, sdescription) {
menucomponent.apply (this);
this.amenucomponents = [];
this.sname = sname;
this.sdescription = sdescription;
this.createiterator = function () {
새 오류를 던지십시오 ( "이 방법은 덮어 써야합니다!");
};
};
menu.prototype = new menucomponent ();
menu.prototype.add = function (omenucomponent) {
// 대체물을 추가합니다
this.amenucomponents.push (omenucomponent);
};
menu.prototype.remove = function (omenucomponent) {
// 대체물을 삭제합니다
var amenuitems = [];
var nmenuitem = 0;
var nlenmenuitems = this.amenucomponents.length;
var oitem = null;
for (; nmenuitem <nlenmenuitems;) {
oitem = this.amenucomponents [nmenuitem];
if (oitem! == omenucomponent) {
amenuitems.push (oitem);
}
nmenuitem = nmenuitem + 1;
}
this.amenucomponents = alenuitems;
};
menu.prototype.getchild = function (nindex) {
// 지정된 대체를 얻습니다
이를 반환하십시오. Amenucomponents [nindex];
};
menu.prototype.getName = function () {
이 is.sname;
};
menu.prototype.getDescription = function () {
이를 반환하십시오 .Sdescription;
};
menu.prototype.print = function () {
// 현재 요리와 모든 하위 디쉬를 인쇄합니다
console.log (this.getName () + ":" + this.getDescription ());
Console.Log ( "----------------------------------------------------------------------------------------------------------------
var nmenucomponent = 0;
var nlenmenucomponents = this.amenucomponents.length;
var omenucomponent = null;
for (; nmenucomponent <nlenmenucomponents;) {
omenucomponent = this.amenucomponents [nmenucomponent];
omenucomponent.print ();
nmenucomponent = nmenucomponent + 1;
}
};
인쇄 인쇄 방법은 추가, 삭제 및 획득 방법을 구현하는 것 외에도 먼저 현재 접시 정보를 인쇄 한 다음 모든 하위 디스의 정보의 인쇄를 통해 반복하는 것입니다.
4 단계 : 지정된 접시 만들기 :
우리는 저녁 식사, 커피, 패스트리 등과 같은 몇 가지 실제 요리를 만들 수 있습니다. 모두 메뉴를 프로토 타입으로 사용하며 코드는 다음과 같습니다.
코드 사본은 다음과 같습니다.
var dinnermenu = function () {
MENU.APPLY (this);
};
DinustMenu.prototype = 새 메뉴 ();
var cafemenu = function () {
MENU.APPLY (this);
};
cafemenu.prototype = 새 메뉴 ();
var pancakehousemenu = function () {
MENU.APPLY (this);
};
pancakehousemenu.prototype = new Menu ();
5 단계 : 상단 메뉴 생성 - 메뉴 책 :
코드 사본은 다음과 같습니다.
var mattress = function (amenus) {
this.amenus = 아메 누스;
};
mattress.prototype.printmenu = function () {
this.amenus.print ();
};
이 기능은 메뉴 배열을 매개 변수로 사용하며 값은 모든 메뉴 내용을 인쇄하기위한 PrintMenu 메소드를 제공합니다.
6 단계, 통화 방법 :
코드 사본은 다음과 같습니다.
var opancakehousemenu = 새 메뉴 ( "팬케이크 하우스 메뉴", "아침 식사");
var odinnermenu = 새 메뉴 ( "저녁 식사 메뉴", "점심");
var ocoffeemenu = 새 메뉴 ( "카페 메뉴", "저녁");
var oallmenus = 새 메뉴 ( "모든 메뉴", "모든 메뉴 결합");
oallmenus.add (Opancakehousemenu);
OALLMENUS.ADD (ODINNERMENU);
Odinnermenu.add (New Menuitem ( "파스타", "마리 나라 소스를 곁들인 스파게티 및 효모 빵 한 조각", True, 3.89));
Odinnermenu.add (Ocoffeemenu);
Ocoffeemenu.add (New Menuitem ( "Express", "Coffee From Machine", False, 0.99));
var omattress = 새로운 매트리스 (oallmenus);
Console.log ( "------------------------------------------------------------------------------------------------------------------------------
omattress.printmenu ();
Console.log ( "------------------------------------------------------------------------------------------------------------------------------
ASP.NET Control Development에 익숙한 학생들이 익숙해 보입니까?
요약
조합 모드의 사용 시나리오는 매우 명확합니다.
객체의 일부를 나타내려면 - 전체 계층;
사용자가 결합 된 객체와 단일 객체의 차이를 무시하기를 원하며 사용자는 결합 된 구조의 모든 객체를 균일하게 사용합니다 (메소드).
또한이 패턴은 종종 데코레이터와 함께 사용됩니다. 일반적으로 공통 상위 클래스 (즉, 프로토 타입)가 있으므로 장식은 추가, 제거 및 GetChild 작업으로 구성 요소 인터페이스를 지원해야합니다.