Diagnostic Java code: Depth-first accessor and interrupted dispatch

Source: Internet
Author: User
Tags abstract

Design patterns can be a great help for a simple design that quickly centralizes to a single project. However, the simplest way to implement a design pattern in a particular environment is not necessarily obvious-there are many ways to do this. This month, we'll discuss some ways to apply a common design pattern to produce simple, short, and robust code.

First, let's look at two patterns that apply to many different environments. In all of the design patterns discussed in the design pattern (Erich Gamma etc, also known as the "Four from group (Gang of Four)", which describes some of the basics of the topic (see Resources), I find that the most widely used is the composite and Visitor patterns.

Let's take a closer look at both of these patterns.

Specifying recursive data types with composite

It is easy to see why the composite mode is useful. The composite pattern is simply an object-oriented method that specifies the data type of a recursive definition, and these opportunities are always present in software development.

The ability to study recursively defined object types is one of the most notable features of software engineering (in contrast to digital design, systems are built from finite state machines).

Extending class hierarchies with Visitor

As for the Visitor model, many of the reasons it has been so widely used are that it complements the composite model.

Visitor is often touted as a way to extend its functionality without actually modifying classes in the existing composite class hierarchy. But their ability is far more than that.

Because accessors allow you to separate some of the functionality of a class hierarchy from the class itself, you can use them in a variety of settings, where it is conceptually difficult to see functionality as an intrinsic part of those classes.

This phenomenon often occurs in the downward recursion on the composite data structure. For example, consider the class hierarchy of a binary tree and determine whether any node in the tree contains a zero accessor:

Listing 1. Two-fork tree with accessor

abstract class Tree {
Public abstract Boolean Accept (treevisitor);
}  
Class Leaf extends tree {
The public static final leaf is only = new leaf ();
  Public Boolean Accept (Treevisitor so) {
return that.forleaf (this);
 
}
Class Branch extends tree {
public int value;
Public tree left;
Public Tree right;   
Public Branch (int _value, tree _left, tree _right) {
This.value = _value;
This.left = _left;
This.right = _right;  
}
Public Boolean accept (Treevisitor so) {
return that.forbranch (this);
 
}
Interface Treevisitor {
Public Boolean forleaf (Leaf);
Public Boolean Forbranch (Branch that);
}
Class Zerofinder implements Treevisitor {
Public Boolean forleaf (Leaf this) {
return new Boolean (f Alse);
}
Public Boolean forbranch (Branch that) {
if (that.value = 0) {return new Boolean (true);}
Boolean FoundinlefT = that.left.accept (this). Booleanvalue ();
Boolean foundinright = That.right.accept (this). Booleanvalue ();
return new Boolean (Foundinleft | | foundinright);
}
}

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.