------ decorative pattern design patterns

In the case of decorative patterns without having to change the original class files and use inheritance to an object dynamically add some extra features.

It is true to wrap objects by creating a wrapper object. Decorative pattern is more flexible than a subclass.

A role:

(1) abstraction means (Component) Roles: defining an object interface that can be dynamically added to the responsibility of these objects.

(2) detailed component (ConcreteComponent) role: a detailed definition of the object, it is possible to add some responsibilities to this object.

(3) decoration (Decorator) Role: Decorative abstract class that inherits Component. From an outer class to extend the functionality of the Component class, but it Component. There is no need to know the Decorator.

(4) detailed decoration (ConcreteDecorator) role: detailed decorative objects, play a role in adding to the Component function.

Schematic:



For Example: Now the popular cross-stitch hanging on the wall at home, which is what we hang Component good cross-stitch shown. ConcreteComponent wire embroidered with the cloth. Decorator stitch bezel is installed; ConcreteDecoratorA is framed around the bezel. ConcreteDecoratorB a glass plate; these parts are hanging on the wall.

This time clear now! And learning to live in close contact.

Second match the scene:

1. a need to extend the functionality class, to a class or to add additional functions.

2. The need to dynamically join a target function. These functions can then dynamically revoked.

3. The need to add a very large number of permutations and combinations of the functions performed by some of the basic functions is generated, so that the inheritance becomes impractical.

4. When the process can not be employed to generate augmented subclass. In one case, there may be a large number of separate extension. To support every combination will produce a large number of sub-categories, so that the explosive growth in the number of subclasses. Another situation may be due to the class definitions are hidden class or subclass definition can not be used to generate.

Three advantages and disadvantages:

Advantages:

1. Design and mode of inheritance of the goal is to expand the object function. But design patterns to provide flexibility than many other inherited.

2. By using different permutations and decorative details of the decoration. Designers can create so many different combinations of behaviors.

Disadvantages:

1. This is more flexible than inherited characteristics, but also the same time means that more and more complexity.

2. The decorative pattern can lead to many subcategories appear design. Assume excessive use, the program will become very complicated.

3. The decorative pattern is programmed for the type of abstract component (Component).

but. Suppose you want to time for the detailed components of programming, they should think again, your application architecture, and decorators are appropriate. Of course, it is possible to change the Component interface, adds new public behavior. Achieve "translucent," the decorator pattern.

In a real project you want to make the best choice.

Four design principles:

1. The multi-purpose compositions, less inheritance.

Use design subclass inherits behavior. It is statically determined at compile time, and all subclasses will inherit the same behavior. However, using a combination approach can be assumed that expansion behavior of the object, it is possible to dynamically expand when executed.

2. The class should be designed to be open for extension, closed for modification.

3. Optimization mode: Suppose there is only one class with no abstract ConcreteComponent when Component interfaces, allowing Decorator inherit Concrete Component. Suppose there is only one class ConcreteDecorator, Decorator Concrete Decorator and can be combined.

V. demonstrates:

Written with different costumes giving programs

1. Unused decorative patterns

(1) Code Structure:


(2) Code :

person categories:

 class Person
    {
        private string name;
        public Person(string name)
        {
            this.name = name;
        }
        public void Show()
        {
            Console.WriteLine("装扮的{0}", name);
        }
    }
Clothing abstract class:

 abstract class Finery
    {
        public abstract void Show();
    }
A variety of costumes subclasses:

 //大T恤
    class TShirts : Finery
    {
        public override void Show()
        {
            Console.Write("大T恤 ");
        }
    }
    //垮裤
     ...
    //皮鞋
    ...
client:
static void Main(string[] args)
        {
            Person xc = new Person("小菜");

            Console.WriteLine("\n第一种装扮:");
            Finery dtx = new TShirts();
            Finery kk = new BigTrouser();
            Finery pqx = new Sneakers();

            dtx.Show();
            kk.Show();
            pqx.Show();
            xc.Show();

            Console.WriteLine("\n另外一种装扮:");
            ...
            Console.Read();
        }

(3) evaluation: "apparel" and "humanity" separated, but apparel is decorated in humans, the two are related. client code appears discrete. Low cohesion, must be assembled inside. The desired function in the correct order in series controlled.


2. Use decorative pattern

(1) Code Structure:

(2) Code:

Person class

class Person
    {
        public Person()
        { }
        private string name;
        public Person(string name)
        {
            this.name = name;
        }
        public virtual void Show()
        {
            Console.WriteLine("装扮的{0}", name);
        }
    }
Apparel categories:

class Finery : Person
    {
        protected Person component;

        //打扮
        public void Decorate(Person component)
        {
            this.component = component;
        }
        public override void Show()
        {
            if (component != null)
            {
                component.Show();
            }
        }
    }
Details apparel categories:

class TShirts : Finery
    {
        public override void Show()
        {
            Console.Write("大T恤 ");
            base.Show();
        }
    }
//其余类相似
    class BigTrouser : Finery
    ...
    class Sneakers : Finery
    ...
    class Suit : Finery
    ...
client:

static void Main(string[] args)
        {
            Person xc = new Person("小菜");

            Console.WriteLine("\n第一种装扮:");

            Sneakers pqx = new Sneakers();
            BigTrouser kk = new BigTrouser();
            TShirts dtx = new TShirts();
       //装饰过程
            pqx.Decorate(xc);
            kk.Decorate(pqx);
            dtx.Decorate(kk);
            dtx.Show();

            Console.WriteLine("\n另外一种装扮:");
            ...
            Console.Read();
        }
    }
Renderings:



Rating: use of decorative patterns, to separate the functions of the dress in person from the person in the class, in compliance with the principle of opening and closing, simplifying the original class, effectively separating the core duties and decorative ribbon class. Class repeatedly removed decorative logic.

The decoration of each function to be placed in a separate category, and let the packaging it to be modified objects. Such custom code can be executed based upon the need to selectively, sequentially using the object to be packaged decorative function.







Guess you like

Origin www.cnblogs.com/mqxnongmin/p/10927768.html