Simplifying IDioms

zhaozj2021-02-16  109

Xiaobu classmates are very uncomfortable, turning over a mist. I think he probably means that if the method needs to pass a more parameters, it is best to package these parameters into a class.

Simplifying IDioms

Before studying complex technologies, it is very helpful to understand the basic method that makes a simple understanding of the code.

Messenger

The most common method is to make the message to packed the information to a target for transmission instead of transmitting these information fragments separately. Note that if there is no letter, the code of translate () read is quite confusing.

//: simplifying: MessengerDemo.javapackage simplifying; import junit.framework *; class Point {// A messenger public int x, y, z; // Since it's just a carrier public Point (int x, int y, int z. ) {This.x = x; this.y = y; this.z = z;} public point (point p) {// copy-constructor this.x = px; this.y = py; this.z = PZ PUBLIC STRING TOSTRING () {return "x:" x "y:" y "z:" z;}} class vector {public int magnitude, direction; public vector (int magnitude, int direction) {This.magnitude = magnitude; this.direction = direction;}} class space {public static point translate (POINT P, Vector V) {P = new point (p); // Don't modify the original // perform calculation Using V. Dummy Calculation: PX = PX 1; PY = PZ 1; PZ = PZ 1; Return P;}} PUBLIC CLASS MessengerDemo Extends Testcase {Public Void Test () {Point P1 = New Point (1, 2 , 3); Point P2 = Space.Translate (P1, New Vector (11, 47)); String Result = "P1:" P1 "P2:" P2; System.out.println (Result); Assertequals (Result, "P1: x: 1 Y: 2Z: 3 p2: x: 2 Y: 3 z: 4");} public static void main (string [] args {Junit.textui.teestrunner.run (MessengerDemo.class);}} ///: ~

Because Messenger is only used to transmit data, the data it transmits is usually declared as public (public) for easy access. However, you can declare them into private (private) according to your needs. Collective parameters ??? (Collecting parameter)

Collecting Parameter is a brother of Messenger, a Messenger pass parameter gives a method, and Collecting Parameter gets information from this method. Generally speaking, this is usually used in the case of multiple methods (Multiple Methods), just like a pink bee.

The container is a particularly useful Collecting Parameter because it is used to dynamically add objects.

//: simplifying: CollectingParameterDemo.javapackage simplifying; import java.util *; import junit.framework *; class CollectingParameter extends ArrayList {} class Filler {public void f (CollectingParameter cp) {cp.add ( "accumulating");.. } public void g (CollectingParameter cp) {cp.add ( "items");} public void h (CollectingParameter cp) {cp.add ( "as we go");}} public class CollectingParameterDemo extends TestCase {public void test ( ) {Filler Filler = new filler (); CollectingParameter CP = New CollectingParameter (); FILLER.F (CP); Filler.g (CP); Filler.h (CP); String Result = " CP; System.out .println (CP); Assertequals (Result, "[Accumulating, Items, As We GO]);} public static void main (string [] args) {junit.textui.teestrunner.run (CollectingParameterDemo.class);}} / //: ~

Collecting Parameter must support or insert some values ​​via certain methods. According to this definition, the messenger can be used as a Collecting Parameter, provided that Collecting Parameter is modified by the method it passes.

table of Contents

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

New Post(0)