The creator mode of Java design pattern, share the program of Love programming Life

Source: Internet
Author: User

PS: In today's 23 design mode of the creator, this concludes. The technology I bring today is shared with the creator model and prototype mode. Of course, in Java these two methods are very common, but we write a little bit low, but this is not my excuse not to learn it!!!

Creator Mode

No builder Pattern code example: requirements create a Motorcycle object and make it start running.

Engine Code Construction:

650) this.width=650; "style=" margin:10px auto; creator mode of "alt=" Java design mode share love programming, Program Life "src=" http://p3.pstatp.com/large/ 3b000004a9b8da9662c3 "/>

Engine Code Construction key

Example of frame code:

650) this.width=650; "style=" margin:10px auto; creator mode of "alt=" Java design mode share love programming, Program Life "src=" http://p3.pstatp.com/large/ 3b02000333d6ae3bad90 "/>

Example of frame code

Example of a tyre machine code:

650) this.width=650; "style=" margin:10px auto; creator mode of "alt=" Java design mode share love programming, Program Life "src=" http://p3.pstatp.com/large/ 3b060000234613f2ec87 "/>

Tyre machine code Show

Motorcycle Object Entity class:

650) this.width=650; "style=" margin:10px auto; creator mode of "alt=" Java design mode share love programming, Program Life "src=" http://p3.pstatp.com/large/ 3b0100033ce65ca32659 "/>

Motorcycle Object Entity Class

Test class:

650) this.width=650; "style=" margin:10px auto; creator mode of "alt=" Java design mode share love programming, Program Life "src=" http://p3.pstatp.com/large/ 3b0100033f5c2904488d "/>

Test class

Summary: Motorcycle objects When a large object: the construction process may require a lot of other objects to use, such as: frame, engine, tires so if each motorcycle object is created, it needs other objects and the caller needs to know the details of the motorcycle, which is very painful for the caller. So on that basis, we want to be able to decouple the caller from the creator.

function:

When the internal data of a class is too complex (usually the class responsible for holding the data, such as config, VO, PO, Entity ...). To create, you might need to understand the internal structure of this class, and how these things are organized and so on a big mess of things, this time will increase the cost of learning and will be very confusing, this time to think of a way to manage the data in this class, How to make it step-by-step at the time of creation, and the code readability is very good don't let me look at the flower eyes Ah, I want the things can also be set in, this is the application of the builder mode,Builder mode can be a class to separate the construction and presentation.

common usage Scenarios:build more complex products.

StringBuilder Dombuilder Saxbuilder

PreparedStatement in SQL

Summary of Builders ' class diagrams

650) this.width=650; "style=" margin:10px auto; creator mode of "alt=" Java design mode share love programming, Program Life "src=" http://p1.pstatp.com/large/ 3b0400003384eb30f62c "/>

Class Diagram Summary

Summary: Above, the caller does not need to know the specific build order in the concrete builder, the caller is focused on their own business. Achieve understanding decoupling. But the most important thing here is the build order, different builder ' can build different instance objects

Case testing: the need to build a corresponding brand of mobile phone objects. The caller does not need to know the specific object's invocation details.

Mobile entity class build code:

650) this.width=650; "style=" margin:10px auto; creator mode of "alt=" Java design mode share love programming, Program Life "src=" http://p3.pstatp.com/large/ 3b0600002a087f401451 "/>

Physical Phone class

The builder interface and the corresponding implementation:

650) this.width=650; "style=" margin:10px auto; creator mode of "alt=" Java design mode share love programming, Program Life "src=" http://p3.pstatp.com/large/ 3b0400005917e6f2c91c "/>

The builder interface and the corresponding implementation

Assembly interfaces and corresponding implementations:

650) this.width=650; "style=" margin:10px auto; creator mode of "alt=" Java design mode share love programming, Program Life "src=" http://p1.pstatp.com/large/ 3b0700003392567a8cc4 "/>

Assembly interfaces and corresponding implementations

Test class:

650) this.width=650; "style=" margin:10px auto; creator mode of "alt=" Java design mode share love programming, Program Life "src=" http://p3.pstatp.com/large/ 3b0700003bd159e2a6d2 "/>

Test class

Summary: And the need for plant differences

A, the underlying dependencies of an object are all assigned to the builder, the specific construction order, by the builder specified. The Director is then responsible for completing the assembly process. Callers do not need to know the exact order of the underlying and the assembly process. Re-create the builder directly if you don't need it, and complete the specific build sequence

b, the builder pattern focuses on the specific process of object creation, while the factory focuses on the results of the final creation of the object.


C, if the objects you build are different, you need multiple builders to complete them. So it will be used with a single case or factory.

Expand:

requirements, the build is similar to an object, but when the object is built, there are many properties. It is possible to forget the attribute, or the property is not fully written. The current object cannot be created and solves the problem through the creator pattern.

Implementation code:

650) this.width=650; "style=" margin:10px auto; creator mode of "alt=" Java design mode share love programming, Program Life "src=" http://p9.pstatp.com/large/ 3b0500008c0e48df75f2 "/>

Implementation code

Test class:

650) this.width=650; "style=" margin:10px auto; creator mode of "alt=" Java design mode share love programming, Program Life "src=" http://p1.pstatp.com/large/ 3b050001d4cfded48f7d "/>

Test class

PS: What do you want to share! You can leave a comment below!!!




Shanghai Shang School-it snake Essence Disease original.
Shanghai Shang School Java Training, Project practical teaching, the achievement of Java Training High-end brand!


This article is from the "13144428" blog, please be sure to keep this source http://13154428.blog.51cto.com/13144428/1966899

The creator mode of Java design pattern, share the program of Love programming Life

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.