6种常用的设计模式

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

例子代码:


public class Singleton {
    //私有静态变量,存储唯一实例
    private static Singleton instance;

    //私有构造函数,防止外部实例化对象
    private Singleton() {
    }

    //公有静态方法,获取唯一实例
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
 

2. 工厂模式(Factory Pattern)
工厂模式通过定义一个创建对象的接口,让工厂类决定实例化哪一个类。

例子代码:


public interface Shape {
    void draw();
}

public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("画一个矩形。");
    }
}

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("画一个圆形。");
    }
}

public class ShapeFactory {
    public Shape getShape(String shapeType) {
        if (shapeType.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        } else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
            return new Rectangle();
        }
        return null;
    }
}
 

3. 观察者模式(Observer Pattern)
观察者模式定义了对象之间的一对多依赖关系,当一个对象状态改变时,所有依赖于它的对象都会得到通知并自动更新。

例子代码:


public interface Observer {
    void update(String message);
}

public class SubscriberOne implements Observer {
    @Override
    public void update(String message) {
        System.out.println("SubscriberOne 收到消息:" + message);
    }
}

public class SubscriberTwo implements Observer {
    @Override
    public void update(String message) {
        System.out.println("SubscriberTwo 收到消息:" + message);
    }
}

public interface Subject {
    void attach(Observer observer);

    void detach(Observer observer);

    void notifyObservers(String message);
}

public class WechatPublicAccount implements Subject {
    private List<Observer> observers = new ArrayList<>();

    // 新关注者
    @Override
    public void attach(Observer observer) {
        observers.add(observer);
    }

    // 取消关注
    @Override
    public void detach(Observer observer) {
        observers.remove(observer);
    }

    // 推送消息给关注者
    @Override
    public void notifyObservers(String message) {
        for (Observer observer : observers) {
            observer.update(message);
        }
    }
}
 

4. 装饰器模式(Decorator Pattern)
装饰器模式动态地将责任附加到对象上。若要扩展功能,装饰器模式提供了比继承更具有弹性的替代方案。

例子代码:


public interface Component {
    void operation();
}

public class ConcreteComponent implements Component {
    @Override
    public void operation() {
        System.out.println("执行 ConcreteComponent 的操作。");
    }
}

public abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    public void operation() {
        component.operation();
    }
}

public class ConcreteDecorator extends Decorator {
    public ConcreteDecorator(Component component) {
        super(component);
    }

    //添加额外功能
    private void addExtraOperation(){
        System.out.println("执行 ConcreteDecorator 的额外操作。");
    }

    public void operation() {
        super.operation();
        addExtraOperation();
    }
}
 

5. 代理模式(Proxy Pattern)
代理模式为其他对象提供一种代理以控制对这个对象的访问。

例子代码:


public interface Image {
    void display();
}

public class RealImage implements Image {
    private String fileName;

    public RealImage(String fileName) {
        this.fileName = fileName;
        loadFromDisk(fileName);
    }

    private void loadFromDisk(String fileName) {
        System.out.println("从硬盘加载 " + fileName);
    }

    public void display() {
        System.out.println("显示 " + fileName);
    }
}

public class ProxyImage implements Image {
    private RealImage realImage;
    private String fileName;

    public ProxyImage(String fileName) {
        this.fileName = fileName;
    }

    public void display() {
        if (realImage == null) {
            realImage = new RealImage(fileName);
        }
        realImage.display();
    }
}
 

6. 模板方法模式(Template Method Pattern)
模板方法模式定义了一个操作中的算法的骨架,而将一些步骤延迟到子类中。

例子代码:


public abstract class Game {
    abstract void initialize();

    abstract void startPlay();

    abstract void endPlay();

    public final void play() {
        initialize();
        startPlay();
        endPlay();
    }
}

public class Cricket extends Game {
    @Override
    void initialize() {
        System.out.println("Cricket 游戏初始化。");
    }

    @Override
    void startPlay() {
        System.out.println("Cricket 游戏开始。");
    }

    @Override
    void endPlay() {
        System.out.println("Cricket 游戏结束。");
    }
}

public class Football extends Game {
    @Override
    void initialize() {
        System.out.println("Football 游戏初始化。");
    }

    @Override
    void startPlay() {
        System.out.println("Football 游戏开始。");
    }

    @Override
    void endPlay() {
        System.out.println("Football 游戏结束。");
    }
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值