Can the Flyweight Pattern be used with mutable objects or not?

The name of the pictureThe name of the pictureThe name of the pictureClash 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.









share









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.























    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.









    share









    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.





















      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.









      share









      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





      share









      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.










      share









      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.








      share



      share








      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.




















          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






          share|improve this answer



























            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.






            share|improve this answer




















              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "131"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );






              Mohammad Nazmul Hossain is a new contributor. Be nice, and check out our Code of Conduct.









               

              draft saved


              draft discarded


















              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






























              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






              share|improve this answer
























                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






                share|improve this answer






















                  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






                  share|improve this answer












                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 4 hours ago









                  Robert Harvey

                  163k40373587




                  163k40373587






















                      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.






                      share|improve this answer
























                        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.






                        share|improve this answer






















                          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.






                          share|improve this answer












                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 1 hour ago









                          Ewan

                          36.1k32981




                          36.1k32981




















                              Mohammad Nazmul Hossain is a new contributor. Be nice, and check out our Code of Conduct.









                               

                              draft saved


                              draft discarded


















                              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.













                               


                              draft saved


                              draft discarded














                              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













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              List of Gilmore Girls characters

                              Confectionery