Principles of lambda Expression Design and architecture for Java language Programming learning [map]

Source: Internet
Author: User

Principles of lambda Expression Design and architecture for Java language Programming learning [figure]:
As you all know, lambda expressions are a simple improvement to the Java language, and in the JDK standard class library, there are a variety of ways to run it. But most Java code is not written by the program Ape that developed the JDK, but by ordinary program apes like ours.
Many people will come across a situation where you have implemented a new feature or fixed a bug and are satisfied with your changes. But after someone else has seen your code--maybe it happened in the code review, it doesn't buy it at all! For what is good code, what is bad code, there is a difference is normal!
Changes in design patterns
Design pattern is another familiar design idea, it is the template that solves the common problem in the software architecture. If you encounter a problem and are familiar with a pattern that adapts to it, you can apply the pattern directly to solve the problem. In a way, the design pattern fixes the best practices for solving specific problems.
Of course, there is no forever best practice. As an example of a once-rage singleton pattern, this pattern ensures that only one object instance is produced. Over the past decade, it has been criticized for making it more fragile and difficult to test. The popularity of agile development makes testing more important, and the problem of Singleton mode turns it into an anti-pattern: a pattern that should be avoided.
So what we should know is that using lambda expressions correctly makes existing design patterns better, simpler, or, in some cases, different implementations.

Some common design patterns are:
Command-by mode:
The command is an object that encapsulates all the details of invoking another method, which the command-mode uses to write out the generalized code that invokes the method sequentially, based on the run-time condition. There are four classes involved in the command mode,
2. Policy mode:
The policy mode can change the algorithm behavior of the software at run time. How to implement the policy pattern depends on your situation, but the main idea is to define a common problem, but different algorithms are implemented, and then these algorithms are encapsulated behind a unified interface.
File compression is a good example:
3. Observer Mode:
The Observer pattern is another behavior pattern that can be simplified and improved by lambda expressions. In observer mode, the Observer holds a list of observers. When the state of the observer is changed, the Observer is notified. The Observer pattern is used extensively in MVC-based GUI tools to automatically refresh the view module when the state of the model changes, to achieve decoupling between them.
4. Template Method Mode:

开发软件时一个常见的情况是有一个通用的方法,只是步骤上略有不同,我们希望不同的实现能够遵守通用模式,保证他们使用了同一个算法,也是为了让代码更加易读。窗边的小豆豆(http://www.simayi.net/dushubiji/6111.html)读书笔记摘抄好词好句及感悟赏析,一旦你从整体上理解了算法,就能更容易理解其各种表现。

The template method pattern is designed for these situations: the design of the overall algorithm is an abstract class, it has a series of abstract methods that represent the steps that can be customized in the algorithm, and this class contains some common code. Each variant of the algorithm is implemented by a specific class, which overrides the abstract method and provides the corresponding implementation.
Writing a DSL using Java
A domain-specific language (DSL) is a programming language for a specific part of a software system. They are usually smaller and less expressive than the common language of Java, which can handle most programming tasks. DSL height Exclusive: Do not ask for the screen, but to have expertise.
Java BDD Framework: The following example describes some of the behavior of a stack.
(swipe left and right to view)
public class stackspec{{
Describe ("A stack", it-
It.should ("Being empty when created", expect, {
Expect.that (New Stack ()). IsEmpty ();
});
It.should ("Push new Elementsonto the top of the stack", expect-
stack<integer> stack = new stack<> ();
Stack.push (1);
Expect.that (stack.get (0)). Isequalto (1);
});
It.should ("Pop the last element pushed onto the stack", expect, {
stack<integer> stack = new stack<> ();
Stack.push (2);
Stack.push (1);
Expect.that (Stack.pop ()). Isequalto (2);
});
});
}):
First we use the verb describe as the kit head, and then define a name to indicate what the behavior is describing, and here we use "a stack".
Every rule reads as close as possible to the sentences in English. They all start with it.should, where it refers to the object being described. Then use a simple English description of the behavior, finally using expect.that prefix, describe the expected behavior.
When a rule is checked, a simple report is obtained from the command line indicating whether a rule has failed. You will notice that the pop operation expects the return value to be 2 instead of 1, so the "pop the last element pushed onto the stack" fails:
A stack
Should pop the last element pushed onto, the stack[expected:but was:]
Should is empty when created
Should push new element onto the top of the stack
Well, we're here this time! Good bye!

Principles of lambda Expression Design and architecture for Java language Programming learning [map]

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.