NOTE

Facade Pattern

Facade-pattern notes with UML plus Java and Go examples that wrap a complex process behind one interface.

Software Architecture & Engineering1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Definition

Encapsulate a complex process in one class, such as utility classes.

2. UML

PlantUML 图表

3. Java

3.1. Client

public class Client
{
    public static void main(String[] args)
    {
        Facade facade = new Facade();
        facade.run();
    }
}

3.2. Complex Process

public class Process1
{
    public void run()
    {
        System.out.println("Process1");
    }

}
public class Process2
{
    public void run()
    {
        System.out.println("Process1");
    }
}

3.3. Facade

public class Facade
{
    public void run()
    {
        Process1 a = new Process1();
        Process2 b = new Process2();

        a.run();
        b.run();

    }
}

4. Golang

4.1. Complex Process

type ExportExcelUtilImpl struct {
}

func NewExportExcelUtilImpl() *ExportExcelUtilImpl {
	return &ExportExcelUtilImpl{}
}

func (e ExportExcelUtilImpl) Export() *Excel {
	excel := &Excel{}
	excel.Name = "export"
	// Read from the database
	data := e.readFromDB()
	// Render to Excel
	renderToExcel(excel, data)
	// Return Excel
	return excel
}

func (e ExportExcelUtilImpl) readFromDB() []byte {
	return nil
}

func renderToExcel(excel *Excel, data []byte) {
	excel.Data = data
}

4.2. Facade

/* Product returned by the utility class */
type Excel struct {
	Name string
	Data []byte
}

func (e Excel) ToString() string {
	return e.Name
}

/* Excel export utility */
type ExportExcelUtil interface {
	Export() Excel
}

4.3. Client

func main() {
	fmt.Println(structural.NewExportExcelUtilImpl().Export().ToString())
}

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub