Facade pattern -Facade Pattern

In real life, there are often things more complicated examples, such as Banfangchanzheng or register a company, sometimes with multiple departments to contact, then if there is a comprehensive department can solve all the procedural problems just fine.

Software design, too, when the function of a system of growing, the subsystem will be more and more customers access to the system has become increasingly complex. Then if the internal system changes, the client must change, contrary to the "principle of opening and closing," but also violated the "Law of Demeter", it is necessary to provide a unified interface for multiple subsystems, thereby reducing system the degree of coupling, which is the appearance of the target pattern.

Figure 1 shows the customer to the local Real Estate Board for the relevant departments to encounter real estate license transfer.
 

Handle the relevant departments of the real estate license transfer
Figure 1 for the relevant departments of the real estate license transfer

Define the appearance and characteristics of mode

Define the appearance (the Facade) mode: by providing a consistent interface to a plurality of complex subsystems, the mode of the subsystems to be accessed more easily. The model has a unified interface to the external, external applications do not care about the specific details of the internal subsystems, it would greatly reduce the complexity of the application, improve the maintainability of the program.

Appearance (the Facade) mode is "Demeter" typical application, it has the following main advantages.

  1. Reducing the degree of coupling between the subsystem and the client, so that the change does not affect the subsystem of the calling client class.
  2. Blocked customer subsystem components, reduces the number of target clients, and easier to use such subsystems.
  3. Reducing dependency compile large software system, the system simplifies the process of migration between the different platforms, compile because a subsystem does not affect other subsystems, it will not affect the appearance of the object.


The main drawback of the appearance (the Facade) mode are as follows.

  1. We can not properly restrict the use of sub-class customers.
  2. Adding a new subsystem may need to modify the appearance or client class source code, contrary to the "on-off principle."

Architecture and Implementation Mode appearance

Appearance of the structure (Facade) model is relatively simple, is the definition of a high-level interface. It contains a reference to the various subsystems, the client can access it through the various subsystems. Now to analyze the basic structure and implementation.

1. Structure Model

Appearance (Facade) model consists of the following major role.

  1. Appearance (Facade) role: to provide a common multiple of external interface subsystem.
  2. Subsystem (Sub System) role: to achieve part of the system functions, customers can access it through the appearance of the characters.
  3. Customer (Client) Role: access by various subsystems of the appearance of a role.


The structure shown in Figure 2.
 

FIG appearance configuration mode
2 a configuration diagram of FIG appearance (the Facade) mode

2. Mode of realization

Facade pattern codes are as follows:

package facade;
public class FacadePattern
{
    public static void main(String[] args)
    {
        Facade f=new Facade();
        f.method();
    }
}
//外观角色
class Facade
{
    private SubSystem01 obj1=new SubSystem01();
    private SubSystem02 obj2=new SubSystem02();
    private SubSystem03 obj3=new SubSystem03();
    public void method()
    {
        obj1.method1();
        obj2.method2();
        obj3.method3();
    }
}
//子系统角色
class SubSystem01
{
    public  void method1()
    {
        System.out.println("子系统01的method1()被调用!");
    }   
}
//子系统角色
class SubSystem02
{
    public  void method2()
    {
        System.out.println("子系统02的method2()被调用!");
    }   
}
//子系统角色
class SubSystem03
{
    public  void method3()
    {
        System.out.println("子系统03的method3()被调用!");
    }   
}

Program results are as follows:

Subsystem 01 method1 () is called! 
Subsystem 02 method2 () is called! 
Subsystem 03 method3 () is called!

Application Examples Appearance Model

[Example 1] using the "look mode" Design of a specialty Wuyuan optional interface.

Analysis: Appearance of the present example is the role WySpecialty JPanel subclass, it has 8 sub-system role Specialty1 ~ Specialty8, which is an icon type (the ImageIcon) a subclass object, to save the specialty Wuyuan icon ( here to download to be displayed Wuyuan specialty of the picture ).

Skin class (WySpecialty) with JTree components to manage the name Wuyuan specialty, and defines an event processing method valueClianged (TreeSelectionEvent e), when the user selects specialty from the tree, the specialty icon object stored in the tag (JLabd) object.

Customers form an object with a split panel to achieve, left to put the role of the appearance of the tree, the label of the selected specialty display image to the right place. The structure shown in Figure 3.
 

FIG configuration management interface specialty Wuyuan
FIG 3 FIG configuration management interface specialty Wuyuan


Code is as follows:

