NOTE

Adapter Pattern

The adapter pattern is used to make an old-system interface compatible with a new interface, with Java and Go examples.

Software Architecture & EngineeringCreated Updated 1 min readhistorical

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

1. Definition

Make an old-system interface compatible.

2. UML

PlantUML 图表

3. Java

3.1. Client

public class Client
{
    public static void main(String[] args)
    {
        // The original interface had no parameters; now it needs a string parameter
        Converter converter = new ConverterImpl(new Target());
        converter.doSomething("test");
    }
}

3.2. Adaptee

public class Target
{
    public void doSothing()
    {
        System.out.println("do something");
    }
}

3.3. Converter

public interface Converter
{
    void doSomething(String type);
}
public class ConverterImpl implements Converter
{
    private Target target;

    public ConverterImpl(Target target)
    {
        this.target = target;
    }

    @Override
    public void doSomething(String type)
    {
        System.out.println(type);
        this.target.doSothing();
    }
}

4. Golang

4.1. Old Interface

/* Old interface */
type IOldUserService interface {
	CreateUser(name string, age int, height int, isReal bool)
}

type OldUserServiceImpl struct {
}

func NewOldUserServiceImpl() *OldUserServiceImpl {
	return &OldUserServiceImpl{}
}

func (t OldUserServiceImpl) CreateUser(name string, age int, height int, isReal bool) {
	fmt.Printf("User name=[%s], age=[%d], height=[%d], isReal=[%t]
",
		name, age, height, isReal)
}

4.2. New Interface

/* New interface */
type INewUserService interface {
	CreateUser(name string, age int, height int)
}

type NewUserServiceImpl struct {
}

func NewNewUserServiceImpl() *NewUserServiceImpl {
	return &NewUserServiceImpl{}
}

func (n NewUserServiceImpl) CreateUser(name string, age int, height int) {
	fmt.Printf("User name=[%s], age=[%d], height=[%d]
",
		name, age, height)
}

4.3. Converter

/* The new interface is now used, but the old interface cannot yet be removed, so an adapter is needed for compatibility */
type IConverter interface {
	CreateUser(name string, age int, height int)
}

type Converter struct {
	OldUserService IOldUserService
}

func NewConverter(oldUserService IOldUserService) *Converter {
	return &Converter{OldUserService: oldUserService}
}

func (c Converter) CreateUser(name string, age int, height int) {
	c.OldUserService.CreateUser(name, age, height, true)
}

4.4. Client

func main() {
	// The new interface accepts only three parameters
	newUserServiceImpl := 结构型模式.NewNewUserServiceImpl()
	newUserServiceImpl.CreateUser("[REDACTED]", 24, 172)

	// The old interface accepts four parameters; an adapter is needed to convert it to the new interface format
	oldUserServiceImpl := 结构型模式.NewOldUserServiceImpl()
	converter := 结构型模式.NewConverter(oldUserServiceImpl)
	converter.CreateUser("[REDACTED]", 24, 172)
}

Discussion

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