Svoboda | Graniru | BBC Russia | Golosameriki | Facebook
Transfiguration pending
Jump to content

Կազմող (նախագծման ձևանմուշ)

Վիքիպեդիայից՝ ազատ հանրագիտարանից
Անվան այլ կիրառումների համար տե՛ս՝ Կազմող (այլ կիրառումներ)
Կազմող
ՏեսակԿառուցվածքային
ՆշանակությունՏվյալները ծառային ստրուկտուրայում միվորելու համար։
Կառուցվածք
ԿիրառությունԵրբ տարբերություն չպետք է լինի մի օբյեկտ կամ օբյեկտների խումբ կանչելու մեջ։
Նկարագրությունը ԳօՖի
"Design Patterns" գրքում
Այո

Կազմող (անգլ.՝ Composite), կառուցվածքային նախագծման ձևանմուշ, որը տվյալները միավորում է ծառային ստրուկտուրայի մեջ՝ հիերարխիա կազմելով եզակիից դեպի ընդհանուրը։ Կազմող ձևանմուշը թույլ է տալիս նույն կերպ դիմել ինչպես առանձին օբյեկտին, այնպես էլ օբյեկտների խմբերին[1]։

Ընդհանուր հասկացողություններ[խմբագրել | խմբագրել կոդը]

  • Client - հայցող
  • Instance - նմուշ
  • Implementation - իրականացում
  • Product - արգասիք

Նպատակ[խմբագրել | խմբագրել կոդը]

Ձևանմուշը բնորոշում է դասերի հիերարխիա, որոնք միևնույն ժամանակ կարող են բաղկացած լինել պրիմիտիվ և բարդ օբյեկտներ։ Հեշտացնում է հայցողի ճարտարապետությունը և ավելի պարզ է դարձնում նոր տիպի օբյեկտների ավելացումը։

Կիրառություն[խմբագրել | խմբագրել կոդը]

Կազմող ձևանմուշը խորհուրդ է տրվում օգտագործել, երբ հայցողի համար տարբերություն չկա օբյեկտների խումբ կամ եզակի օբյեկտ կազմելու համար[1]։ Կարելի է օգտագործել, եթե ծրագրավորողը տարբերություն չի դնում պարզ և բարդ օբյեկտներ ստեղծելու մեջ։

Կառուցվածք[խմբագրել | խմբագրել կոդը]

Կազմող ձևանմուշ-ի UML դիագրամը
Component
  • բաղկացուցիչ մասերի աբստրակցիա
  • հայտարարում է ինտերֆեյս օբյեկտների կոմպոզիցիայում
  • (ոչ պարտադիր) ռեկուրսիվ կառուցվածով ինտերֆեյս է տրամադրում բաղկացուցիչ մասի մայր օբյեկտին և, եթե հարկ է, այն իրականացնում է
Leaf
  • ներկայացնում է դուստր օբյեկտները կոմպոզիցայում։
  • իրականացնում է բոլոր Component-ների մեթոդները
Composite
  • ներկայացնում է Component-ի բաղադրիչը (կոմպոնետն ունի դուստր օբյեկտ)
  • սահմանում է մեթոդներ դուստր օբյեկտների ղեկավարելու համար
  • սահմանում է Component-ի բոլոր մեթոդները, գլխավորապես նրանք հղում է դուստրերին
Կազմող ձևանմուշը LePUS3-ում

Օրինակներ[խմբագրել | խմբագրել կոդը]

Java[խմբագրել | խմբագրել կոդը]

/** "Component" */
interface Graphic {

    //Prints the graphic.
    public void print();
}

/** "Composite" */
import java.util.List;
import java.util.ArrayList;
class CompositeGraphic implements Graphic {

    //Collection of child graphics.
    private List<Graphic> childGraphics = new ArrayList<Graphic>();

    //Prints the graphic.
    public void print() {
        for (Graphic graphic : childGraphics) {
            graphic.print();
        }
    }

    //Adds the graphic to the composition.
    public void add(Graphic graphic) {
        childGraphics.add(graphic);
    }

