Updating Object Properties While Respecting Encapsulation

This article is a follow-up to my previous article Accessing Object Properties While Respecting Encapsulation. In that article I looked at making object properties accessible while respecting encapsulation. In this article I will look at how to design operations on objects in order to update property values while respecting encapsulation – that is, without resorting to the all too common “setter” (otherwise known as a “set” method).

By the end of my previous article we had the following:

        class Circle
        {
            public uint Radius
            {
                get { return radius_ ; }
            }

            // ...

            private int x_centre, y_centre;
            private uint radius_;

            // ...
        }

I used this fragment of a C# class to make the point that our approach should be to decide what properties we want to make publicly readable, and then decide how we’re to implement them; If the implementation just happens to be returning a data member, then that’s fine – but that is a private matter for the class.

Moving on, it seems reasonable that users of this class would also want to be able to update the size of the circle. Therefore, should we make it possible to assign to the Radius property? The answer is simple - NO! How then, can we make it possible to update a Circle object’s size?
To understand this, it is worth stepping back and looking at what we actually get from object oriented programming. In this case, I’m thinking specifically of how objects help us to represent the real world. With this in mind, when tempted to make an object property “settable”, we should stop for a minute and think about what we really want to achieve – and what we want to achieve is not the setting of the Radius property, but the resizing of the Circle object. Therefore, we should endeavour to say just that by having a Resize() method. The class now looks like this:

    class Circle
    {
        public uint Radius
        {
            get { return radius_ ; }
        }

        public void Resize(uint new_radius)
        {
            radius_ = new_radius;
        }

        // ...

        private int x_centre, y_centre;
        private uint radius_;

        // ...
    }

Here is a sample usage code fragment:

    class UsesCircle
    {
        static void Example(Circle the_circle)
        {
            uint new_radius = 42;
            the_circle.Resize(new_radius);
             //...
        }

        //...
    }

By designing the class’ operations with the real world in mind, once again we have code that speaks to its reader in more natural manner, and once again it achieves this simply by bearing in mind how the operation would be expressed in English. Looking back for moment, let me point out the question posed above: how can we make it possible to update a Circle object’s size? It is worth noting how simply asking the right question provided a strong clue to its answer!

Moving on, I now want to leave my Circle class example for the time being, and move on to emphasise another important point: there are many cases where it would actually be ridiculous to make a property value explicitly “settable”! My favourite example is a class to represent a bank account. First, just to strengthen the point, let me show this done the wrong way:

        class BankAccount
        {
            public long Balance
            {
                get { return balance_; }
                set { balance_ = value; }
            }

            // ...        

            private long balance_;

            // ...
        }

The point is this: do you know of a bank that will let you set your balance? If so, please get in touch and let me know which one! Real world bank accounts allow the user to do the following:

  • Query their balance
  • Make deposits
  • Make withdrawals

Therefore the operations on the BankAccount class should reflect this. Here is a fragment illustrating the design done sensibly:

        class BankAccount
        {
            public long Balance
            {
                get { return balance_; }
            }

            void Deposit(uint amount)
            {
                balance_ += amount;
            }

            void Withdraw(uint amount)
            {
                balance_ -= amount;
            }

            // ...        

            private long balance_;

            // ...
        }

Note that BankAccount could dispense with its Withdraw() method, and instead allow Deposit() to accept a negative amount. Which is the most natural approach rather depends on the application area in which BankAccount is being used, and it serves to illustrate the existence of alternatives.

That bring this matter to a conclusion. This article and its predecessor have, out of necessity, presented quite simple examples. Sadly, in reality and in practice, software development is far from simple. However, I hope the two articles will contribute to the clearing up of misunderstandings that appear to remain all too common.

Leave a comment

?>ungloryhole tug jobs rubs a huge cock public invasion mouth with cock NEXTDOORMALE TOMMYDXXX Ice La Fox Medical Femdom Bareblacking Confession Of Married Man publicinvasion tugjobs Charmane Star outinpublic.com hazehim Charmane Star publicnude publicinvasion Monsters Of Cock Brooke Banner VISCONTITRIPLETS tranny land magical feet haze him collegerules Summer Bailey shaved pussy hammered itsgonnahurt itsgonnahurt Blonde round ass submityourbitch BIGGEST ASS CRAVING collegerules.com out in public Gyno Orgasm Videos submityourbitch tight ass drilled Bang Bros haze him submit your bitch outinpublic.com Stephanie Cane hazehim.com NEXTDOORPASS Big tit fucked publicinvasion lucky stud in a hot threesome Big Mouthfuls SpecialExercises sexy blonde petite public invasion college rules pussy pounded Roxy Love publicinvasion outinpublic.com GAy Cumshots Extrem trannyland VINTAGEGAYLOOPS public invasion ungloryhole Nude sport videos Lacey lounging facialfest Hot tight pussy facial fest submityourbitch.com Licking Dick And Ass tugjobs public invasion milf lessons camel toe Aiden Aspen huge tits ungloryhole publicinvasion Aiden Aspen lesbian dildo drilling college rules outinpublic outinpublic lesbian lover licks Sexy big tits pornstar Beautiful hand job outinpublic.com Special Exercises hazehim collegerules.com submit your bitch Milf cravers trannyland publicinvasion hottest white girls fat juicy ass. outinpublic summer time milf Courtney Simpson public invasion bang pas hazehim milfsoup natural beautiful tits SpecialExamination submityourbitch Gia Malone Lesbiansportvideos smacked those asses ungloryhole Redhead BBW bait Bus NEXTDOORPASS Nudesportvideos CODYCUMMINGS Latina pornstar pussy publicinvasion its gonna hurt Blonde gets fucked hard collegerules Special Examination NEXTDOORHOOKUPS Vince Ferelli MALEDIGITAL CIndia Summers un glory hole submityourbitch.com public invasion busty adventures college rules Amy Reid milfsoup bang bus college rules evanrivers its gonna hurt collegerules fuck team five trannyland.com great ass public invasion MedicalFemdom out in public Lesbian sport videos itsgonnahurt NEXTDOORBUDDIES fat mature redhead GynoOrgasmVideos Big Mouthfuls assparade publicinvasion two beautiful asses 3DS Max 8 OEM3Q 3GP Video Converterdownload acdsee manager 2009 oemred eye remover pro 1.2cheapest windows vistacheapest windows vista ultimatecheap AutoCAD 2009Vision Backup Enterprisecheap Macromedia ColdFusionnero photoshow 5 downloadadobe photoshop cs oemcoldfusion mx 7 downloadkamagra sildenafil citratekamagra generic viagraorder caverta onlinecaverta 100buy cavertatadaliscasodex 50 mg tablet