Java and Design Patterns

Source: Internet
Author: User

Design patterns, first look at the name, design, mode, the purpose is to design, in order to design to give some definitions, summed up, abstract out of the way, called patterns.

What is the design? Design in software construction, pre-load (requirements analysis, product definition, architecture selection), after the start or accompanying (coding, testing), including structures, packages, classes, subroutines, and the pattern is that these things.


Design pattern is a kind of thought


The sharing of design patterns is not purely technical, it is thought.

It is difficult to say what is thought, which requires the audience to have an appropriate level of understanding, and this level of evolution is far more elusive than the progress of a technology or tool, sometimes a 10-year cycle is less than an epiphany.

Design pattern is a kind of thought, is related to language strong.

In low-level languages (machines, compilations), there will be no patterns, and in future high-level languages, patterns will be implemented from the syntactic level (the schema itself is semantically); In today's widely used high-level language (Java), patterns are drawn out, and widely used, on the basis of problems that address specific scenarios.

Low-level and high-level languages, without a dividing line, are gradually evolving. Each programmer is at its level (or realm) at a level that understands low-level backwardness (such as machine language), but does not understand advanced levels. This is the "hacker and painter" in the blub of the dilemma.

Extended to the workplace, life and the world view, each person also live in their own level of abstraction, can understand the low-level backwardness, but can not understand the high levels of advanced where. From the level of the State, there is right and wrong, but if not evolved to a hierarchy, but also can not be fundamentally persuaded, need to reach, to recognize.


Three characteristics of object-oriented


Object-oriented three major features: encapsulation, inheritance, polymorphism.

Encapsulation, often associated with concepts such as abstraction, high cohesion, and consistent levels of abstraction.

The cap is a vertical line, which is commonly used in programming, but more commonly used is the inclusion ("has a"), which, broadly speaking, can even be thought of as an adapter pattern. Can be included without inheritance, because inheritance introduces complexity on the inheritance system.

Polymorphism is an override based on inheritance, plus a reference to a parent class variable to a child class object. You can think of polymorphism as a small step in Java, a static type language, to a dynamic type language (such as Ruby/python).


Abstract:

Extract the essence of things to form a concept.

and use the concept of reasoning, judgment of thinking activities.

Abstract communication.

Simulation ability, imagination ability.

Expressive ability (verbal, written, classical and Qiong Yao).

Words, words, sentences, paragraphs.


Single case


Singleton is one of the most important design patterns. is a stateless logic. Stateless is an important means for the application layer to achieve high availability and scalability.


Singleton One: Basic


Demonstrates one of the simplest examples of singleton, deferred initialization, and a single-case code for a double-check mechanism.

For the double-check mechanism of the single case, reduce the collision between threads (synchronized), only the first few threads can collide, the strength of the lock is small, the performance is improved, the thread is also safe.

For double-check Singleton, there is no practical meaning, only historical significance, increase the Java Programmer's insight into the error, if you write a single case, the simplest way is enough.

In the past, there was code class.forname ("Com.mysql.jdbc.Driver") in the old JDBC Manipulation database encoding, which was designed to load classes into the PermGen area of the JVM, so that the static method of the class could be used, which was not the same as the initialization of the class.

There are also benefits to explicit API initialization, such as the web system, where Init writes to the listener. Put the initialization in Init. It's clear.


Single example two: Spring


In the Java ecosystem, in addition to the JDK, the most basic framework is spring. The high-level abstraction of the spring framework has helped us solve the technical problems of many scenarios.

Spring's underlying, underlying thought is control inversion (Invest of controls), Dependency injection (Dependency injection), which can be thought of as a manifestation of a singleton. The relationship between the two singleton objects is said to be the case.

Control reversal, reversal of control, who used to have control; the idea of controlling inversion is that you can use it, but I still have the ability to initialize my own control.

Dependency injection, who wants to use (who depends), who is responsible for bringing in other classes.

Write a simple 3 class that can explain spring's thoughts. The basic process of spring IOC is to read the configuration file or scan the spring annotations, and use reflection to instantiate the class and place it in a map.

Combining Spring Technology Insider: In-depth analysis of spring architecture and design principles, it takes 1 weeks to take a brief look at the spring source, which is what we found.


Single case three: state and thread safety


Reference article: http://www.iteye.com/topic/960532

Stateful: A state is a data storage function.

Stateless: Stateless is a functional operation and the object is thread-safe.


Several concepts: Maintain global State, control state Read and write, thread safety.

The above refers to the thread safety of the state. For the explanation of HashMap, if the thread safety from the state of the absolute interpretation, is also said to be, hashmap inside there is an array, is also stateful.


Single example four: Database


A single case of state hardening: In fact, the database, each row is a single case.

A single example across a JVM: There are many implementations, as long as the lock persistence is met.

Multi-process Security singleton: The database lock is said.

All of these are distributed ideas.

Since the database was available, programmers have become cheap.


Other creation modes


Factories, abstract factories, builders, prototypes.

Factory: Creating objects is complex, so you need a factory that creates objects.

Builders: such as the Nike Factory, with the upper, laces, and then a complete whole, this is the builder mode.

When to use Singleton, when to use a single state (the single State of the Class).

Why not use static, and use a single case? There is an explanation: control inversion, Dependency injection, this idea. Retains control over the initialization itself. If the complete tool class has no initialization requirements, static is satisfied.


Structural mode


Opening and closing principle: Closed to modification, open to expansion.

Packaging (Composite): Do not want to change the original code, but also want to implement some functions.

Proxy: Give an example of a guardian, let eat to eat. Do something about it without changing the semantics.

Adaptation (Adapter): Adapter More than proxy, it changed the semantics, a bit of transformation.

Appearance (facade): Compared with the agent first, the structure is similar, the semantics are different.

Inheritance is a label, and the interface shows what it is capable of.


Object-oriented issues that have been debated:

is used to include (with your ability, but not hit your tag, "has a"), or with inheritance ("is a"). One view is that inheritance introduces complexity to software design.


Behavioral patterns


Behavior pattern, which describes the action of an object. Limitations, must be executed sequentially.???

Template: High-level process abstraction.

Responsibility chain: Demonstrates a process that defines the order of execution through annotations. Benefits: 1. Dynamic organizational processes; 2. The execution of the code control process; Deepening is the workflow.

The above two modes, is the reconstruction of the weapon, coupled with reflection, you can write the general function of the middleware.


Not the end.


Design patterns are boring, remember three things: Create an object, wrap an object, and, depending on the current scene, make it easy.

Farmer Spring: We do not produce water, we are just nature's porters. Programmers can't be farmers Mountain springs, not just jar bags of porters. To learn to create, while moving bricks, while thinking about how to create better things.


The words of the Master (Linus)


Bad programmers are concerned with code. Good programmers are concerned with data structures and the relationships between them.

Object-oriented language with object as the core, plus some associated methods, is simply raving.

The important thing is the data structure, the object itself is what important?

What's really interesting is when you interact with different types of objects and have lock rules.

However, even at this time, it is absolutely wrong to encapsulate what "object interface" is, because it is no longer a single object problem.

His conclusion is that object-oriented solutions are small problems.


This master is writing the operating system, from his point of view, nature is right, even if not from his point of view, we can also recognize the importance of data structure.

http://www.csdn.net/article/1970-01-01/2824040

Java and Design Patterns

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.