14. 프락스 패턴(Proxy Pattern) - 보호용 프락시
"본문 내용"
14. 프락시 패턴(Proxy Pattern) – 보호용 프락시
▶ IView.cs
namespace ProtectionProxy
{
interface IView
{
void View();
string Owner
{
get;
}
}
}
▶ Picture.cs
using System;
namespace ProtectionProxy
{
class Picture:IView
{
string name;
public string Owner
{
get;
private set;
}
public Picture(string name,string owner)
{
this.name = name;
Owner = owner;
}
public void View()
{
Console.WriteLine("파일명:{0} 소유자:{1}",name,Owner);
}
}
}
▶ ProtectionPicture.cs
using System;
using System.Collections.Generic;
namespace ProtectionProxy
{
class ProtectionPicture:IView
{
Picture picture = null;
string user = string.Empty;
List<string> available_users = new List<string>();
public ProtectionPicture(string name,string owner)
{
picture = new Picture(name,owner);
}
public void View()
{
if(IsAvailableUser(user))
{
picture.View();
}
else
{
Console.WriteLine("사진을 볼 수 있는 권한이 없습니다.");
}
}
public string Owner
{
get
{
return picture.Owner;
}
}
private bool IsAvailableUser(string user)
{
if(picture.Owner == user)
{
return true;
}
foreach(string name in available_users)
{
if(name == user)
{
return true;
}
}
return false;
}
public void AddAvailableUser(string owner,string user)
{
if(owner != Owner)
{
Console.WriteLine("소유자만이 사용 권한 설정을 할 수 있습니다.");
return;
}
if(IsAvailableUser(user)==false)
{
available_users.Add(user);
}
}
public void SetUser(string user)
{
this.user = user;
}
}
}
▶ PictureManager.cs
using System;
using System.Collections.Generic;
namespace ProtectionProxy
{
class PictureManager
{
List<IView> pictures = new List<IView>();
string login_user = string.Empty;
public void Login(string user)
{
login_user = user;
Console.WriteLine("{0} 님께서 로긴 하였습니다.",login_user);
}
public void MakePicture(string name)
{
if(login_user != string.Empty)
{
pictures.Add(new Picture(name,login_user));
}
}
public void MakePicture(string name,List<string> available_users)
{
if(login_user != string.Empty)
{
ProtectionPicture pro_picture =
MakeProtectionPicture(name,available_users);
pictures.Add(pro_picture);
}
}
public void ListPicture()
{
ProtectionPicture pp=null;
foreach(IView ivew in pictures)
{
pp = ivew as ProtectionPicture;
if(pp != null)
{
pp.SetUser(login_user);
}
ivew.View();
}
}
public void Logout()
{
Console.WriteLine("{0}님께서 로그 아웃 하였습니다.",login_user);
login_user = string.Empty;
}
ProtectionPicture MakeProtectionPicture(string name,List<string> available_users)
{
ProtectionPicture pro_picture = new ProtectionPicture(name,login_user);
foreach(string uname in available_users)
{
pro_picture.AddAvailableUser(login_user,uname);
}
return pro_picture;
}
}
}
▶ Program.cs
using System.Collections.Generic;
namespace ProtectionProxy
{
class Program
{
static void Main(string[] args)
{
PictureManager pm = new PictureManager();
List<string> avail_users = new List<string>();
pm.Login("홍길동");
pm.MakePicture("홍길동A", avail_users);
avail_users.Add("강감찬");
pm.MakePicture("홍길동B", avail_users);
pm.MakePicture("홍길동C");
pm.ListPicture();
pm.Logout();
pm.Login("강감찬");
pm.ListPicture();
pm.Logout();
}
}
}
'.NET > 설계 패턴(C#)' 카테고리의 다른 글
[설계 패턴 C#] 16. 명령 패턴(Command Pattern) (0) | 2016.12.06 |
---|---|
[설계 패턴 C#] 15. 책임 연쇄 패턴(Chain of Responsibility Pattern) (0) | 2016.12.05 |
[설계 패턴 C#]13. 프락스 패턴(Proxy Pattern) - 가상 프락시 (0) | 2016.06.24 |
[설계 패턴 C#]12. 프락스 패턴(Proxy Pattern) - 원격지 프락시 (0) | 2016.06.23 |
[설계 패턴 C#] 11. 플라이급 패턴(Flyweight Pattern) (0) | 2016.06.23 |