Design mode Template - Seeking articles (original)

xiaoxiao2021-03-06  59

Design mode Template - Seeking articles

This time I went to Beijing, took the special trip to visit my university classmate, everyone can don't look at him, he is a famous four talents in our class, especially good at Chinese painting, and he is also small in Beijing. The famous arrogance, this is not just right to ask him a pair of "spring tree autumn cream", of course, this pair is still signed on his big name. It can be okay. . .嘿嘿.

Ok, gossip is less than the Journey to the Journey:

1, here, first define the abstract class of ChinesePainting:

Public abstract void signname (); // sign a big name

Public void drawchinesepainting () {// drawing country painting

Signname ();

}

}

2. Refine the specific implementation class for the Computersystem interface:

Public class chinesepaintingby refriend extends Chinesepainting {

Public ChinesePaintingByFriend () {

System.out.println ("Painted Spring Festival Autumn Cream Diagram!");

}

Public void signname () {

System.out.println ("Cover the big name!");

}

}

3, write test classes:

Public class test {

Public static void main (String [] args) {

ChinesePainting MyaskChinesePainting = new ChinesePaintingByFriend (); // I am going to a friend

MyaskChinesePainting.drawChinesePainting (); // Painting Spring Festival Cream Drawings

}

}

4, explanation:

A: Template Definition: Define the skeleton of an operation algorithm to delay the execution of some steps into its subclass.

B: From this example we can see, in the abstract class of ChinesePainting. Although we define the signname () method, but not implemented, it is called again in DrawChinesePainting (), and the SignName () method is really realized but placed in the subclass of CHINESEPAINTINGBYFRIEND. In fact, this is the main idea of ​​the template mode.

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

New Post(0)