package facade;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class WySpecialtyFacade
{
    public static void main(String[] args)
    {
        JFrame f=new JFrame ("外观模式: 婺源特产选择测试");
        Container cp=f.getContentPane();       
        WySpecialty wys=new WySpecialty();       
        JScrollPane treeView=new JScrollPane(wys.tree);
        JScrollPane scrollpane=new JScrollPane(wys.label);       
        JSplitPane splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,treeView,scrollpane); //分割面版
        splitpane.setDividerLocation(230);     //设置splitpane的分隔线位置
        splitpane.setOneTouchExpandable(true); //设置splitpane可以展开或收起                       
        cp.add(splitpane);
        f.setSize(650,350);
        f.setVisible(true);   
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class WySpecialty extends JPanel implements TreeSelectionListener
{
    private static final long serialVersionUID=1L;
    final JTree tree;
    JLabel label;
    private Specialty1 s1=new Specialty1();
    private Specialty2 s2=new Specialty2();
    private Specialty3 s3=new Specialty3();
    private Specialty4 s4=new Specialty4();
    private Specialty5 s5=new Specialty5();
    private Specialty6 s6=new Specialty6();
    private Specialty7 s7=new Specialty7();
    private Specialty8 s8=new Specialty8();
    WySpecialty(){       
        DefaultMutableTreeNode top=new DefaultMutableTreeNode("婺源特产");
        DefaultMutableTreeNode node1=null,node2=null,tempNode=null;       
        node1=new DefaultMutableTreeNode("婺源四大特产(红、绿、黑、白)");
        tempNode=new DefaultMutableTreeNode("婺源荷包红鲤鱼");
        node1.add(tempNode);
        tempNode=new DefaultMutableTreeNode("婺源绿茶");
        node1.add(tempNode);
        tempNode=new DefaultMutableTreeNode("婺源龙尾砚");
        node1.add(tempNode);
        tempNode=new DefaultMutableTreeNode("婺源江湾雪梨");
        node1.add(tempNode);
        top.add(node1);           
        node2=new DefaultMutableTreeNode("婺源其它土特产");
        tempNode=new DefaultMutableTreeNode("婺源酒糟鱼");
        node2.add(tempNode);
        tempNode=new DefaultMutableTreeNode("婺源糟米子糕");
        node2.add(tempNode);
        tempNode=new DefaultMutableTreeNode("婺源清明果");
        node2.add(tempNode);
        tempNode=new DefaultMutableTreeNode("婺源油煎灯");
        node2.add(tempNode);
        top.add(node2);           
        tree=new JTree(top);
        tree.addTreeSelectionListener(this);
        label=new JLabel();
    }   
    public void valueChanged(TreeSelectionEvent e)
    {
        if(e.getSource()==tree)
        {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if(node==null) return;
            if(node.isLeaf())
            {
                Object object=node.getUserObject();
                String sele=object.toString();
                label.setText(sele);
                label.setHorizontalTextPosition(JLabel.CENTER);
                label.setVerticalTextPosition(JLabel.BOTTOM);
                sele=sele.substring(2,4);
                if(sele.equalsIgnoreCase("荷包")) label.setIcon(s1);
                else if(sele.equalsIgnoreCase("绿茶")) label.setIcon(s2);
                else if(sele.equalsIgnoreCase("龙尾")) label.setIcon(s3);
                else if(sele.equalsIgnoreCase("江湾")) label.setIcon(s4);
                else if(sele.equalsIgnoreCase("酒糟")) label.setIcon(s5);
                else if(sele.equalsIgnoreCase("糟米")) label.setIcon(s6);
                else if(sele.equalsIgnoreCase("清明")) label.setIcon(s7);
                else if(sele.equalsIgnoreCase("油煎")) label.setIcon(s8);
                label.setHorizontalAlignment(JLabel.CENTER);
            }
        }               
    }
}
class Specialty1 extends ImageIcon
{
    private static final long serialVersionUID=1L;
    Specialty1()
    {
        super("src/facade/WyImage/Specialty11.jpg");
    }
}
class Specialty2 extends ImageIcon
{
    private static final long serialVersionUID=1L;
    Specialty2()
    {
        super("src/facade/WyImage/Specialty12.jpg");
    }
}
class Specialty3 extends ImageIcon
{
    private static final long serialVersionUID=1L;
    Specialty3()
    {
        super("src/facade/WyImage/Specialty13.jpg");
    }
}
class Specialty4 extends ImageIcon
{
    private static final long serialVersionUID=1L;
    Specialty4()
    {
        super("src/facade/WyImage/Specialty14.jpg");
    }
}
class Specialty5 extends ImageIcon
{
    private static final long serialVersionUID=1L;
    Specialty5()
    {
        super("src/facade/WyImage/Specialty21.jpg");
    }
}
class Specialty6 extends ImageIcon
{
    private static final long serialVersionUID=1L;
    Specialty6()
    {
        super("src/facade/WyImage/Specialty22.jpg");
    }
}
class Specialty7 extends ImageIcon
{
    private static final long serialVersionUID=1L;
    Specialty7()
    {
        super("src/facade/WyImage/Specialty23.jpg");
    }
}
class Specialty8 extends ImageIcon
{
    private static final long serialVersionUID=1L;
    Specialty8()
    {
        super("src/facade/WyImage/Specialty24.jpg");
    }
}

Model application scenarios appearance

Generally can be considered in the following using the appearance model.

  1. When the system constructs a hierarchical structure, using the appearance model defined entry points each subsystem can be simplified dependencies between subsystems.
  2. When a subsystem of a complex system of many, the appearance pattern can design a simple interface for external access to the system.
  3. When there is a big link between the client and a plurality of subsystems, which can be introduced into the separation aspect mode, thereby improving the independence and portability subsystem.

Expand Appearance Model

In appearance mode, when adding or removing the need to modify the appearance-based subsystem, which is contrary to the "on-off principle." If the introduction of the abstract class appearance, it is to some extent solve the problem, the structure shown in Figure 5.
 

Exterior appearance introduced patterns abstract class structure of FIG.

Guess you like

Origin blog.csdn.net/yucaixiang/article/details/94030739