Talking about the decoration mode, talking about the decoration

Source: Internet
Author: User

Talking about the decoration mode, talking about the decoration
Differences between decoration and inheritance:

Decoration: Based on existing functions and provides enhanced functions, decoration Classes usually receive decorated objects through constructor methods. And provides stronger functions based on the functions of the objects to be decorated. Compared with inheritance, the decoration mode can extend the functions of objects without creating more subclasses. English name: Decorator

 

Inheritance: To extend a function, You have to inherit the parent class and overwrite the class, which leads to a bloated system and high memory usage.

 

In general, the interface can also be viewed as an abstract class, that is, the parent class object can also be an interface. If A Class A implements an interface T, when we declare an instance: T t = new A (); t looks like a t, essentially A, which is also A type of polymorphism!

First, create an interface:

1 public class test {2 interface a {3 void fun (); 4} 5 class B implements a {6 7 public void fun () {8 System. out. println ("this implementation interface class! "); 9} 10 11} 12 class c extends B {13 public void fun () {14 System. out. println (" this is a subclass of the implementation interface class !! "); 15} 16} 17 public static void main (String [] args) {18 a dd = new test (). new c (); 19 dd. fun (); 20} 21}

Run:

 

Example 1:

From the example of a great god of CSDN, we can see that this program can be understood as long as the functions of polymorphism are clear. To put it bluntly, the decoration mode is nothing more than the flexible use of polymorphism and constructor methods:

