JAVA design Pattern-Structural Model summary __java

Source: Internet
Author: User

A structured pattern describes how classes and objects are organized to form larger structures. The structure type class pattern uses the inheritance mechanism to combine the interface and the realization, the structural object pattern uses the combination aggregation to combine the object to realize the new function, may change the object composition relation in the operation time, has the more flexibility, therefore only concerns the structural object pattern. There are 7 types of common structural patterns: ABCDFFP (adapter,bridge,composite,decorator,façade,flyweight,proxy)

1. Adapter mode (Adapter): Classes that do not work together because of incompatible interfaces can work together . There is a class or object adapter pattern that resolves problems where there is already a target customer interface and an existing functional class interface is incompatible. The object adapter pattern implementation is that the adapter object contains an adapter object for interface conversion, which is usually used when the project is modified or expanded. Adapter mode allows one interface to be compatible with other interfaces, thus giving a unified abstraction of several different interfaces.

The adapter mode focuses on the transformation of existing interfaces through the inheritance of classes or the combination of objects, which uses the implementation of "multiple inheritance" and brings bad high coupling, so it is generally not recommended. Object adapters take the form of "object Combination", which is more consistent with the spirit of loose coupling.


2. Bridging mode (bridge): Separation of abstraction and implementation so that they can be changed independently .

Collaboration: Abstraction forwards the client's request to its Implementor object.

3. Combination mode: Combine objects into a tree hierarchy, giving users consistent access to individual objects and grouped objects .

The composite pattern enables customers to have consistent use of individual objects and composite objects. This solves the solution of the client program and the complex object container decoupling, namely: through inheriting the unified interface, we can regard the container object and its child object as the same class object use, reduces the complexity in the object use.

Example: Java unit Test framework JUnit defines two concepts: TestCase and Testsuite, the former is a class that writes tests, the latter is a collection of different testcase, and the collection can also contain testcase, Running a testsuite in this way will run all the testcase it contains. JUnit uses a combination pattern to unify testcase and Testsuite, create a test interface to play the component role, testcase play the leaf role, testsuite play composite role. JUnit is a security-biased combination pattern, so you can't use test instead of testcase and Testsuite.

4. Decorator Mode (decorator): Dynamically adds some responsibilities to the object . Decorator mode is more flexible than generating subclasses in terms of adding functionality. [GOF "Design pattern"] The decorator model adopts object combination rather than inheritance to realize the ability to extend object function dynamically at runtime, and can extend multiple functions as needed to avoid the "flexibility difference" and "multi-subclass derivation problem" brought by using inheritance alone. At the same time, it conforms to the principle of object-oriented design, "prioritize the use of objects rather than inheritance" and "open-closed" principles.

Example: JUnit used the decorative mode, in the Java.extensions package, Testdecorator, Repeatedtest is the testcase of the extension of the decorative mode. Use methods such as: Testdecorator test=new repeatedtest (New Testxx (), 3);.

5. Appearance pattern (façade): Provides a consistent interface for a set of interfaces in a subsystem that makes this subsystem easier to use . The skin pattern is used to define a uniform look for multiple subsystems, and describes how to represent the entire subsystem with a single object. Façade is called by the customer role and is familiar with the functions of the subsystem, and several functional combinations are scheduled internally according to the needs of the customer.

Collaboration: Clients communicate with subsystems by sending requests to façade, façade forward these messages to the appropriate subsystem objects, and clients using façade do not need to directly access subsystem objects.

6. The mode of sharing: the use of shared technology to effectively support a large number of fine-grained objects. the implementation of string in Java, the refinement of the object sharing granularity;

Collaboration: The state required for flyweight execution must be internal or external, the internal state is stored in the Concreteflyweight object, and the external object is stored or computed by the client object. The state is passed to it when the user invokes the flyweight operation. Users should not instantiate the Concreteflyweight class directly, but only get Concreteflyweight objects from the Flyweightfactory object, so that they can be shared appropriately.

7. Proxy mode (proxy): Provides a proxy for other objects to control access to such objects. the proxy pattern provides some indirect access to some specific properties of the object, limiting, enhancing, or modifying these properties.

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.