zoukankan      html  css  js  c++  java
  • 2021/11/24策略模式

    策略者模式

    java代码

    package Strategy;
    
    public class AirplaneStrategy implements TravelStrategy {
        @Override
        public void goTravel() {
            System.out.println("乘飞机去旅行,飞机策略");	
        }
    }
    
    package Strategy;
    
    public class BicycleStrategy implements TravelStrategy {
        @Override
        public void goTravel() {
            System.out.println("骑行,骑行策略");	
        }
    }
    
    package Strategy;
    
    public class CarStrategy implements TravelStrategy {
        @Override
        public void goTravel() {
            System.out.println("自驾游,自驾策略");	
        }
    }
    
    package Strategy;
    
    public class FootStrategy implements TravelStrategy {
        @Override
        public void goTravel() {
            System.out.println("徒步旅行,徒步策略。");	
        }
    }
    
    package Strategy;
    
    public class StrategyTest {
        public static void main(String[] args) {
            Traveler traveler = new Traveler();
            
            traveler.setTravelStrategy(new AirplaneStrategy());
            traveler.goTravel();
            System.out.println();
            
            traveler.setTravelStrategy(new CarStrategy());
            traveler.goTravel();
            System.out.println();
            
            traveler.setTravelStrategy(new BicycleStrategy());
            traveler.goTravel();
            System.out.println();
            
            traveler.setTravelStrategy(new FootStrategy());
            traveler.goTravel();
            System.out.println();
        }
    }
    
    package Strategy;
    
    public class Traveler {
        private TravelStrategy travelStrategy;
    
        public TravelStrategy getTravelStrategy() {
            return travelStrategy;
        }
    
        public void setTravelStrategy(TravelStrategy travelStrategy) {
            this.travelStrategy = travelStrategy;
        }
        
        public void goTravel() {
            travelStrategy.goTravel();
        }
    }
    
    package Strategy;
    
    public interface TravelStrategy {
        void goTravel();
    }
    
  • 相关阅读:
    一条命令深度清理你的mac
    将以太坊封装为 ERC20
    golang subprocess tests
    go 笔记
    readme
    如何使用 channel
    修改vscode caipeiyu.writeCnblog ,简化博客发布
    thedao
    firefox 59 无法使用 pac 代理上网
    scrapy简单使用
  • 原文地址:https://www.cnblogs.com/qiangini/p/15600657.html
Copyright © 2011-2022 走看看