    //Removes the graphic from the composition.
    public void remove(Graphic graphic) {
        childGraphics.remove(graphic);
    }
}

/** "Leaf" */
class Ellipse implements Graphic {

    //Prints the graphic.
    public void print() {
        System.out.println("Ellipse");
    }
}

/** Client */
public class Program {

    public static void main(String[] args) {
        //Initialize four ellipses
        Ellipse ellipse1 = new Ellipse();
        Ellipse ellipse2 = new Ellipse();
        Ellipse ellipse3 = new Ellipse();
        Ellipse ellipse4 = new Ellipse();

        //Initialize three composite graphics
        CompositeGraphic graphic = new CompositeGraphic();
        CompositeGraphic graphic1 = new CompositeGraphic();
        CompositeGraphic graphic2 = new CompositeGraphic();

        //Composes the graphics
        graphic1.add(ellipse1);
        graphic1.add(ellipse2);
        graphic1.add(ellipse3);

        graphic2.add(ellipse4);

        graphic.add(graphic1);
        graphic.add(graphic2);

        //Prints the complete graphic (four times the string "Ellipse").
        graphic.print();
    }
}}

C#[խմբագրել | խմբագրել կոդը]

The following example, written in C#.

namespace CompositePattern
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    //Client
    class Program
    {
        static void Main(string[] args)
        {
            // initialize variables
            var compositeGraphic = new CompositeGraphic();
            var compositeGraphic1 = new CompositeGraphic();
            var compositeGraphic2 = new CompositeGraphic();

            //Add 1 Graphic to compositeGraphic1
            compositeGraphic1.Add(new Ellipse());

            //Add 2 Graphic to compositeGraphic2
            compositeGraphic2.AddRange(new Ellipse(), 
                new Ellipse());

            /*Add 1 Graphic, compositeGraphic1, and 
              compositeGraphic2 to compositeGraphic */
            compositeGraphic.AddRange(new Ellipse(), 
                compositeGraphic1, 
                compositeGraphic2);

            /*Prints the complete graphic 
            (four times the string "Ellipse").*/
            compositeGraphic.Print();
            Console.ReadLine();
        }
    }
    //Component
    public interface IGraphic
    {
        void Print();
    }
    //Leaf
    public class Ellipse : IGraphic
    {
        //Prints the graphic
    	public void Print()
        {
            Console.WriteLine("Ellipse");
        }
    }
    //Composite
    public class CompositeGraphic : IGraphic
    {
        //Collection of Graphics.
        private readonly List<IGraphic> graphics;

        //Constructor 
        public CompositeGraphic()
        {
            //initialize generic Collection(Composition)
            graphics = new List<IGraphic>();
        }
        //Adds the graphic to the composition
        public void Add(IGraphic graphic)
        {
            graphics.Add(graphic);
        }
        //Adds multiple graphics to the composition
        public void AddRange(params IGraphic[] graphic)
        {
            graphics.AddRange(graphic);
        }
        //Removes the graphic from the composition
        public void Delete(IGraphic graphic)
        {
            graphics.Remove(graphic);
        }
        //Prints the graphic.
        public void Print()
        {
            foreach (var childGraphic in graphics)
            {
                childGraphic.Print();
            }
        }
    }
}

Պարզագույն օրինակ[խմբագրել | խմբագրել կոդը]

//IVSR: Composite example
    /// Treats elements as composition of one or more elements, so that components can be separated
    /// between one another
    public interface IComposite
    {
        void CompositeMethod();
    }

    public class LeafComposite :IComposite 
    {

        #region IComposite Members

        public void CompositeMethod()
        {
            //To Do something
        }

        #endregion
    }

    /// Elements from IComposite can be separated from others 
    public class NormalComposite : IComposite
    {

        #region IComposite Members

        public void CompositeMethod()
        {
            //To Do Something
        }

        #endregion

        public void DoSomethingMore()
        {
            //Do Something more .
        }
    }

Ծանոթագրություններ[խմբագրել | խմբագրել կոդը]

  1. 1,0 1,1 Gamma, Erich; Richard Helm; Ralph Johnson; John M. Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. էջեր 395. ISBN 0-201-63361-2.