Several common design patterns in Java __java

Source: Internet
Author: User
Tags odbc
Several design patterns that are more common in Java learning

Still school-horse soldier teacher roughly speaking, summed up

1. Single case mode (single state mode):

There are only a few classes or only one class in the course of the program's operation, "single case Mode". Regardless of how many times the external class calls "construct," the object in memory is allocated only once, to a single instance pattern. External objects cannot be created, so objects must be created inside the original for external invocation. Because there is no object created outside, you can only ask the static method, so the method to return the created object must be a static type, and the static method can only access the statically member variable, so the instantiated object type must be a static type.

Single example, many examples, static factory design, three "inseparable", understand can. A single example design pattern is present in the factory, without a new explanation. Static factories are different from ordinary factories, they are in conflict with one another.

2. General Factory design mode:

produce something to the factory. To get it out of the coupling.
Ordinary factory: Can expand the product, (expands the product factory), if expands the product line to be troublesome that, can cause the factory class to overflow.
Abstract Factory: Not suitable for the expansion of new product varieties, that will change a lot, change the abstract factory interface, new features, and specific factory functions.
Spring's Bean Factory.
Any custom vehicle type (also not arbitrary, is a subclass).
Arbitrary production process method: In order to unify the wording, must use the abstract (transportation) factory.
Abstract Factory: Skin-changing software-the overall style has changed, buttons, menus.

3. Strategy Design pattern: (_strategy)

4. Event design Mode "Listener" (Observer). There is a listening class, and the event type of the listener.

inside the design pattern: The interpreter is not important.
The adorner is simple.

the design pattern of chasing Meimei: include.
5.Brige (bridging) "gift-giving" mode self-study is more difficult. The integration of the relationship between the separation, careful integration, so that too many subclasses, the inheritance relationship itself is coupled high.
Extend to 2 dimensions, as in the example, one dimension is divided according to the specific type, the other is divided according to the type, resulting in the emergence of subclasses.
You should support extensions on 2 dimensions.
Core: That's all that's available. Using aggregation instead of inheritance is a very common means.
Bridge mode is not used a lot, very little use, horse soldiers have no use. 2 dimension permutations and combinations are bridged.
Replace inheritance with a combination.

6. Adapter design mode (Adapter): Java IO package inputstreamreader is suitable, one side is InputStream, while the reader, to a inputstreamreader transfer.
The distinction between design patterns is semantics, not grammar. The voltage of the charger. One side of an interface, the other side is another interface, incompatible with each other, on the adapter.
Where JDBC-ODBC is the adapter, Java to access ODBC, but not direct access, the middle through JDBC access to solve the problem, JDBC side of the Java connection, while connected to ODBC. That's what
Jdbc-odbc Bridge. Sometimes it is not obvious to differentiate from agents, but this is only a semantic distinction.

7.Command (command) obey the orders. Let him do all kinds of maxims. DoSomething () will also be extended in the future.
The original 23 design patterns: In the beginning, command design mode is: Do menu undo (History), return---undo.
A request that can be made by any object to execute. So the above is the right so the operation can be "revoked". Undo the painted picture and undo the written word.
At the time of the undo of all the graphical operations: Implement Undo (), use the stack to record all the actions performed, because the execution is undone first.
Struts says that it implements the command to complete a function order. is actually:.

obviously you can let boy do many things at once, so boy should have a lot of command references, that is, collection class: List.

8.State: Not much. The action of a class differs depending on the state of the class.
Turn the if-else into a pattern.

9. Decoration design mode: Decorator.

Mediator: The "very simple" mediator design pattern. As a mediator within the system. Intermediary, consultant. Design pattern version of the tank war inside there.





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.