官术网_书友最值得收藏!

Components in action

Enough of theory! Let's build a component tree based on the class hierarchy illustrated in the preceding diagram. This way, we will demonstrate how we can take advantage of the composite pattern for building a user interface using simplified syntax. We will take a look at a similar example in the context of Angular in Chapter 5, Getting Started with Angular Components and Directives:

Composite c1 = new Composite(); 
Composite c2 = new Composite(); 
Composite c3 = new Composite(); 
 
c1.components.push(c2); 
c1.components.push(c3); 
 
Leaf l1 = new Leaf(); 
Leaf l2 = new Leaf(); 
Leaf l3 = new Leaf(); 
 
c2.components.push(l1); 
c2.components.push(l2);  
c3.components.push(l3); 

The preceding pseudocode creates three instances of the Composite class and three instances of the Leaf class. The c1 instance holds references to c2 and c3 inside the components list. The c2 instance holds references to l1 and l2, and c3 holds reference to l3:

Figure 6

The preceding diagram is a graphical representation of the component tree we built in the snippet. This is a simplified version of what the view in the modern JavaScript frameworks looks like. However, it illustrates the very basics of how we can compose directives and components. For instance, in the context of Angular, we can think of directives as instances of the Leaf class (since they don't own view and, thus, cannot compose other directives and components) and components as instances of the Composite class.

If we think more abstractly for the user interface in AngularJS, we can notice that we used quite a similar approach. The templates of our views compose different directives together in order to deliver a fully functional user interface to the end user of our application.

主站蜘蛛池模板: 青州市| 阿拉善左旗| 三明市| 芒康县| 太湖县| 内丘县| 鄂伦春自治旗| 蚌埠市| 五大连池市| 延津县| 营山县| 襄城县| 荥阳市| 澄城县| 华宁县| 江西省| 万州区| 奉贤区| 阿巴嘎旗| 辽中县| 黔西县| 樟树市| 四子王旗| 陕西省| 宜黄县| 泽库县| 贵港市| 中西区| 项城市| 临泽县| 会同县| 郎溪县| 平顶山市| 海淀区| 遂川县| 谢通门县| 泰和县| 镇雄县| 武川县| 桃源县| 娱乐|