1 public class decorateMode {2 // defines the modifier 3 public interface Human {4 public void wearClothes (); 5 6 public void writable towhere (); 7} 8 9 // defines the modifier 10 public abstract class Decorator implements Human {11 private Human human; 12 13 public Decorator (Human human) {14 this. human = human; 15} 16 17 public void wearClothes () {18 human. wearClothes (); 19} 20 21 public void synchronized towhere () {22 human. walkToWh Ere (); 23} 24} 25 26 // the three types of decoration are defined below. This is the first and the second and third functions are refined in sequence, 27 more public class Decorator_zero extends Decorator {28 29 public Decorator_zero (Human human) {30 super (human); 31} 32 33 public void goHome () {34 System. out. println ("into the house .. "); 35} 36 37 public void findMap () {38 System. out. println (" .. "); 39} 40 41 public void wearClothes () {42 // TODO Auto-generated method stub 43 super. wearClothes (); 44 goHome (); 45} 46 47 public void upload towhere () {48 // TODO Auto-generated method stub 49 super. optional towhere (); 50 findMap (); 51} 52} 53 54 public class Decorator_first extends Decorator {55 56 public Decorator_first (Human human) {57 super (human ); 58} 59 60 public void goClothespress (){ 61 System. out. println ("go to the closet to find it .. "); 62} 63 64 public void findPlaceOnMap () {65 System. out. println (" find on Map .. "); 66} 67 68 public void wearClothes () {69 // TODO Auto-generated method stub 70 super. wearClothes (); 71 goClothespress (); 72} 73 74 public void writable towhere () {75 // TODO Auto-generated method stub 76 super. optional towhere (); 77 findPlaceOnMap (); 78} 79} 80 81 public class Decorator_two extends Decorator {82 83 public Decorator_two (Human human) {84 super (Human ); 85} 86 87 public void findClot Hes () {88 System. out. println ("Find a D & G .. "); 89} 90 91 public void findTheTarget () {92 System. out. println (" find the mysterious garden and Castle on Map .. "); 93} 94 95 public void wearClothes () {96 // TODO Auto-generated method stub 97 super. wearClothes (); 98 findClothes (); 99} 100 101 public void writable towhere () {102 // TODO Auto-generated method stub103 super. inclutowhere (); 104 findTheTarget (); 105} 106 107 // defines the decorator, the decorator's initial state is decorated with some of its own 109 public class Person implements Human {110 111 public void wearClothes () {112 // TODO Auto-generated method stub 113 System. out. println ("what to wear .. "); 114} 115 116 public void returns towhere () {117 // TODO Auto-generated method stub118 System. out. println (" where to go .. "); 119} 120} 121 // test class. You can see how similar java I/O operations are. 122 public static void main (String [] args) {123 decorateMode = new decorateMode (); 124 Human person = decorateMode. new Person (); 125 Decorator decorator = decorateMode. new Decorator_two (decorateMode. new Decorator_first (126 decorateMode. new Decorator_zero (person); 127 decorator. wearClothes (); 128 decorator. optional towhere (); 129} 130}

Running result:

Example 2:

Read the row content of the file and add the number of lines and semicolons to the front of each row.

1 package com. beiwo. io; 2 3 import java. io. bufferedReader; 4 import java. io. fileNotFoundException; 5 import java. io. fileReader; 6 import java. io. IOException; 7 import java. io. reader; 8 9/* 10 11 BufferedReader: expands the FileReader function. 12 13 requirement 1: Read the Code through readLine, and add a row number to each row. 14 requirement 2: Read the Code through readLine, and add a semicolon to each row. 15 requirement 3: Read the Code through readLine. Each row is enclosed by a pair of quotation marks. 16 17 // ============================================== ===================================== 18 requirement 4: read the Code through readLine, and add a quotation mark + line number to each line. 19 requirement 5: Read the Code through readLine, and add a semicolon + line number to each row. 20 requirement 6: Read the Code through readLine, and add a semicolon + quotation mark to each line. 21 requirement 7: Read code through readLine. Add a semicolon + quotation mark + row number to each row. 22 23 if inheritance is used: 24 benefits: code structure and clarity are easy to understand 25 disadvantages: the inheritance system will be huge. To implement the seven requirements, you must create seven sub-classes 26 27 28 decorator mode: to enhance the functionality of a class, you can also make the decorator classes decorated with each other. 29 30 31 32 */33 34 // Add the row number 35 class BufferedReaderLineNum extends BufferedReader {36 // define an object to receive the passed object 37 BufferedReader bufferedReader; 38 int count = 1; 39 40 // The subclass inherits the parent class and calls the non-argument constructor of the parent class by default. 41 public BufferedReaderLineNum (BufferedReader in) {42 super (in); 43 this. bufferedReader = in; 44} 45 46 // method 47 @ Override 48 public String readLine () throws IOException {49 // TODO Auto-generated meth Od stub 50 // call the readLine 51 String content of the parent class = bufferedReader. readLine (); 52 // give the content field a row number 53 if (content = null) {54 55 return null; 56} 57 content = count + "" + content; 58 count ++; 59 return content; 60} 61 62} 63 64 // Add a semicolon to the 65 class BufferedReaderLineSemi extends BufferedReader {66 // define an object to receive the passed object 67 BufferedReader bufferedReader; 68 // The subclass inherits the parent class and calls the non-argument constructor of the parent class 69 public BufferedReaderLineSemi (Bu FferedReader in) {70 super (in); 71 this. bufferedReader = in; 72} 73 74 // rewrite the readLine method 75 @ Override 76 public String readLine () throws IOException {77 // TODO Auto-generated method stub 78 // call the readLine 79 String content = bufferedReader of the parent class. readLine (); 80 // give the content field a row number 81 if (content = null) {// indicates that the data has been read 82 83 return null; 84} 85 86 return content + ";"; 87} 88 89} 90 91 92 93 public class Demo1 {94 95/** 96 * @ param args 97 * @ throws IOException 98 */99 public static void main (String [] args) throws IOException {100 // TODO Auto-generated method stub101 testLineNum (); 102} 103 104 public static void testLineNum () throws IOException {105 106 // 1. open a channel with a file path 107 FileReader reader = new FileReader ("Enter the file path here, for example, C: \. java "); 108 // create a buffer stream 109 BufferedReader bufferedReader = new BufferedReade R (reader); 110 // 2. create a buffer stream with semicolons: 111 bytes lineSemi = new BufferedReaderLineSemi (bufferedReader); 112 // create a buffer stream with row number points: 113 BufferedReaderLineNum lineNum = new BufferedReaderLineNum (lineSemi ); 114 // 3. start to read data 115 String content = null; 116 while (content = lineNum. readLine ())! = Null) {117 118 System. out. println (content); 119} 120 121 // 4. close resource 122 lineNum. close (); 123} 124}

Effect:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.