美文网首页
设计模式用例(三)

设计模式用例(三)

作者: 月巴月巴 | 来源:发表于2018-12-31 12:28 被阅读0次
    1. Strategy 模式
    interface Evaluator {
        void eval(Candidate candidate);
    }
    
    class Candidate {
        private double techLevel;
        private double behaviorLevel;
        private double managingSkill;
        private Evaluator evaluator;
    
        public void setEvaluator(Evaluator evaluator) {
            this.evaluator = evaluator;
        }
    
        public Candidate(double techLevel, double behaviorLevel, double managingSkill) {
            this.techLevel = techLevel;
            this.behaviorLevel = behaviorLevel;
            this.managingSkill = managingSkill;
        }
    
        public double getTechLevel() {
            return techLevel;
        }
    
        public double getBehaviorLevel() {
            return behaviorLevel;
        }
    
        public double getManagingSkill() {
            return managingSkill;
        }
    
        public void evaluate() {
            if (evaluator == null) {
                evaluator = new TechValue();
            }
            evaluator.eval(this);
        }
    }
    
    class TechValue implements Evaluator {
        public void eval(Candidate candidate) {
            System.out.println("Tech value: " + (candidate.getTechLevel() * 0.8 + candidate.getBehaviorLevel() * 0.2));
        }
    }
    
    class ManagerValue implements Evaluator {
        public void eval(Candidate candidate) {
            System.out.println("Manage value: " + (candidate.getManagingSkill() * 0.6 + candidate.getBehaviorLevel() * 0.4));
        }
    }
    
    public class StrategyStudy {
        public static void main(String[] args) {
            Candidate engineer = new Candidate(80, 80, 50);
            engineer.setEvaluator(new TechValue());
            engineer.evaluate();
    
            engineer.setEvaluator(new ManagerValue());
            engineer.evaluate();
        }
    }
    //output:
    //Tech value: 80.0
    //Manage value: 62.0
    
    
    
    1. Bridge 模式
    public interface GameDevice {
        void pressBtn();
    }
    
    public interface Joystick {
        void press();
    }
    
    public class Computer implements GameDevice {
        private Joystick joystick;
    
        public Computer(Joystick joystick) {
            this.joystick = joystick;
        }
    
        @Override
        public void pressBtn() {
            System.out.println("Play Computer");
            joystick.press();
        }
    
        public void setJoystick(Joystick joystick) {
            this.joystick = joystick;
        }
    }
    
    public class XBox implements GameDevice {
        private Joystick joystick;
    
        public XBox(Joystick joystick) {
            this.joystick = joystick;
        }
    
        @Override
        public void pressBtn() {
            System.out.println("Play Xbox");
            joystick.press();
        }
    
        public void setJoystick(Joystick joystick) {
            this.joystick = joystick;
        }
    }
    
    public class LogiTechStick implements Joystick{
        @Override
        public void press() {
            System.out.println("Press Logi Tech joy stick");
        }
    }
    
    public class XBox360Stick implements Joystick {
        @Override
        public void press() {
            System.out.println("Press xbox 360 joy stick");
        }
    }
    
    public class XBoxOneStick implements Joystick {
        @Override
        public void press() {
            System.out.println("Press xbox One joy stick");
        }
    }
    
    public class BridgeStudy {
        public static void main(String[] args) {
            // change joystick implementation, no code change needed in GameDevice
            XBox xbox = new XBox(new XBoxOneStick());
            xbox.pressBtn();
            xbox.setJoystick(new XBox360Stick());
            xbox.pressBtn();
            // change game device implementation  no code change needed in JoyStick
            Computer computer = new Computer(new XBoxOneStick());
            computer.pressBtn();
            computer.setJoystick(new LogiTechStick());
            computer.pressBtn();
        }
    }/// output
    //Play Xbox
    //Press xbox One joy stick
    //Play Xbox
    //Press xbox 360 joy stick
    //Play Computer
    //Press xbox One joy stick
    //Play Computer
    //Press Logi Tech joy stick
    
    
    1. Iterator 模式
    class Score {
        private char[] notes = {'C', 'D', 'E', 'F', 'G', 'A', 'B'};
        ScoreIterator iterator() {
            return new ScoreIterator(this);
        }
    
        public char[] getNotes() {
            return notes;
        }
    }
    
    class ScoreIterator {
        private Score score;
        private int p = -1;
        ScoreIterator(Score score) {
            this.score = score;
        }
    
        char next() {
            if (hasNext()) {
                return score.getNotes()[++p];
            } else {
                throw new RuntimeException("out of bound");
            }
        }
    
        boolean hasNext() {
            return p < score.getNotes().length - 1;
        }
    
        void reset() {
            p = -1;
        }
    }
    
    public class IteratorPattern {
        public static void main(String[] args) {
            Score score = new Score();
            ScoreIterator iterator = score.iterator();
            while(iterator.hasNext()) {
                System.out.print(iterator.next());
            }
        }
    }
    // output
    // CDEFGAB
    

    相关文章

      网友评论

          本文标题:设计模式用例(三)

          本文链接:https://www.haomeiwen.com/subject/xbrslqtx.html