Composite Mode C # implementation

Source: Internet
Author: User

Allows you to set permissions for users and user groups. Users in A group have the same permissions.

1 // user and group parent interface
2 public interface userbase
3 {
4 void addchild (userbase U); // Add a user or group
5 void setpurview (string purview); // Set permissions
6}
7 // user
8 public class user: userbase
9 {
10 private string purview = "";
11
12 public void addchild (userbase C)
13 {
14 throw new exception ("method not supported! ");
15
16}
17
18 public void setpurview (string strpurview)
19 {
20 // Set User Permissions
21 purview = strpurview;
22}
23}
24 // User Group.
25 public class group: userbase
26 {
27 private arraylist userlist = new arraylist ();
28
29 public void addchild (userbase c) // Add a user
30 {
31 userlist. Add (C );
32}
33
34 public void setpurview (string strpurview)
35 {
36 foreach (userbase U in userlist)
37 U. setpurview (strpurview );
38}
39}
40
41 class mymain
42 {
43 public static void main ()
44 {
45 // create a user group
46 group group1 = new group (); // User Group 1
47 group group2 = new group (); // User Group 2
48 user user1 = new user (); // user 1
49 user user2 = new user (); // user 2
50 user user3 = new user (); // user 3
51
52
53 // Add two users to the group
54 group1.addchild (user1 );
55 group1.addchild (user2 );
56
57 group2.addchild (user1 );
58
59 group2.addchild (group1 );
60
61 group1.setpurview ("permission 1"); // sets the Group permission. The group user permission is set.
62 group2.setpurview ("permission 2 ");
63
64 user3.setpurview ("permission 2"); // set user 3 permissions}
65}
66
67

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.