Can the Flyweight Pattern be used with mutable objects or not?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I recently learned about Flyweight Pattern from this link.
It is written there " it’s very important that the flyweight objects are immutable: any operation on the state must be performed by the factory. "
I did not understand this well.
design-patterns object-oriented
New contributor
Mohammad Nazmul Hossain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
favorite
I recently learned about Flyweight Pattern from this link.
It is written there " it’s very important that the flyweight objects are immutable: any operation on the state must be performed by the factory. "
I did not understand this well.
design-patterns object-oriented
New contributor
Mohammad Nazmul Hossain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I recently learned about Flyweight Pattern from this link.
It is written there " it’s very important that the flyweight objects are immutable: any operation on the state must be performed by the factory. "
I did not understand this well.
design-patterns object-oriented
New contributor
Mohammad Nazmul Hossain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I recently learned about Flyweight Pattern from this link.
It is written there " it’s very important that the flyweight objects are immutable: any operation on the state must be performed by the factory. "
I did not understand this well.
design-patterns object-oriented
design-patterns object-oriented
New contributor
Mohammad Nazmul Hossain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Mohammad Nazmul Hossain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 3 hours ago
Kilian Foth
86.1k32231260
86.1k32231260
New contributor
Mohammad Nazmul Hossain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 6 hours ago


