First contact decorator

Preface

A few days ago, my teacher used an ornament when optimizing and refactoring my code. I felt that the ornament was a useful thing, and it was also involved in the webmaster's blog, so here's my initial understanding of the ornament.

analogy

I see a good example on the web.Underpants can be used to cover up our shame, but in winter we can't protect ourselves from the wind and the cold. Smart people invented pants so that they won't be cold with them. Decorators, like what we call trousers, provide us with warmth without affecting the function of shorts. That is, they provide us with new functions without affecting the function of the original function.
The student also likened the decorator very well. He likened the decorator to a waiter in a coffee shop. I thought the coffee was a bit bitter. The waiter took the coffee away and brought me coffee and sugar.The links are as follows https://segmentfault.com/a/1190000019148468
Simply put, the decorator adds new functionality without affecting the original function.

Why do we have ornaments?

Decorators are also a function in nature, and we know that a good code should be closed for modifications and open for extensions, which means decorators.Decorators allow other functions to add additional functionality without requiring any code changes. They can be used in scenarios such as log insertion, performance testing, transaction processing, caching, permission checking, and so on. With decorators, we can pull out a lot of identical code that is not related to the function itself and continue to reuse it.

Give an example

As an example of a program I wrote, ListView is a style 2 of c#and we need to
The top of the ListView that added our header was written this way

this.listView1.Columns.Add("School Number", 100, HorizontalAlignment.Center);
            this.listView1.Columns.Add("Full name", 100, HorizontalAlignment.Center);
            this.listView1.Columns.Add("Mathematics", 100, HorizontalAlignment.Center);
            this.listView1.Columns.Add("English", 100, HorizontalAlignment.Center);
            this.listView1.Columns.Add("Politics", 100, HorizontalAlignment.Center);
            this.listView1.Columns.Add("Total score", 100, HorizontalAlignment.Center);
            this.listView1.Columns.Add("Average", 100, HorizontalAlignment.Center);
            this.listView1.Columns.Add("Rank", 100, HorizontalAlignment.Center);
            this.listView1.View = System.Windows.Forms.View.Details;
        }

The effect is

We found that the same function Columns.Add, Columns.Add used in the above code needs to pass in three variables, but we found that the last two parameters are the same, only the first parameter changes, so we can decorate the original function

class YzListView
    {
        private ListView listView;

        private int width = 100;

        private HorizontalAlignment align = HorizontalAlignment.Center;

        public YzListView(ListView listView) {
            this.listView = listView;
        }

        public YzListView Add(string name) {
            return this.Add(name, this.width, this.align);
        }

        public YzListView Add(string name, int width) {
            return this.Add(name, width, this.align);
        }

        public YzListView Add(string name, int width, HorizontalAlignment align){
            this.listView.Columns.Add(name, width, align);
            return this;
        }
    }

Because the code of the ListView class cannot be modified, we define a new class, named YzListView, to pass in and receive the ListView in the constructor, and to decorate the Columns.Add function in the ListVIew class.We then decorate the Columns.Add function in the ListVIew class, decorate the Columns.Add function by calling the name parameter passed in from the newly defined Add function one level at a time, and finally calling the Columns.Add function.

epilogue

In practice, I can understand knowledge more deeply. When I write code and think about using decorators, I really understand decorators.

Tags: C# Windows

Posted on Sat, 09 Nov 2019 00:08:21 -0500 by reneeshtk