Home >Java >javaTutorial >Commonly used design patterns and anti-patterns in Java frameworks

Commonly used design patterns and anti-patterns in Java frameworks

WBOY
WBOYOriginal
2024-06-04 17:49:07362browse

Design patterns and anti-patterns in Java framework: Design patterns: Factory pattern: Simplifies object creation without specifying specific classes. Singleton mode: Ensure there is only one instance to facilitate the creation of global objects. Anti-Pattern: God Class: Too many behaviors and responsibilities, making the code difficult to maintain. Sausage anti-pattern: processes depend on each other, making code difficult to trace and execute.

Commonly used design patterns and anti-patterns in Java frameworks

Common design patterns and anti-patterns in Java frameworks

In Java frameworks, design patterns and anti-patterns are widely used to create flexible, maintainable code. This article will introduce some common design patterns and anti-patterns and demonstrate their application through practical cases.

Design Pattern

Factory Pattern:
Factory pattern is used to create instances of objects without specifying their concrete classes. It allows the creation of different types of objects without modifying client code.

public class Factory {

    public static Shape getShape(String type) {
        switch (type) {
            case "Circle":
                return new Circle();
            case "Rectangle":
                return new Rectangle();
            default:
                return null;
        }
    }
}

public class Main {

    public static void main(String[] args) {
        Shape shape = Factory.getShape("Circle");
        shape.draw();  // 输出:绘制圆形
    }
}

Singleton pattern:
The singleton pattern ensures that a class has only one instance. It is used to create global objects such as database connections.

public class Singleton {

    private static Singleton instance;

    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

public class Main {

    public static void main(String[] args) {
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();

        System.out.println(instance1 == instance2);  // 输出:true
    }
}

Anti-pattern

God class (God Object) anti-pattern:
The God class is a class that contains too many behaviors and responsibilities. This makes the code difficult to maintain and understand.

public class GodObject {

    // 大量的方法和字段...

    public void doSomething() {
        // 复杂的行为...
    }

    public void doSomethingElse() {
        // 另一个复杂的行为...
    }
}

Sausage Anti-Pattern (Spaghetti Code) Anti-Pattern:
The Spaghetti Code anti-pattern is a process with a large number of interdependencies in the code. This makes it difficult to understand and track code execution.

public class SpaghettiCode {

    public void foo() {
        bar();
        if (condition) {
            baz();
        } else {
            qux();
        }
    }

    public void bar() {
        // 依赖于 foo()
    }

    public void baz() {
        // 依赖于 foo()
    }

    public void qux() {
        // 依赖于 foo()
    }
}

By understanding and applying these design patterns and anti-patterns, developers can create more robust and maintainable Java framework code.

The above is the detailed content of Commonly used design patterns and anti-patterns in Java frameworks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn