设计模式 -- 策略模式
发布日期:2021-05-14 12:39:23 浏览次数:20 分类:精选文章

本文共 3244 字,大约阅读时间需要 10 分钟。

���������������Strategy Pattern���

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Context������������������������������������������������������������������������������������

���������������������������

������������������������������������

  • ���������������Algorithm Interface���������������������������������������������������������������������������������
  • ������������������Strategy Implementations���������������������������������������������������������������������������������
  • ������������Context���������������������������������������������������������������������������������������
  • ���������������������

    ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������

    ���������������������

  • ������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������
  • ���������������������

  • ���������������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������
  • ���������������������������

  • ������������������������������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������
  • ���������������������������������

    package com.designPattern.strategy;
    public class StrategyTest {
    public static void main(String[] args) {
    // ������������������������
    ContextGc a = new ContextGc(new GcA());
    a.show();
    // ������������������������
    ContextGc b = new ContextGc(new GcB());
    b.show();
    // ������������������������
    ContextGc c = new ContextGc(new GcC());
    c.show();
    }
    }
    interface GcStrategy {
    void algorithm();
    }
    class GcA implements GcStrategy {
    @Override
    public void algorithm() {
    System.out.println("������������������");
    }
    }
    class GcB implements GcStrategy {
    @Override
    public void algorithm() {
    System.out.println("������������������");
    }
    }
    class GcC implements GcStrategy {
    @Override
    public void algorithm() {
    System.out.println("������������������");
    }
    }
    class ContextGc {
    private GcStrategy strategy;
    public ContextGc(GcStrategy strategy) {
    this.strategy = strategy;
    }
    public void show() {
    strategy.algorithm();
    }
    }

    ������

    ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:jsp自定义标签库引用java属性值
    下一篇:设计模式 -- 外观模式(门面模式)

    发表评论

    最新留言

    留言是一种美德,欢迎回访!
    [***.207.175.100]2025年04月18日 22时30分48秒