For faster navigation, this Iframe is preloading the Wikiwand page for Накопичувач (шаблон проєктування).

Накопичувач (шаблон проєктування)

Матеріал з Вікіпедії — вільної енциклопедії.

Накопичувач (англ. Collecting Parameter) — шаблон проєктування, який пропонує накопичувати повідомлення про стан об'єкту.

Реалізація

[ред. | ред. код]

Нехай, необхідно показувати повідомлення про зміну стану об'єкту. Оскільки наперед не відомо середовище розгортання даного об'єкту, накопичуватимемо повідомлення в пам'яті, а відповідальність відображення реалізуємо в залежності від середовища.

typedef vector<string> CollectingParameter;

struct Filler
{
    void f(CollectingParameter& cp)
    {
        cp.push_back("accumulating");
        // Do something else
    }
    
    void g(CollectingParameter& cp)
    {
        cp.push_back("items");
        // Do something else
    }
    
    void h(CollectingParameter& cp)
    {
        cp.push_back("as we go");
        // Do something else
    }
};

void main()
{
    Filler filler;
    CollectingParameter cp;
    filler.f(cp);
    filler.g(cp);
    filler.h(cp);
    
    // вивід повідомлень залежить від середовища, наприклад, у консоль
    // вебсервер міг би показувати дані у вигляді сторінки, тощо
    for (vector<string>::iterator it = cp.begin(); it != cp.end(); ++it)
    {
        cout << *it << ' ';
    }
}

Даний шаблон може бути корисний, коли необхідно виконати відкладену валідацію.

public class HomeController
{
    public ActionResult Index(string value)
    {
        if(string.IsNullOrEmpty(value))
        {
            ModelState.AddModelError("value", "There is something wrong with value.");
        }
        
        . . .
        IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
        . . .
    }
}

Одна із різновидностей цього шаблону передбачає для накопичення повідомлень використовувати властивості, а не структуру даних.

Див. також

[ред. | ред. код]

Джерела

[ред. | ред. код]
{{bottomLinkPreText}} {{bottomLinkText}}
Накопичувач (шаблон проєктування)
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?