Deeply decorating

xiaoxiao2021-03-06  18

First, the primer

Decorative mode? Definitely let you think of a black and fire home decoration. In fact, both in the truth still have a lot of similar places. Family decoration is nothing more than to cover up the wall of the original, unfair, and put a layer of inexperous paint, let life a little color. The wall is still the wall, his essence has not changed, but it is only a layer of coat.

What is the decorative pattern in the design pattern?

Second, definition and structure

Decorative mode is also called wrapper. GOF is defined in the "Design Mode" book as: dynamically adds additional duties to an object. In terms of increased function, Decorator mode is more flexible compared to generated subclasses.

Let us understand this sentence. Let's design the "door" class. Suppose you as the "door" class as follows:

Now, in one place in the system, you need a DOOR that can alarm, how do you do it? You may write a Sub-class ALARMDoor, add a sub-class unique method ALARM (). Well, where you use alarms you must let customers know that the alarm is used, otherwise this unique approach cannot be used. Moreover, this also violates the LISKOV replacement principles. Maybe you have to say, then add this method to DOR, this is not unified? But all doors must have alarm, at least a "dumb" alert. And when your system uses a warning in one or two places, this is obviously unreasonable - although the default adapter can be used to make up. At this time, you can consider adding additional features to your door dynamics. Let's take a look at the composition of decorative model, don't worry about solving the above problems, I will understand it under the following! 1) Abstract component role (Component): Defines an abstract interface to standardize an object to receive additional responsibility. 2) Concrete Component: This is a decorator that defines a class that will be decorated to increase the function. 3) Decorative role (Decorator): Holding an example of a component object and defines an interface of an abstract component definition. 4) Concrete Decorator: Responsible for adding a added function to the component. Look at the category of the decorative mode: ConcreteComponent in the figure may inherit other systems, and he also implements the Component interface in order to achieve decorative mode. The structure of the entire decorative mode is implemented in combination mode, but the purpose of attention is completely different (regarding the differences on the two, please pay attention to my future article). Third, for example, this example is from the JUnit that I have recently studied. If you don't understand JUnit, you can refer to "JUnit Getting Started", "JUnit Source Code Analysis (1)", "JUnit Source Code", "JUnit Source code analysis (3) ". It is worthy of Erich Gamma, one of GOF, and small things use n types of patterns in it. Let's take a look at the decorative mode in JUnit. In JUnit, TestCase is a very important class that allows for functional extensions. In the JUnit.extensions package, TestDecorator, RepeatedTest is an extension of TestCase's decorative mode. Below we put them and the role on the top of the top.

Oh, look at the source code, this is the most direct! // This is an abstract component role public interface test {/ ** * counter the number of test. * / Public abstract int counttest. * / Public abstract int counttestcases (); / ** * Runs a test and collects ITS Result in a TestResult instance * / public abstract void run (TestResult result);.} // particular component object, but this is an abstract class public abstract class TestCase extends Assert implements Test {...... public int countTestCases () {return 1;} ...... public TestResult run () {TestResult result = createResult (); run (result); return result;} public void run (TestResult result) {result.run (this);} ......} // decorative role public class TestDecorator extends Assert Implements Test {// Here, the instance of the component object is retained here, and the instance of the component object is preserved; public testDecorator (Test Test) {ftest = test;} / ** * the b asic run behaviour * / public void basicRun (TestResult result) {fTest.run (result);}. public int countTestCases () {return fTest.countTestCases ();} public void run (TestResult result) {basicRun (result);} Public string toString () {return ftest.toString (); public test;}} // specific decorative role, this type of enhancement is to set the execution of the test class PUBLIC CLAITED EXTENDS TESTDECoCorator { Private Int fTimeSrepeat; Public RepeatedTest (Test Test, Int Repeat) {Super (Test); if (Repeat <

0) throw new IllegalArgumentException ( "Repetition count must be> 0"); fTimesRepeat = repeat;} // decorate it to see how public int countTestCases () {return super.countTestCases () * fTimesRepeat;} public void run (TestResult Result) {for (INT i = 0; I

Although those who understand those systems are easy to customize, it is difficult to learn these systems, and it is also difficult to troubleshoot. This is the shortcomings of the decorative model mentioned by GOF. Can you experience it? What they said, I think it means the specific decorative role. This is the side effects of an object dynamically added function.

// This is an abstract component role public interface test {/ ** * counter the number of test. * / Public abstract int counttest. * / Public abstract int counttestcases (); / ** * Runs a test and collects ITS Result in a TestResult instance * / public abstract void run (TestResult result);.} // particular component object, but this is an abstract class public abstract class TestCase extends Assert implements Test {...... public int countTestCases () {return 1;} ...... public TestResult run () {TestResult result = createResult (); run (result); return result;} public void run (TestResult result) {result.run (this);} ......} // decorative role public class TestDecorator extends Assert Implements Test {// This will preserve an instance of the component object with the instance protected test ftest; public testDecorator (Test Test) {ftest = test;} / ** * the basic ru . N behaviour * / public void basicRun (TestResult result) {fTest.run (result);} public int countTestCases () {return fTest.countTestCases ();} public void run (TestResult result) {basicRun (result);} public String toString () {Return ftest.toString (); public test;}} // specific decorative role, this type of enhancement is the execution of the test class can set the execution of the test class PUBLIC Class RepeatedTest Extends TestDecorator {Private INT fTimeSremeSrepeat; Public RepeatedTest (Test Test, Int Repeat {Super (Test); IF (Repeat <

0) throw new IllegalArgumentException ( "Repetition count must be> 0"); fTimesRepeat = repeat;} // decorate it to see how public int countTestCases () {return super.countTestCases () * fTimesRepeat;} public void run (TestResult Result) {for (INT i = 0; I

转载请注明原文地址:https://www.9cbs.com/read-39774.html

New Post(0)