Mohammad Nazmul Hossain
167
167
New contributor
Mohammad Nazmul Hossain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Mohammad Nazmul Hossain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Mohammad Nazmul Hossain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
6
down vote
accepted
Flyweight objects must be immutable because our factory can only guarantee that it has remembered the correct object if it can also guarantee that the object it originally created has not been modified.
Flyweight objects provide a memory-efficient way to share common state between objects. This common state is called "intrinsic." Intrinsic state must be kept immutable, because if you change it, you change it for every object sharing the intrinsic state.
The immutable nature of the intrinsic state also allows the flyweight to be thread-safe.
There are several descriptions of Flyweight on the Internet; most of them are not very good. The best one I've found so far is here: https://refactoring.guru/design-patterns/flyweight
add a comment |Â
up vote
1
down vote
While the fly-weight pattern defines itself as having immutable 'fly-weight' objects, its will still achieve its goals of reducing RAM usage if you don't make them immutable.
The issue with making the objects mutable is that say for example your 'Tree' object has several properties, some are stored on a fly-weight object and some belong to the instance.
public class Tree
public int x get; set;
public int y get; set;
private static flyTree; //shared across many trees
public int Colour
get return flyTree.Colour;
set flyTree.Colour = value;
When you change, for example, the x and y coordinates they will change for that instance of Tree only. But when you change colour it will change for all Trees.
As a user of the Tree object you wont have an easy way to tell which properties belong to the tree and which belong to the underlying fly-weight object.
Making the fly-weight shared part of the Tree immutable prevents a developer accidentally changing a property on all Trees when they mean to change it only for one Tree.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
Flyweight objects must be immutable because our factory can only guarantee that it has remembered the correct object if it can also guarantee that the object it originally created has not been modified.
Flyweight objects provide a memory-efficient way to share common state between objects. This common state is called "intrinsic." Intrinsic state must be kept immutable, because if you change it, you change it for every object sharing the intrinsic state.
The immutable nature of the intrinsic state also allows the flyweight to be thread-safe.
There are several descriptions of Flyweight on the Internet; most of them are not very good. The best one I've found so far is here: https://refactoring.guru/design-patterns/flyweight
add a comment |Â
up vote
6
down vote
accepted
Flyweight objects must be immutable because our factory can only guarantee that it has remembered the correct object if it can also guarantee that the object it originally created has not been modified.
Flyweight objects provide a memory-efficient way to share common state between objects. This common state is called "intrinsic." Intrinsic state must be kept immutable, because if you change it, you change it for every object sharing the intrinsic state.
The immutable nature of the intrinsic state also allows the flyweight to be thread-safe.
There are several descriptions of Flyweight on the Internet; most of them are not very good. The best one I've found so far is here: https://refactoring.guru/design-patterns/flyweight
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
Flyweight objects must be immutable because our factory can only guarantee that it has remembered the correct object if it can also guarantee that the object it originally created has not been modified.
Flyweight objects provide a memory-efficient way to share common state between objects. This common state is called "intrinsic." Intrinsic state must be kept immutable, because if you change it, you change it for every object sharing the intrinsic state.
The immutable nature of the intrinsic state also allows the flyweight to be thread-safe.
There are several descriptions of Flyweight on the Internet; most of them are not very good. The best one I've found so far is here: https://refactoring.guru/design-patterns/flyweight
Flyweight objects must be immutable because our factory can only guarantee that it has remembered the correct object if it can also guarantee that the object it originally created has not been modified.
Flyweight objects provide a memory-efficient way to share common state between objects. This common state is called "intrinsic." Intrinsic state must be kept immutable, because if you change it, you change it for every object sharing the intrinsic state.
The immutable nature of the intrinsic state also allows the flyweight to be thread-safe.
There are several descriptions of Flyweight on the Internet; most of them are not very good. The best one I've found so far is here: https://refactoring.guru/design-patterns/flyweight
answered 4 hours ago
Robert Harvey
163k40373587
163k40373587
add a comment |Â
add a comment |Â
up vote
1
down vote
While the fly-weight pattern defines itself as having immutable 'fly-weight' objects, its will still achieve its goals of reducing RAM usage if you don't make them immutable.
The issue with making the objects mutable is that say for example your 'Tree' object has several properties, some are stored on a fly-weight object and some belong to the instance.
public class Tree
public int x get; set;
public int y get; set;
private static flyTree; //shared across many trees
public int Colour
get return flyTree.Colour;
set flyTree.Colour = value;
When you change, for example, the x and y coordinates they will change for that instance of Tree only. But when you change colour it will change for all Trees.
As a user of the Tree object you wont have an easy way to tell which properties belong to the tree and which belong to the underlying fly-weight object.
Making the fly-weight shared part of the Tree immutable prevents a developer accidentally changing a property on all Trees when they mean to change it only for one Tree.
add a comment |Â
up vote
1
down vote
While the fly-weight pattern defines itself as having immutable 'fly-weight' objects, its will still achieve its goals of reducing RAM usage if you don't make them immutable.
The issue with making the objects mutable is that say for example your 'Tree' object has several properties, some are stored on a fly-weight object and some belong to the instance.
public class Tree
public int x get; set;
public int y get; set;
private static flyTree; //shared across many trees
public int Colour
get return flyTree.Colour;
set flyTree.Colour = value;
When you change, for example, the x and y coordinates they will change for that instance of Tree only. But when you change colour it will change for all Trees.
As a user of the Tree object you wont have an easy way to tell which properties belong to the tree and which belong to the underlying fly-weight object.
Making the fly-weight shared part of the Tree immutable prevents a developer accidentally changing a property on all Trees when they mean to change it only for one Tree.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
While the fly-weight pattern defines itself as having immutable 'fly-weight' objects, its will still achieve its goals of reducing RAM usage if you don't make them immutable.
The issue with making the objects mutable is that say for example your 'Tree' object has several properties, some are stored on a fly-weight object and some belong to the instance.
public class Tree
public int x get; set;
public int y get; set;
private static flyTree; //shared across many trees
public int Colour
get return flyTree.Colour;
set flyTree.Colour = value;
When you change, for example, the x and y coordinates they will change for that instance of Tree only. But when you change colour it will change for all Trees.
As a user of the Tree object you wont have an easy way to tell which properties belong to the tree and which belong to the underlying fly-weight object.
Making the fly-weight shared part of the Tree immutable prevents a developer accidentally changing a property on all Trees when they mean to change it only for one Tree.
While the fly-weight pattern defines itself as having immutable 'fly-weight' objects, its will still achieve its goals of reducing RAM usage if you don't make them immutable.
The issue with making the objects mutable is that say for example your 'Tree' object has several properties, some are stored on a fly-weight object and some belong to the instance.
public class Tree
public int x get; set;
public int y get; set;
private static flyTree; //shared across many trees
public int Colour
get return flyTree.Colour;
set flyTree.Colour = value;
When you change, for example, the x and y coordinates they will change for that instance of Tree only. But when you change colour it will change for all Trees.
As a user of the Tree object you wont have an easy way to tell which properties belong to the tree and which belong to the underlying fly-weight object.
Making the fly-weight shared part of the Tree immutable prevents a developer accidentally changing a property on all Trees when they mean to change it only for one Tree.
answered 1 hour ago


Ewan
36.1k32981
36.1k32981
add a comment |Â
add a comment |Â
Mohammad Nazmul Hossain is a new contributor. Be nice, and check out our Code of Conduct.
Mohammad Nazmul Hossain is a new contributor. Be nice, and check out our Code of Conduct.
Mohammad Nazmul Hossain is a new contributor. Be nice, and check out our Code of Conduct.
Mohammad Nazmul Hossain is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f381097%2fcan-the-flyweight-pattern-be-used-with-mutable-objects-or-not%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password