设计模式-代理模式

来源:互联网 发布:校准时间软件 编辑:程序博客网 时间:2024/04/30 17:33

生活中的例子

要找老板谈话,首先可能遇到秘书,秘书说他可以代理老板处理一些事情,并且可以电话老板获得指示.

秘书就是这个场景中的代理类,他代理了老板,并提供了一些附加方法.

在代理模式中

1.代理类对象和真实类对象拥有相同的接口

2.代理类中含有真实类的私有引用,可以使得外界和真实类进行交互,并提供一些附加的操作或方法.

下面以论坛访问的权限控制为例

java实现代码:

package com.liu.pattern12;public class TestProxy {public static void main(String[] args) {// TODO Auto-generated method stubUser u1 = new User("liu","user");User u2 = new User("zhao","non");ForumProxy proxy = new ForumProxy(u1);proxy.access();proxy = new ForumProxy(u2);proxy.access();}}class User{private String name;private String type;public String getName() {return name;}public User(String name, String type) {this.name = name;this.type = type;}public void setName(String name) {this.name = name;}public String getType() {return type;}public void setType(String type) {this.type = type;}}interface IForum{public void access();}class Forum implements IForum{private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}private String type;public Forum(String name, String type) {this.name = name;this.type = type;}public String getType() {return type;}public void setType(String type) {this.type = type;}public void access(){System.out.println(name+"("+type+")可以访问!");}}class ForumProxy implements IForum{private Forum forum;public ForumProxy(User user){forum = new Forum(user.getName(),user.getType());}@Overridepublic void access() {// TODO Auto-generated method stubif("user".equals(forum.getType())){forum.access();}else{System.out.println(forum.getName()+ ",没有权限不能访问!");}}}


 

c#实现代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace com.liu.pattern12{    class TestProxy    {        static void Main(string[] args)        {            User u1 = new User("liu", "user");            User u2 = new User("zhao", "non");            ForumProxy proxy = new ForumProxy(u1);            proxy.Access();            proxy = new ForumProxy(u2);            proxy.Access();            System.Console.ReadKey();        }    }    class User{    private String _name;    private String _type;        public String Name{            get{return _name;}            set{_name=value;}        }        public String Type{            get{return _type;}            set{_type=value;}        }    public User(String name, String type) {    _name = name;            _type = type;    }     }    interface IForum{    void Access();    }class Forum : IForum{private String _name;private String _type;    public String Name{        get{return _name;}        set{_name=value;}    }    public String Type{        get{return _type;}        set{_type=value;}    }    public Forum(String name, String type) {    _name = name;            _type = type;    }    public void Access(){System.Console.WriteLine(_name+"("+_type+")可以访问!");}}class ForumProxy : IForum{private Forum forum;public ForumProxy(User user){forum = new Forum(user.Name,user.Type);}public void Access() {// TODO Auto-generated method stubif("user".Equals(forum.Type)){forum.Access();}else{            System.Console.WriteLine(forum.Name + ",没有权限不能访问!");}}}}


 

0 0
原创粉丝点击