The appearance pattern __java of Java design mold

Source: Internet
Author: User

The façade (appearance) pattern provides a concise and consistent interface for various types (or structures and methods) in a subsystem, hiding the complexity of subsystems and making subsystems easier to use. before using a façade

Suppose there are five classes: propressor, compiler, assembler, linker, AR. Five steps for GCC: precompiling, compiling, compiling, linking, and packaging (refer to the Linux g++ link).
These five steps are divided into separate tasks, but they are coupled before. For example, precompiling, compiling, compiling, linking these four steps is a sequential relationship. They must be executed sequentially in order to get the correct results.
However, users don't care how they collaborate, they only want to use a GCC command to get the final executable file.

public class User {
    private target generatetarget (file source)
    {
        propressor cpp;
        compiler cc1;
        Assembler as;
        linker ld;

        Target i = cpp.propressing (source);
        Target s = cc1.compilation (i);
        Target o = as.assembly (s);
        Target out = ld.linking (o);
        return out;
    }

    public void Main (string[] args) {
        target exe = Generatetarget (args[1]);
    }

Sometimes, users just want to generate static libraries and dynamic libraries.

public class User {
    private target generatestaticlib (file source)
    {
        propressor cpp;
        compiler cc1;
        Assembler as;
        Ar ar;

        Target i = cpp.propressing (source);
        Target s = cc1.compilation (i);
        Target o = as.assembly (s);
        Target a = ar.ar (o);
        return A;
    }
    Private target Generatedynamiclib (file source)
    {
        propressor cpp;
        compiler cc1;
        Assembler as;
        linker ld;

        Target i = cpp.propressing (source);
        Target s = cc1.compilation (i);
        Target o = as.assembly (s);
        Target so = Ld.linkingwithpic (o);
        return so;
    }

    public void Main (string[] args) {
        target lib1 = Generatestaticlib (args[1));
        Target lib2 = Generatedynamiclib (args[2]);
    }
after the introduction of the façade

If you have a certain understanding of the process of compiling a link for gcc, you will be able to easily read these functions.
but not everyone who writes the user knows how the steps are used.
Even if you know these steps, it is cumbersome to ask each user to write it again.
Imagine that if you don't have the GCC command, you want to generate executable files or libraries from the source code, and then you have to knock down the CPP, CC1, as, LD, AR commands to build. You'd rather spend a few minutes writing a script, putting these commands together and executing them uniformly. The
adorner works a bit like this script. Several subsystems in the GCC process are more complex to use, thus encapsulating several commonly used usages as interfaces to simplify user usage. For special cases, subsystems can also be used directly.

public class GCC {public target generatetarget (file source) {Propressor cpp;
        compiler cc1;
        Assembler as;

        Linker ld;
        Target i = cpp.propressing (source);
        Target s = cc1.compilation (i);
        Target o = as.assembly (s);
        Target out = ld.linking (o);
    return out;
        Public target Generatestaticlib (file source) {Propressor cpp;
        compiler cc1;
        Assembler as;

        Ar ar;
        Target i = cpp.propressing (source);
        Target s = cc1.compilation (i);
        Target o = as.assembly (s);
        Target a = ar.ar (o);
    return A;
        Public target Generatedynamiclib (file source) {Propressor cpp;
        compiler cc1;
        Assembler as;

        Linker ld;
        Target i = cpp.propressing (source);
        Target s = cc1.compilation (i);
        Target o = as.assembly (s);
        Target so = Ld.linkingwithpic (o);
    return so; } public class User {
    public static void Main (string[] args) {gcc g;
        Target out = G.generatetarget (args[1]);
        Target LIB1 = Generatestaticlib (args[2]);
        Target lib2 = Generatedynamiclib (args[3]);
    Target i = cpp.propressing (source); }
}
use the back sense

An appearance pattern is finished, now analyze its role. 1. The appearance of GCC for complex subsystems propressor, compiler, assembler, linker, AR provides a simple interface. Users do not need to understand the relationship between subsystems to understand the functionality provided by GCC. 2. The functions of these subsystems are related, the coupling is relatively high, the relationship between subsystems is more likely to change, but the possibility of changing the function is not significant. When changes occur between subsystems and the functionality is unchanged, only the appearance of GCC needs to be modified, and user code does not need to be modified. 3. For special cases, the subsystem can also be used directly, maintaining the original flexibility.

In some cases, it can play a positive role, and some situations may have the opposite effect. Each of these patterns has its own application scenario. 1. Multiple classes serve the same function, and they are highly coupled and complex to use. 2. Relationships between multiple classes are often changed. 3. There are some common collocation methods used by many classes. 4. Users usually do not care about the coordination between several classes.

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.