Java中的主要设计模式

Java中的设计模式是一套被广泛认可的解决特定问题的解决方案。它们分为三大类:创建型模式、结构型模式和行为型模式。以下是一些常见的设计模式及其简要介绍和代码实现示例:

创建型模式

  1. 单例模式(Singleton): 确保一个类只有一个实例,并提供一个全局访问点。

    java 复制代码
    public class Singleton {
        private static Singleton instance;
        private Singleton() {}
    
        public static synchronized Singleton getInstance() {
            if (instance == null) {
                instance = new Singleton();
            }
            return instance;
        }
    }
  2. 工厂方法模式(Factory Method): 定义一个用于创建对象的接口,让子类决定实例化哪一个类。

    java 复制代码
    interface Product {
        void use();
    }
    
    class ConcreteProduct implements Product {
        public void use() {
            System.out.println("Using ConcreteProduct");
        }
    }
    
    abstract class Creator {
        abstract Product factoryMethod();
    }
    
    class ConcreteCreator extends Creator {
        public Product factoryMethod() {
            return new ConcreteProduct();
        }
    }
  3. 建造者模式(Builder): 用于创建一个复杂对象,同时允许用户只通过指定复杂对象的类型和内容就能构建它们。

    java 复制代码
    class Product {
        private String part1;
        private String part2;
    
        public static class Builder {
            private String part1;
            private String part2;
    
            public Builder setPart1(String part1) {
                this.part1 = part1;
                return this;
            }
    
            public Builder setPart2(String part2) {
                this.part2 = part2;
                return this;
            }
    
            public Product build() {
                return new Product(this.part1, this.part2);
            }
        }
    
        private Product(String part1, String part2) {
            this.part1 = part1;
            this.part2 = part2;
        }
    }

结构型模式

  1. 适配器模式(Adapter): 允许对象间的接口不兼容问题通过一个"适配器"来解决。

    java 复制代码
    interface Target {
        void request();
    }
    
    class Adaptee {
        public void specificRequest() {
            System.out.println("Specific Request");
        }
    }
    
    class Adapter implements Target {
        private Adaptee adaptee;
    
        public Adapter(Adaptee adaptee) {
            this.adaptee = adaptee;
        }
    
        public void request() {
            adaptee.specificRequest();
        }
    }
  2. 装饰器模式(Decorator): 动态地给一个对象添加额外的职责。

    java 复制代码
    interface Component {
        void operate();
    }
    
    class ConcreteComponent implements Component {
        public void operate() {
            System.out.println("ConcreteComponent");
        }
    }
    
    abstract class Decorator implements Component {
        protected Component component;
    
        public Decorator(Component component) {
            this.component = component;
        }
    
        public void operate() {
            component.operate();
        }
    }
    
    class ConcreteDecoratorA extends Decorator {
        public ConcreteDecoratorA(Component component) {
            super(component);
        }
    
        public void operate() {
            super.operate();
            // Add additional behavior
            System.out.println("ConcreteDecoratorA");
        }
    }

行为型模式

  1. 策略模式(Strategy): 定义一系列算法,把它们一个个封装起来,并使它们可互换。

    java 复制代码
    interface Strategy {
        void execute();
    }
    
    class ConcreteStrategyA implements Strategy {
        public void execute() {
            System.out.println("Strategy A");
        }
    }
    
    class Context {
        private Strategy strategy;
    
        public Context(Strategy strategy) {
            this.strategy = strategy;
        }
    
        public void executeStrategy() {
            strategy.execute();
        }
    }
  2. 观察者模式(Observer): 对象间的一对多依赖关系,当一个对象改变状态时,所有依赖于它的对象都会得到通知并自动更新。

    java 复制代码
    interface Observer {
        void update();
    }
    
    class ConcreteObserver implements Observer {
        public void update() {
            System.out.println("Observer updated");
        }
    }
    
    class Subject {
        private List<Observer> observers = new ArrayList<>();
    
        public void attach(Observer observer) {
            observers.add(observer);
        }
    
        public void notifyObservers() {
            for (Observer observer : observers) {
                observer.update();
            }
        }
    }
相关推荐
月光水岸New1 小时前
Ubuntu 中建的mysql数据库使用Navicat for MySQL连接不上
数据库·mysql·ubuntu
狄加山6751 小时前
数据库基础1
数据库
我爱松子鱼1 小时前
mysql之规则优化器RBO
数据库·mysql
闲猫1 小时前
go orm GORM
开发语言·后端·golang
丁卯4041 小时前
Go语言中使用viper绑定结构体和yaml文件信息时,标签的使用
服务器·后端·golang
chengooooooo1 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
Tirzano2 小时前
springsecurity自定义认证
spring boot·spring
Rverdoser2 小时前
【SQL】多表查询案例
数据库·sql
Galeoto3 小时前
how to export a table in sqlite, and import into another
数据库·sqlite
希忘auto3 小时前
详解Redis在Centos上的安装
redis·centos