Maximize the reusability of the code, overcome the shortcomings of traditional object-oriented programming methods in reusabilit

xiaoxiao2021-03-05  21

Reuse is a myth, which seems to be a consensus that become programmers. However, reuse may be difficult to implement because traditional object-oriented programming methods have some shortcomings in reuse. This tip describes three steps to form a different way to support reuse. Step 1: Transfer the function to the class instance method due to the lack of accuracy of the class inheritance mechanism, so it is not an ideal mechanism for code reuse. That is, if you need to reuse a single method of a class, you must inherit other methods of such a class and data members. This cums will not be necessary to reuse the code of this method complicated. Inheritance class has introduced additional complexity on its parent class: changes to the parent class will affect the subclass; when changing the parent class or subclass, it is difficult to remember what methods covered (or no Which methods are covered; and whether the corresponding parent class should be invoked. Any way to perform a single conceptual task should be independent and should be used as the preferred method for reuse. To achieve this, we must return to process programming, remove the code from the class instance method and move it into a global visible process. In order to improve the reusability of such a process, you should write such a method like writing a static utility: each process is only using its own input parameters and / or completing its work in the call to other global visible processes, and should not Use any non-local variables. This external dependency reduction reduces the complexity of the use of the process, thereby promoting the reuse of it elsewhere. Of course, even those who don't plan reuse will benefit from this structure, because its structure is always quite clear. In Java, the method cannot be existed from the class. However, you can take the related steps to make the method a single class, a public visible static method. As an example, you can use a class similar to the following: class polygon {..public int getImeter () {...} public boolean isconvex () {...} public boolean containspoint (POINT P) {...} ..} and change it to the following form: Class Polygon {..public int getPerimeter () {return pPolygon.computeperimeter (this);} public boolean isconvex () {return ppolygon.isconvex (this);} public boolean containsPoint (Point p) {return pPolygon.containsPoint (this, p);} ..} wherein, pPolygon follows: class pPolygon {static public int computePerimeter (Polygon polygon) {...} static public boolean isConvex (Polygon polygon ) {...} Static Public Boolean ContainSpoint (POLYGON POLYGON, POINT P) PPOLYGON reflects the process of the package in this class is mainly related to the type Polygon object. Poempt of P represents the unique use of this class to organize public visible static processes. However, the names in Java are unregulated with lowercase letters, and the categories like PPOLYGON do not complete the normal class function. That is to say, it does not mean a type of object; it is just an organizational entity required for the language. All of the effects made in the above case are that client code is no longer to reuse its functions by inheriting Polygon. This feature is now available in the PPOLYGON class. The client code only uses the features it needs, without having to care about the features it does not need. This does not mean that the class does not play an active role in the new process programming style.

On the contrary, the class must perform the necessary packet tasks and encapsulate the data members of the objects represented. In addition, the class has excellent reusability by implementing multiple interfaces, referring to the description in the second step. However, you should categorize the preferred techniques of reusability and polymorphisms through class inheritance, because the functionality is not the best choice for reusability in an instance method. The four-person best-selling book Design Patterns briefly refers to a technique that is only a small difference with this technology. The Strategy mode in this book is advocated to encapsulate each series of members of the relevant algorithm in a common public interface so that the client code can exchange these algorithms. Because an algorithm is typically written as one or several independent processes, this package emphasizes the process of executing a single task (ie, an algorithm) without emphasizing the objects including code and data, performing multiple tasks. This step also reflects the same basic idea. However, the interface package algorithm means that the algorithm is written as an object that implements the interface. This means that we are still bound in other methods of coupled together with data and other methods of their packaging objects, thus making reuse complications. Each time you use an algorithm, you must instantiate these objects is also a problem, which will reduce the performance of the program. Fortunately, a solution provided by Design Patterns solves these two problems. You can use the Flyweight mode when writing a Strategy object, so that each object has only a well-known shared instance (this instance handles execution issues) so that each shared object will not maintain the status between two accesss (so this Objects do not include any member variables to solve many coupling issues). The generated Flyweight-Strategy mode integrates the technology of the package function in this step in the global available stateless process. Step 2: Converting the input parameter type of the non-basic data type to the interface type through the interface parameter type rather than through class inheritance, which is a true foundation for reusability in object-oriented programming methods, just as Allen Holub As mentioned in "Build User Interfaces for Object-Oriented Systems, Part 2". "... Reusability is implemented by writing interfaces instead of writing classes. If all parameters of a method are references to some known interfaces, these interfaces are from some classes you have never heard. Implemented, then the method can operate objects that are not existing when writing code. From a technical statement, it can be reused is a method, rather than passing to the method. "Apply Holub's discussion to the first The result of steps, once a function block can be used as a global visible independent process, you can further improve its reusability by converting each of the type of type input parameter type to an interface type. Thus, any class of objects that implement the interface type is in line with the requirements of this parameter, not just the requirements of the original class. This way, this process is potentially available for more object types. For example, assuming that you have a global visible static method: Static Public Boolean Contains (Rectangle Rect, INT X, INT Y) {...} This method is intended to determine if a given rectangle contains a given location.

Here you should change the type of RECT parameters from class type Rectangle to interface type, as shown below: Static Public Boolean Contains (Rectangular Rect, Int x, int y) {...} Rectangular Could Be The Following Interface: Public Interface Rectangular {Rectangle getBounds ();} Now describing objects for Rectangular classes (that is, the Rectangular interface) can be passed to prectangular.contains () as the parameters of RECT (). We improve the reusability of methods by relaxing the constraints that can be passed to the method. However, in the above example, when the getBounds method of the Rectangle interface returns a Rectangle, you may not know what practical benefits will be used to use the Rectangular interface; that is, if we know that the object we have to be in is requested. Can return Rectangle; why don't you pass the Rectangle type and get to the interface type? The most important reason is related to the collection. It is assumed that there is a method: static public boolean areaOverlapping (Collection Rects) {...} This method is intended to determine whether the Rectangular object in a given collection has overlap. Next, in the method body, when you process each object in the collection, if you cannot convert the object to an interface type such as Rectangular, how can I access the Rectangle of that object? The only choice is to convert objects to a specific class type (we know that there is a method in this class to provide Rectangle), which means that the method must know what type of type it is in advance, so it can only be used Use these types. This is the first step to avoid the problem! Step 3: Selecting a small input parameter interface type of coupling In executing a second step, should you choose what interface type to replace a given class type? The answer is: any interface that can fully describe the process on the parameters and have a minimum of any interfaces. The smaller the interface to which the parameter object is to be implemented, the larger the chance to implement the interface, and the more of its objects can be used as the number of classes of this parameter. It is easy to see if you have such a method: static public boilean isoverlapping (Window Window1, Window Window2) {...} This method is designed to determine whether the two (assumed for the Rectangular) window overlap, if the method only requires Its two parameters provide their respective Rectangular coordinates, it is best to simplify the type of these two parameters to reflect this fact: Static Public Boolean AreoverLapping (Rectangular Rect1, Rectangular Rect2) {...} The above code assumes the front of Window Type objects can also realize Rectangular. You can now reuse any of the features included in the first method of the Rectangular object. You may have many experiences, that is, fully specifying the available interfaces required for parameters to include excessive unnecessary methods. When you encounter this situation, you should define a new public interface in the global namespace so that other methods that may face the same dilemma reuse this interface. You may have many experiences, that is, it is best to create a unique interface to specify a single process for a parameter. The interface you created will only be used for that parameter.

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

New Post(0)