开发者

encapsulation means putting related items together?

开发者 https://www.devze.com 2023-04-02 02:00 出处:网络
I was going through an article about Object Oriented Programming and it stated that encapsulation means putting related items together, but I don\'t understand how the article\'s representative exampl

I was going through an article about Object Oriented Programming and it stated that encapsulation means putting related items together, but I don't understand how the article's representative example: UserProfile.js. Though this example is in JavaScript, I'm looking to understand these concepts in Java.

Can anyone explain me these two questions with a pseudo code:

  1. What is encapsulation?
  2. Why 开发者_JAVA百科do we need encapsulation with pseudo code?


Encapsulation isn't necessarily about putting related items together, it's a technique of hiding internal information of an object. I'm not sure if I agree with the premise of the author of the article you linked... I don't accept that a struct is really a method of encapsulation in the object-oriented sense of the word.

Encapsulation

Psudo [sic] code is a technique for explicitly writing coding logic without the need for syntactical constraints. Considering this context, I don't understand your second question.

Pseudocode


No, that is cohesion. Encapsulation is hidding things from who doesn't need them.


Michael has it correct.

In Object Oriented programming Encapsulation is the first pace. Encapsulation is the procedure of covering up of data and functions into a single unit (called class). An encapsulated object is often called an abstract data type.

ref: http://www.c-sharpcorner.com/UploadFile/ggaganesh/EncapsulationInCS11082005064310AM/EncapsulationInCS.aspx

Encapsulation is the hiding of the non-essential features.

So why do we need it.

Programing is about translating a solution to a problem into logical code to solve that problem. Because of this, there maybe many complicated methods and functions that we don't want Mr.Joe Blow developer to use. We will encapsulate (or BlackBox) those methods so they cannot be used (they are still used internally). This reduces complexity by only representing important functions and hiding others.

As for needing it in pseudo code, i'm not sure. Michael did a good job with explaining that.

I haven't had enough coffee to give a good example,Plus my Rubik's cube broke :(, i'll write one up for you.


The encapsulation stand for "hiding element for free usage", is a part of Object Oriented Programming paradigm.

It is used to specify the range of visibility elements of code.

Let assume that we have a class with field called password where the password is stored. If this password would be visible for everyone, then there would be no need for a password.

Additional thing is that this helps to maintain the code in order.


Encapsulation isn't goal in OODesign. It is only way to achieve the finest, needed abstraction.

What is encapsulation?

  • in specific - it means hiding properties from non-desirable access
  • in overall - it means hiding every project design decision which could be changed in future. Therefore in encapsulation we should consider also e.g. concrete method implementation. From this POV we encapsulate its behavior so that for some POV we don't want to know how it is doing it, knowing only what this method is doing. Encapsulation could be achieved also for example using inheritance mechanism!

How we use encapsulation/ Example - hide every class property. You could as why do we have to do so - it is much effort and unnecessary code! Consider simple example where you can set some int properties. In your scenario - this variable should be in specific range. If someone sets it wrong - how would you design workflow to prevent this action?

More sophisticated but still simple example are collections. In many cases we shouldn't provide full collections to your's object neighbourhood. Encapsulation allows you to provide every property client just a copy of your object. In some cases - it could be helpful.


I think to really understand and appreciate encapsulation you really need a little bit of history.

It used to be that if you wrote a program it would be kind of as though every line of code were printed on a single sheet of paper where everything has knowledge and access to everything else and there are no fancy constructs in which to hide or store variables out of site of your functions.

Lets say you are trying to write some program with 100 different functions and 100 variables. Can you imagine how disorganized and ugly that would get? Effectively, all that code is just a giant formless script that gets executed in some linear fashion and has no real structure, rhyme or reason to it other than that one line of code comes before another line of code and so on.

Encapsulation was invented to take a program like that and give it a skeletal structure, allowing you to hide and organize those 100 functions and variables into a sensible whole. In the case of your user info class here, they take everything that is relating to UserProfile and stick it in a "Capsule" so that it can only be accessed through a reference to to UserProfile. It might look like overkill in this context, but if you have a much larger program, you will be extremely happy to be able to do this.

Its a fancy word for something that is extremely obvious once you understand where the people who created these terms were coming from.


I think encapsulation is closely related to information hiding and abstraction. It is simply the practice of hiding implementation details and object internals from the outside world. It helps both with clarity as well as reducing coupling.

The capabilities of a class are declared in the interface of methods it defines, not in the detail of how they are implemented. Good encapsulation ensures the public interface is sufficient for callers to use without revealing internal implementation details. A well encapsulated design reduces coupling, as the internals can be replaced without affecting everything else that uses that class (through its interface).

0

精彩评论

暂无评论...
验证码 换一张
取 消