【问题标题】:With Proxy Pattern and Without Proxy Pattern有代理模式和没有代理模式
【发布时间】:2015-07-31 14:22:32
【问题描述】:

我已经浏览了几个示例,但我没有找到任何显示相同示例的示例使用代理模式和不使用代理模式,

谁有一个通用的例子?看到这样的例子肯定会明白我们是否真的需要使用代理模式

【问题讨论】:

    标签: design-patterns proxy-pattern


    【解决方案1】:

    代理模式适用于需要比简单指针更通用或更复杂的对象引用。以下是代理模式适用的几种常见情况:

    • 控制对另一个对象的访问
    • 延迟初始化
    • 实现日志记录
    • 促进网络连接
    • 对对象的引用计数

    举个简单的例子,让我们考虑一个场景,一群巫师想要进入一座塔,但有一个代理保护资源,只允许前三个巫师通过。

    public class Wizard {
    
        private String name;
    
        public Wizard(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return name;
        }
    
    }
    
    public class WizardTower {
    
        public void enter(Wizard wizard) {
            System.out.println(wizard + " enters the tower.");
        }
    
    }
    
    public class WizardTowerProxy extends WizardTower {
    
        private static final int NUM_WIZARDS_ALLOWED = 3;
    
        private int numWizards;
    
        @Override
        public void enter(Wizard wizard) {
            if (numWizards < NUM_WIZARDS_ALLOWED) {
                super.enter(wizard);
                numWizards++;
            } else {
                System.out.println(wizard + " is not allowed to enter!");
            }
        }
    }
    
    public class App {
    
        public static void main(String[] args) {
    
            WizardTowerProxy tower = new WizardTowerProxy();
            tower.enter(new Wizard("Red wizard"));
            tower.enter(new Wizard("White wizard"));
            tower.enter(new Wizard("Black wizard"));
            tower.enter(new Wizard("Green wizard"));
            tower.enter(new Wizard("Brown wizard"));
    
        }
    }
    

    控制台输出:

    Red wizard enters the tower.
    White wizard enters the tower.
    Black wizard enters the tower.
    Green wizard is not allowed to enter!
    Brown wizard is not allowed to enter!
    

    根据要求,这里是相同的示例代码,现在将WizardTowerProxy 代码合并到WizardTower。

    public class Wizard {
    
        private String name;
    
        public Wizard(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return name;
        }
    }
    
    public class WizardTower {
    
        private static final int NUM_WIZARDS_ALLOWED = 3;
    
        private int numWizards;
    
        public void enter(Wizard wizard) {
            if (numWizards < NUM_WIZARDS_ALLOWED) {
                System.out.println(wizard + " enters the tower.");
                numWizards++;
            } else {
                System.out.println(wizard + " is not allowed to enter!");
            }
        }
    }
    
    public class App {
    
        public static void main(String[] args) {
    
            WizardTower tower = new WizardTower();
            tower.enter(new Wizard("Red wizard"));
            tower.enter(new Wizard("White wizard"));
            tower.enter(new Wizard("Black wizard"));
            tower.enter(new Wizard("Green wizard"));
            tower.enter(new Wizard("Brown wizard"));
        }
    }
    

    控制台输出:

    Red wizard enters the tower.
    White wizard enters the tower.
    Black wizard enters the tower.
    Green wizard is not allowed to enter!
    Brown wizard is not allowed to enter!
    

    【讨论】:

    • 感谢您的回复,我期待 2 个示例并排比较没有代理实现和代理实现。所以对于上面的示例,实现“只有三个第一个向导通过”约束/条件在单个分类示例中(即都写在向导类中)我猜应该是非代理示例???
    • @Sanjeewa 我添加了另一个具有相同功能的示例,这次没有代理。
    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 2011-03-04
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多