« Flyweight pattern » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
(Page créée avec « Category:CSharp Category:Design Patterns = Description = * Use sharing to support large numbers of fine-grained objects efficiently »)
 
Aucun résumé des modifications
Ligne 3 : Ligne 3 :
= Description =
= Description =
* Use sharing to support large numbers of fine-grained objects efficiently
* Use sharing to support large numbers of fine-grained objects efficiently
= Exemple =
<filebox fn='User.cs'>
public class User
{
    public string Name { get; set; }
    public override string ToString() => $"Simple user: {Name}";
}
</filebox>
<filebox fn='Administrator.cs'>
public class Administrator : User
{
    public override string ToString() => $"Admin user: {Name}";
}
</filebox>
<filebox fn='UserFactory.cs'>
public class UserFactory
{
    IDictionary<UserType, User> userTemplatesByUserType = new Dictionary<UserType, User>();
    public int TotalObjectsCreated
    {
        get { return userTemplatesByUserType.Count; }
    }
    public User GetUserFromFactory(UserType userType)
    {
        User userTemplate = null;
        if (userTemplatesByUserType.ContainsKey(userType))
        {
            userTemplate = userTemplatesByUserType[userType];
        }
        else
        {
            switch (userType)
            {
                case UserType.User:
                    userTemplate = new User();
                    userTemplatesByUserType.Add(UserType.User, userTemplate);
                    break;
                case UserType.Administrator:
                    userTemplate = new Administrator();
                    userTemplatesByUserType.Add(UserType.Administrator, userTemplate);
                    break;
                case UserType.Guest:
                    userTemplate = new Guest();
                    userTemplatesByUserType.Add(UserType.Guest, userTemplate);
                    break;
                default:
                    throw new Exception($"Unknown user type {userType}.");
            }
        }
        return userTemplate;
    }
}
</filebox>
<kode lang='cs'>
var userFactory = new UserFactory();
var user = userFactory.GetUserFromFactory(UserType.User);
user.Name = "Nicolas";
Console.WriteLine(user);
for (var i = 0; i < 10; i++)
{
    // call only once the Administrator ctor then reuse the instance
    user = userFactory.GetUserFromFactory(UserType.Administrator);
    user.Name = $"Admin{i}";
    Console.WriteLine(user);
}
</kode>

Version du 7 juin 2020 à 12:52

Description

  • Use sharing to support large numbers of fine-grained objects efficiently

Exemple

User.cs
public class User
{
    public string Name { get; set; }

    public override string ToString() => $"Simple user: {Name}";
}
Administrator.cs
public class Administrator : User
{
    public override string ToString() => $"Admin user: {Name}";
}
UserFactory.cs
public class UserFactory
{
    IDictionary<UserType, User> userTemplatesByUserType = new Dictionary<UserType, User>();

    public int TotalObjectsCreated
    {
        get { return userTemplatesByUserType.Count; }
    }

    public User GetUserFromFactory(UserType userType)
    {
        User userTemplate = null;
        if (userTemplatesByUserType.ContainsKey(userType))
        {
            userTemplate = userTemplatesByUserType[userType];
        }
        else
        {
            switch (userType)
            {
                case UserType.User:
                    userTemplate = new User();
                    userTemplatesByUserType.Add(UserType.User, userTemplate);
                    break;
                case UserType.Administrator:
                    userTemplate = new Administrator();
                    userTemplatesByUserType.Add(UserType.Administrator, userTemplate);
                    break;
                case UserType.Guest:
                    userTemplate = new Guest();
                    userTemplatesByUserType.Add(UserType.Guest, userTemplate);
                    break;
                default:
                    throw new Exception($"Unknown user type {userType}.");
            }
        }

        return userTemplate;
    }
}
Cs.svg
var userFactory = new UserFactory();
var user = userFactory.GetUserFromFactory(UserType.User);
user.Name = "Nicolas";
Console.WriteLine(user);

for (var i = 0; i < 10; i++)
{
    // call only once the Administrator ctor then reuse the instance
    user = userFactory.GetUserFromFactory(UserType.Administrator);
    user.Name = $"Admin{i}";
    Console.WriteLine(user);
}