精华内容
下载资源
问答
  • Composite

    2019-05-09 14:47:00
    Purpose: To treat a group of objects the same way as a single instance of the object. RenderableInterface.php ...namespace DesignPatterns\Structural\Composite; interface Rende...

    Purpose:

    To treat a group of objects the same way as a single instance of the object.

    RenderableInterface.php

    <?php 
    
    namespace DesignPatterns\Structural\Composite;
    
    interface RenderableInterface
    {
        public function render(): string;
    }
    ?>
    

      

    Form.php

    <?php 
    
    namespace DesignPatterns\Structural\Composite;
    
    /**
     * The composite node MUST extend the component contract. This is mandatory for building
     * a tree of components.
     */
    class Form implements RenderableInterface
    {
        /**
            * Varibles Description
            *
            * @var RenderableInterface[]
        */
        private $elements;
    
        /**
         * runs through all elements and calls render() on them, then returns the complete representation
         * of the form.
         *
         * from the outside, one will not see this and the form will act like a single object instance
         *
         * @return string
         */
        public function render(): string
        {
            $formCode = '<form>';
    
            foreach ($this->elements as $element) {
                $formCode .= $element->render();
            }
    
            $formCode .= '</form>';
    
            return $formCode;
        }
    
        /**
            * @param RenderableInterface $element
            *
            * @return Void
        */
        public function addElement(RenderableInterface $element)
        {
            $this->elements[] = $element;
        }
    }
    
    ?>
    

     

    InputElement.php

    <?php 
    
    namespace DesignPatterns\Structural\Composite;
    
    /**
     * Input Element
     */
    class InputElement implements RenderableInterface
    {
        /**
            * Function Description
            *
            * @return string
        */
        public function render(): string
        {
            return '<input type="text" />';
        }
    }
    
    ?>
    

      

    TextElement.php

    <?php 
    
    namespace DesignPatterns\Structural\Composite;
    
    /**
     * Text Element
     */
    class TextElement implements RenderableInterface
    {
        /**
            * Varibles Description
            *
            * @var String
        */
        private $text;
    
        public function __construct(string $text)
        {
            $this->text = $text;
        }
    
        /**
            * Render
            *
            * @return string $text
        */
        public function render(): string
        {
            return $this->text;
        }
    }
    ?>
    

      

     

    Tests/CompositeTest.php

    <?php
    
    namespace DesignPatterns\Structural\Composite\Tests;
    
    use DesignPatterns\Structural\Composite;
    use PHPUnit\Framework\TestCase;
    
    class CompositeTest extends TestCase
    {
        public function testRender()
        {
            $form = new Composite\Form();
            $form->addElement(new Composite\TextElement('Email:'));
            $form->addElement(new Composite\InputElement());
            $embed = new Composite\Form();
            $embed->addElement(new Composite\TextElement('Password:'));
            $embed->addElement(new Composite\InputElement());
            $form->addElement($embed);
    
            // This is just an example, in a real world scenario it is important to remember that web browsers do not
            // currently support nested forms
    
            $this->assertEquals(
                '<form>Email:<input type="text" /><form>Password:<input type="text" /></form></form>',
                $form->render()
            );
        }
    }
    

      

     

    转载于:https://www.cnblogs.com/victorchen/p/10838402.html

    展开全文
  • Design Pattern - Composite(C#)

    万次阅读 多人点赞 2019-02-03 11:44:16
    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!... Definition Compose objects into tree structures to represent part-... Composite lets clients treat individua...

    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 

    Definition

    Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

    Participants

        The classes and/or objects participating in this pattern are:

    • Component (DrawingElement)
      • Declares the interface for objects in the composition.
      • Implements default behavior for the interface common to all classes, as appropriate.
      • Declares an interface for accessing and managing its child components.
      • (optional) Defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate.
    • Leaf (PrimitiveElement)
      • Represents leaf objects in the composition. A leaf has no children.
      • Defines behavior for primitive objects in the composition.
    • Composite (CompositeElement)
      • Defines behavior for components having children.
      • Stores child components.
      • Implements child-related operations in the Component interface.
    • Client (CompositeApp)
      • Manipulates objects in the composition through the Component interface.

    Sample Code in C#


    This structural code demonstrates the Composite pattern which allows the creation of a tree structure in which individual nodes are accessed uniformly whether they are leaf nodes or branch (composite) nodes.

    /*
     * Structural Composite Design Pattern.
     */
    
    namespace Composite.Sample
    {
        using System;
        using System.Collections.Generic;
    
        /// <summary>
        /// Startup class for Structural Composite Design Pattern.
        /// </summary>
        internal static class Program
        {
            #region Methods
    
            /// <summary>
            /// Entry point into console application.
            /// </summary>
            private static void Main()
            {
                // Create a tree structure.
                var root = new Composite("root");
                root.Add(new Leaf("Leaf A"));
                root.Add(new Leaf("Leaf B"));
                var comp = new Composite("Composite X");
                comp.Add(new Leaf("Leaf XA"));
                comp.Add(new Leaf("Leaf XB"));
                root.Add(comp);
                root.Add(new Leaf("Leaf C"));
    
                // Add and remove a leaf.
                var leaf = new Leaf("Leaf D");
                root.Add(leaf);
                root.Remove(leaf);
    
                // Recursively display tree.
                root.Display(1);
            }
    
            #endregion
        }
    
        /// <summary>
        /// The 'Component' abstract class.
        /// </summary>
        internal abstract class Component
        {
            #region Fields
    
            /// <summary>
            /// The name.
            /// </summary>
            protected readonly string Name;
    
            #endregion
    
            #region Constructors and Destructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="Component"/> class.
            /// </summary>
            /// <param name="name">
            /// The name.
            /// </param>
            protected Component(string name)
            {
                Name = name;
            }
    
            #endregion
    
            #region Public Methods and Operators
    
            /// <summary>
            /// Add.
            /// </summary>
            /// <param name="c">
            /// The component.
            /// </param>
            public abstract void Add(Component c);
    
            /// <summary>
            /// Display.
            /// </summary>
            /// <param name="depth">
            /// The depth.
            /// </param>
            public abstract void Display(int depth);
    
            /// <summary>
            /// Remove.
            /// </summary>
            /// <param name="c">
            /// The component.
            /// </param>
            public abstract void Remove(Component c);
    
            #endregion
        }
    
        /// <summary>
        /// The 'Composite' class.
        /// </summary>
        internal class Composite : Component
        {
            #region Fields
    
            /// <summary>
            /// The children.
            /// </summary>
            private readonly List<Component> _children = new List<Component>();
    
            #endregion
    
            #region Constructors and Destructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="Composite"/> class.
            /// </summary>
            /// <param name="name">
            /// The name.
            /// </param>
            public Composite(string name)
                : base(name)
            {
            }
    
            #endregion
    
            #region Public Methods and Operators
    
            /// <summary>
            /// Add.
            /// </summary>
            /// <param name="component">
            /// The component.
            /// </param>
            public override void Add(Component component)
            {
                _children.Add(component);
            }
    
            /// <summary>
            /// Display.
            /// </summary>
            /// <param name="depth">
            /// The depth.
            /// </param>
            public override void Display(int depth)
            {
                Console.WriteLine(new string('-', depth) + Name);
    
                // Recursively display child nodes
                foreach (Component component in _children)
                {
                    component.Display(depth + 2);
                }
            }
    
            /// <summary>
            /// Remove.
            /// </summary>
            /// <param name="component">
            /// The component.
            /// </param>
            public override void Remove(Component component)
            {
                _children.Remove(component);
            }
    
            #endregion
        }
    
        /// <summary>
        /// The 'Leaf' class.
        /// </summary>
        internal class Leaf : Component
        {
            #region Constructors and Destructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="Leaf"/> class.
            /// </summary>
            /// <param name="name">
            /// The name.
            /// </param>
            public Leaf(string name)
                : base(name)
            {
            }
    
            #endregion
    
            #region Public Methods and Operators
    
            /// <summary>
            /// Add.
            /// </summary>
            /// <param name="c">
            /// The component.
            /// </param>
            public override void Add(Component c)
            {
                Console.WriteLine("Cannot add to a leaf.");
            }
    
            /// <summary>
            /// Display.
            /// </summary>
            /// <param name="depth">
            /// The depth.
            /// </param>
            public override void Display(int depth)
            {
                Console.WriteLine(new string('-', depth) + Name);
            }
    
            /// <summary>
            /// Remove.
            /// </summary>
            /// <param name="c">
            /// The component.
            /// </param>
            public override void Remove(Component c)
            {
                Console.WriteLine("Cannot remove from a leaf.");
            }
    
            #endregion
        }
    }
    
    // Output:
    /*
    -root
    ---Leaf A
    ---Leaf B
    ---Composite X
    -----Leaf XA
    -----Leaf XB
    ---Leaf C
    
    */

    This real-world code demonstrates the Composite pattern used in building a graphical tree structure made up of primitive nodes (lines, circles, etc.) and composite nodes (groups of drawing elements that make up more complex elements).

    /*
     * Real-World Composite Design Pattern.
     */
    
    namespace Composite.RealWorld
    {
        using System;
        using System.Collections.Generic;
        
        /// <summary>
        /// Startup class for Real-World Composite Design Pattern.
        /// </summary>
        internal static class Program
        {
            #region Methods
    
            /// <summary>
            /// Entry point into console application.
            /// </summary>
            private static void Main()
            {
                // Create a tree structure.
                var root = new CompositeElement("Picture");
                root.Add(new PrimitiveElement("Red Line"));
                root.Add(new PrimitiveElement("Blue Circle"));
                root.Add(new PrimitiveElement("Green Box"));
    
                // Create a branch.
                var comp = new CompositeElement("Two Circles");
                comp.Add(new PrimitiveElement("Black Circle"));
                comp.Add(new PrimitiveElement("White Circle"));
                root.Add(comp);
    
                // Add and remove a PrimitiveElement.
                var pe = new PrimitiveElement("Yellow Line");
                root.Add(pe);
                root.Remove(pe);
    
                // Recursively display nodes.
                root.Display(1);
            }
    
            #endregion
        }
    
        /// <summary>
        /// The 'Component' tree node.
        /// </summary>
        internal abstract class DrawingElement
        {
            #region Fields
    
            /// <summary>
            /// The name.
            /// </summary>
            protected readonly string Name;
    
            #endregion
    
            #region Constructors and Destructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="DrawingElement"/> class.
            /// </summary>
            /// <param name="name">
            /// The name.
            /// </param>
            protected DrawingElement(string name)
            {
                Name = name;
            }
    
            #endregion
    
            #region Public Methods and Operators
    
            /// <summary>
            /// Add.
            /// </summary>
            /// <param name="d">
            /// The drawing element.
            /// </param>
            public abstract void Add(DrawingElement d);
    
            /// <summary>
            /// Display.
            /// </summary>
            /// <param name="indent">
            /// The indent.
            /// </param>
            public abstract void Display(int indent);
    
            /// <summary>
            /// Remove.
            /// </summary>
            /// <param name="d">
            /// The drawing element.
            /// </param>
            public abstract void Remove(DrawingElement d);
    
            #endregion
        }
    
        /// <summary>
        /// The 'Leaf' class.
        /// </summary>
        internal class PrimitiveElement : DrawingElement
        {
            #region Constructors and Destructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="PrimitiveElement"/> class.
            /// </summary>
            /// <param name="name">
            /// The name.
            /// </param>
            public PrimitiveElement(string name)
                : base(name)
            {
            }
    
            #endregion
    
            #region Public Methods and Operators
    
            /// <summary>
            /// Add.
            /// </summary>
            /// <param name="c">
            /// The drawing element.
            /// </param>
            public override void Add(DrawingElement c)
            {
                Console.WriteLine("Cannot add to a PrimitiveElement.");
            }
    
            /// <summary>
            /// Display.
            /// </summary>
            /// <param name="indent">
            /// The indent.
            /// </param>
            public override void Display(int indent)
            {
                Console.WriteLine(new string('-', indent) + " " + Name);
            }
    
            /// <summary>
            /// Remove.
            /// </summary>
            /// <param name="c">
            /// The drawing element.
            /// </param>
            public override void Remove(DrawingElement c)
            {
                Console.WriteLine("Cannot remove from a PrimitiveElement.");
            }
    
            #endregion
        }
    
        /// <summary>
        /// The 'Composite' class.
        /// </summary>
        internal class CompositeElement : DrawingElement
        {
            #region Fields
    
            /// <summary>
            /// The elements.
            /// </summary>
            private readonly List<DrawingElement> _elements = new List<DrawingElement>();
    
            #endregion
    
            #region Constructors and Destructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="CompositeElement"/> class.
            /// </summary>
            /// <param name="name">
            /// The name.
            /// </param>
            public CompositeElement(string name)
                : base(name)
            {
            }
    
            #endregion
    
            #region Public Methods and Operators
    
            /// <summary>
            /// Add.
            /// </summary>
            /// <param name="d">
            /// The drawing element.
            /// </param>
            public override void Add(DrawingElement d)
            {
                _elements.Add(d);
            }
    
            /// <summary>
            /// Display.
            /// </summary>
            /// <param name="indent">
            /// The indent.
            /// </param>
            public override void Display(int indent)
            {
                Console.WriteLine(new string('-', indent) + "+ " + Name);
    
                // Display each child element on this node.
                foreach (DrawingElement d in _elements)
                {
                    d.Display(indent + 2);
                }
            }
    
            /// <summary>
            /// Remove.
            /// </summary>
            /// <param name="d">
            /// The drawing element.
            /// </param>
            public override void Remove(DrawingElement d)
            {
                _elements.Remove(d);
            }
    
            #endregion
        }
    }
    
    // Output:
    /*
    -+ Picture
    --- Red Line
    --- Blue Circle
    --- Green Box
    ---+ Two Circles
    ----- Black Circle
    ----- White Circle
    
    */

     

    展开全文
  • composite

    千次阅读 2004-12-29 14:28:00
    定义名称: composite结构:意图:将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对单个对象和组合对象的使用具有一致性。适用性: 你想表示对象的部分-整体层次结构。 你希望用户忽略组合对象...

    定义

    名称:  composite

    结构:

    o_composite.jpg

    意图:

    将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对单个对象和组合对象的使用具有一致性。

    适用性:

    你想表示对象的部分-整体层次结构。 你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

    效果:



    经典案例

    1、Java AWT Component-Container
            毫无疑问的,AWT中的Component-Container体系就是一个很好的Composite模式的例子。Container继承于Component,而Container中有可以包含有多个Component,因为Container实际上也是Component,因而Container也可以包含Container。这样通过Component-Container结构的对象组合,形成一个树状的层次结构。这也就是Composite模式所要做的。
            Composite模式是为了简化编程而提出的,一般的在编程的时候,如果严格的区分Component和Container的话,有时候会带来许多不便,而且这些往往是没有必要的。比如,我要在一个Container中放置一个Component,我并不需要知道这个Component到底是一个Container,或者就是一个一般的Component,在父级容器中所要做的,只是记录一个Component的引用,在需要的时候调用Component的绘制方法来显示这个Component。当这个Component确实是一个Container的时候,它可以通过Container重载后的绘制方法,完成对这个容器的显示,并把绘制消息传递给到它的子对象去。也就是说,对一个父级容器而言,它并不不关心,其子对象到底是一个Component,还是一个Container。它需要将Component和Container统一对待。

    2、JUnit



    最佳实践

    1、Composite优缺点
            优点:对于Composite模式,也许人们一开始的注意力会集中在它是如何实现组合对象的。但Composite最重要之处在于用户并不关心是组合对象还是单个对象,用户将以统一的方式进行处理,所以基类应是从单个对象和组合对象中提出的公共接口。
      缺点:Composite最大的问题在于不容易限制组合中的组件。
            使用改进的composite模式可以缓和这个矛盾,见参考文章2。


    相关模式
    1、visitor / iterator
            在对composite中的元素进行集体操作或遍历操作时,经常使用visitor模式和iterator模式

    参考文章
    1、设计模式之Composite(组合)
            http://dev.csdn.net/article/52/52259.shtm
    2、从Java类库看设计模式 (Composite,Strategy,Iterator)
             http://www.sawin.com.cn/doc/SD/UML/classlib.htm
    3、诊断 Java 代码: 深度优先访问器和中断的分派
            http://www-900.ibm.com/developerWorks/cn/java/j-diag/part20/index.shtml

    展开全文
  • Composite Resources

    2020-12-26 04:08:59
    But, to assist someone who takes the initiative to add composite resource API calls to restforce, can you explain what files would be created and where ? And what would have to be added to existing ...
  • Composite-开源

    2021-06-08 16:42:56
    Composite 是一个 Java 包,它提供了 Composite 设计模式的标准实现。 该包简单且不干扰,并提供常见使用模式的便捷实现。
  • Composite commands

    2020-12-30 07:47:50
    I stumbled upon an <a href="https://gunnarpeipman.com/composite-command-pattern/">interesting article</a> yesterday about composite commands. Basically a composite command is a command container, each...
  • Composite 模式

    2019-10-31 08:40:40
    Composite 模式 能够使容器与内容具有一致性,创造出递归结构的模式就是Composite模式。 示例程序类图 示例程序 Entry public abstract class Entry { public abstract String getName(); // 获取名字 public ...

    Composite 模式

    能够使容器与内容具有一致性,创造出递归结构的模式就是Composite模式

    示例程序类图

    类图

    示例程序

    Entry

    public abstract class Entry {
        public abstract String getName();                               // 获取名字
        public abstract int getSize();                                  // 获取大小
        public Entry add(Entry entry) throws FileTreatmentException {   // 加入目录条目
            throw new FileTreatmentException();
        }
        public void printList() {                                       // 为一览加上前缀并显示目录条目一览
            printList("");
        }
        protected abstract void printList(String prefix);               // 为一览加上前缀
        @Override
        public String toString() {                                      // 显示代表类的文字
            return getName() + " (" + getSize() + ")";
        }
    }
    

    File

    public class File extends Entry {
        private String name;
        private int size;
        public File(String name, int size) {
            this.name = name;
            this.size = size;
        }
        @Override
        public String getName() {
            return name;
        }
        @Override
        public int getSize() {
            return size;
        }
        @Override
        protected void printList(String prefix) {
            System.out.println(prefix + "/" + this);
        }
    }
    

    Directory

    import java.util.Iterator;
    import java.util.ArrayList;
    
    public class Directory extends Entry {
        private String name;                    // 文件夹的名字
        private ArrayList directory = new ArrayList();      // 文件夹中目录条目的集合
        public Directory(String name) {         // 构造函数
            this.name = name;
        }
        @Override
        public String getName() {               // 获取名字
            return name;
        }
        @Override
        public int getSize() {                  // 获取大小
            int size = 0;
            Iterator it = directory.iterator();
            while (it.hasNext()) {
                Entry entry = (Entry)it.next();
                size += entry.getSize();
            }
            return size;
        }
        @Override
        public Entry add(Entry entry) {         // 增加目录条目
            directory.add(entry);
            return this;
        }
        @Override
        protected void printList(String prefix) {       // 显示目录条目一览
            System.out.println(prefix + "/" + this);
            Iterator it = directory.iterator();
            while (it.hasNext()) {
                Entry entry = (Entry)it.next();
                entry.printList(prefix + "/" + name);
            }
        }
    }
    

    FileTreatmentException

    public class FileTreatmentException extends RuntimeException {
        public FileTreatmentException() {
        }
        public FileTreatmentException(String msg) {
            super(msg);
        }
    }
    

    Main

    public class Main {
        public static void main(String[] args) {
            try {
                System.out.println("Making root entries...");
                Directory rootdir = new Directory("root");
                Directory bindir = new Directory("bin");
                Directory tmpdir = new Directory("tmp");
                Directory usrdir = new Directory("usr");
                rootdir.add(bindir);
                rootdir.add(tmpdir);
                rootdir.add(usrdir);
                bindir.add(new File("vi", 10000));
                bindir.add(new File("latex", 20000));
                rootdir.printList();
    
                System.out.println("");
                System.out.println("Making user entries...");
                Directory yuki = new Directory("yuki");
                Directory hanako = new Directory("hanako");
                Directory tomura = new Directory("tomura");
                usrdir.add(yuki);
                usrdir.add(hanako);
                usrdir.add(tomura);
                yuki.add(new File("diary.html", 100));
                yuki.add(new File("Composite.java", 200));
                hanako.add(new File("memo.tex", 300));
                tomura.add(new File("game.doc", 400));
                tomura.add(new File("junk.mail", 500));
                rootdir.printList();
            } catch (FileTreatmentException e) {
                e.printStackTrace();
            }
        }
    }
    

    Composite 模式中的登场角色

    1.Leaf(树叶)

    表示“内容”的角色。在该角色中不能放入其他对象。在示例程序中,由File类扮演此角色。

    2.Composite(复合物)

    表示容器的角色。可以在其中放入Leaf角色和Composite角色。在示例程序中,由Directory类扮演此角色。

    3.Component

    使Leaf角色和Composite角色具有一致性的角色。Component角色是Leaf角色和Composite角色的父类。在示例程序中,由Entry类扮演此角色。

    4.Client

    使用Composite模式的角色。在示例程序中,由Main类扮演此角色。

    通用类图

    通用类图

    注意点

    使用Composite模式可以使容器与内容具有一致性,也可以称其为多个和单个的一致性,即将多个对象结合在一起,当作一个对象进行处理。

    展开全文
  • USB Composite Device

    热门讨论 2013-02-10 02:55:14
    USB Composite Device
  • Composite Pattern

    2019-10-06 18:13:02
    Composite Pattern The Composite Patternallows you to compose objects into tree structures to represent part-whole hierarchies. Com...
  • Marionette CompositeView 演示 由于上的用户提出的问题而开始。 修复了与他有关 jsfiddle 的一件事,但注意到 Marionette & Backbone 的大量误用。 所以我希望这个例子能帮助很多人理解 CompositeView 作为树/叶...
  • Composite-Pattern-源码

    2021-03-28 04:49:18
    Composite-Pattern
  • composite模式

    2021-02-27 12:04:33
    composite模式:容器本身与内部元素具有一致性,例如文件与文件夹的关系。 Entry:文件和文件夹的抽象,使得file和directory具有一致性 File:文件 Directory:文件夹,可以存放文件
  • jwd-composite-源码

    2021-02-13 17:18:59
    jwd-composite
  • Composite C1源代码

    2021-03-15 21:13:16
    Composite C1 CMS是一个功能齐全的免费开放源码的网站内容管理系统,专注于企业网站的开发。是非常实用和面向任务的用户界面,使用户不同的技能来完成任务和合作,使用熟悉的工具和向导。使用Composite C1 CMS构建...
  • 回购(Marionette 的 CompositeView)
  • gadget之composite

    2021-03-17 22:28:10
    gadget之composite composite简介 composite英文意思为复合/综合。其为整个gadget驱动的中间层,往下注册usb_gadget_driver并与usb_udc(usb_gadget设备)绑定,往上通过usb_composite_dev与usb_composite_driver...
  • Composite模式

    2015-07-14 09:50:40
    在开发时,如果遇到使用递归构建树状的组合结构,那么可以考虑使用Composite模式。Composite模式将对象组合成树形结构,来表示部分、整体的层次结构。
  • A New Design of Composite Patch Repair for Composite Structures
  • am.composite-源码

    2021-07-14 19:54:17
    Composite 是应用程序的中级构建块。 它用于在自定义的 UI 部分中组合元素和其他组合。 它由标记中的自定义标记表示: <my></my> 这个自定义标签可以在最终标记中由它的模板替换。 我如何使用它? 将它用于页面...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 78,267
精华内容 31,306
关键字:

composite