Wednesday, February 22, 2012

Choosing good abstractions

Coupling and cohesion

The efficiency of the programs you design depends on how well you choose the classes they use.

Well-abstracted classes lead to programs that are easy to maintain and extend. However, if you choose bad abstractions, you could end up with an error-prone application that doesn't model the problem domain effectively.
Simplicity is the key to choosing good classes.

The classes you choose should capture only one key abstraction. If a class performs two functions, then you should split it into two separate classes, each with its own function.
There are five key concepts you should consider when choosing classes:
  • coupling
  • cohesion
  • sufficiency
  • completeness
  • primitiveness
These allow you to measure how well-designed classes are.
Coupling refers to how tightly connected classes are. If a class is very dependent on other classes to carry out its functions, then it is strongly coupled.
Strongly coupled classes can lead to problems in program design. Because such classes are interdependent, they are difficult to extend, and bugs in one can affect others.

This interdependency can also make it difficult for other programmers to understand programs that contain such classes.
To perform adequately, classes must be connected to other classes. But you should choose a level of connection that doesn't make one class too dependent on another.

This is particularly important when you design for inheritance.
Though inheritance is a form of coupling, it can make programs easier to design and extend.

So when choosing classes, you need to balance the requirements of inheritance against the need for weak coupling.
Cohesion is a measure of how closely related the elements in a class are. These elements are the states, behaviors, and functionalities of the class.
A class whose members are simply grouped together and have little in common is only coincidentally cohesive. Such a class might have two unlinked functions, or it may group object states with unrelated object behavior.

Because its elements do not cohere to realize a single purpose, such a class is confusing, difficult to use, and often too complex to implement.
Let's say you used a class named BankEmployee to model how bank staff are paid and how they interact with customers.

Since the elements of these separate functionalities are unrelated, this class would be a bad abstraction.
Classes with good cohesion are well defined and contain elements that properly belong together.

Such classes are easy to understand and use because they serve a definite purpose – they have functional cohesion.

Sufficiency, completeness, and primitiveness
Sufficiency, completeness, and primitiveness are related principles.

A good class needs to balance the requirements of each of these principles before it can function well.
  • Sufficiency
  • Completeness
  • Primitiveness
Sufficiency
Sufficiency is the characteristic that ensures classes are able to function effectively.

You must create classes that contain all the attributes and operations necessary to perform the tasks you require of them. So if you create a Car class, you must include an accelerate operation – otherwise no car object will be able to change speed.

Sufficiency means that you should include only those attributes and operations that are needed for the system you are modeling.

If you want to create a class that models a typical car, you need to specify things like its speed and model.

But you don't need to specify what type of suspension the car uses – this would complicate the class without adding any important functionality to it.

Whether a class is sufficient or not depends on its purpose and on the level of detail it is intended to capture.

If you need to represent something in great detail, then you might have to include specifications that would not be needed in a more abstract model.
Completeness
Completeness determines whether the interface of the class captures all the behavior of the class.

The attributes and operations you give to a class must be general enough to allow other classes to communicate with it.
Primitiveness
Primitiveness means that you shouldn't make a class unnecessarily complicated. It is a principle that lets you control a large spectrum of behavior with just a few operations.

For any class, you should choose operations and attributes that are very basic.

To create more complex class states, you can simply change the values of the attributes you have chosen.

Or you can combine simple operations to create complex ones.

Let's say that in the Car class, you want to model the way gear changes work.

A vertical rectangle with three compartments depicts the Car class with the class name, Car, in the first compartment. The second compartment contains the attributes which are: engineSize, color, model, maxSpeed, currentSpeed. The third compartment contains the operations which are: accelerate(), brake(), turn(), getSpeed(), shiftUp(), shiftDown(), shiftUpTwoGears().

You will need to create a shiftUp operation, but you won't need a shiftUpTwoGears operation. You can simply use the more primitive shiftUp operation twice.

Primitiveness is very closely related to completeness.

To make a class complete, you need to account for all its behavior. But by defining this with primitive functions, you ensure that you don't have to list every possible permutation of a class's behavior.

The level of primitiveness you choose depends on how you want your program to perform.

If you need a program to run very fast, it may be better to use a complex function once rather than using a primitive one several times.

Summary

Classes should capture only one key abstraction. Classes that depend on other classes to function are strongly coupled. You should choose classes with weak coupling, because this makes programs easier to extend, maintain, and debug. Classes should also be functionally cohesive.

To be sufficient, a class must have enough attributes and operations to capture all the relevant states and behavior of its objects. You should make the set of these properties sufficiently complete to allow the purpose of the class to be easily understood, but you should also try to keep the class as simple as possible. You can do this by observing the principle of primitiveness.

102 comments:

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

ccna training in chennai Tnagar

Learning new technolgy would help oneself at hard part of their career. And staying updated is the only way to survive in current position. Your content tells the same. Thanks for sharing this information in here. Keep blogging like this. Android Training in Chennai


Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
Mobile App development Company
Ios App development Company

Abstractions would mean a lot of use to the new era java programmers because they help in simplifying coding. In fact, despite the high skills needed for the applications, their usefulness still stands out in this era. When you ask the java experts, you will be told that the Java is creating an excellent development environment. Hire trained Law School Admission Essay Editors online.

Great article. After reading this article i learnt something new information about hadoop from this article..

big data training in chennai | best hadoop training in chennai

cI read the post and I have really enjoyed your blogs posts.looking for the next post.
Digital Marketing Training In Bangalore.

I must thank you for the efforts you have put in spending this site. I am hoping to out the same high-grade content by you later on as well. In truth, your creative writing abilities has inspired me to get my own, personal blog now..
Java Training in Chennai
Dot Net Training in Chennai
Cloud Computing Training in Chennai
Digital Marketing Training in Chennai
SAS Training in Chennai
SEO Training in Chennai
AWS Training in Chennai
Microsoft Azure Training in Chennai

This is very nice post thanks for updating this information and Thanks for your sharing with us.
Web design company in chennai

Thank you for sharing such a nice and interesting blog with us.
SAP SD Training in Chennai


This comment has been removed by the author.

very informative blog and very useful for readers. making us to learn more from your blog.keep on updating your blog
...

VMware Training in Chennai


Omrdeals.in is an online platform that
allows people to find offers and the greatest
deals in and around OMR. It is the directory
of the business in and around omr. It helps
internet users to surf the required deals and
offers.

Deals in OMR

It is really very excellent,I find all articles was amazing.Awesome way to get exert tips from everyone,not only i like that post all peoples like that post.Because of all given information was wonderful and it's very helpful for me.
SAP Training in Chennai
SAP ABAP Training in Chennai
SAP FICO Training in Chennai
SAP MM Training in Chennai


Nice information.. Thanks for sharing. Addmen group is one of the best omr software provide in India and abroad also. If you want any kind of omr software India you just have to visit our website and see the different omr software.
omr software

This article gave good information.Thank you for posting this article.
iOS Training In Chennai | iOS Training Institute In Chennai

Nice information.. Thanks for sharing. Addmen offers Omr answer sheet formats for practice of any compitative exam before final paper this will useful for students.
best omr software

Excellent, Good work and its really a useful information dude

Thanks for an insightful post.These tips are really helpful.Keep it up.Keep blogging.!!
Villas in Goa
python training in Vijaynagar

Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....

Solar Rooftop
Solar Water Heater
Solar Panel
Solar Module
Energy Efficient
BLDC Fan
Solar Power
Power Plant
Solar Training
Solar Pump

You have provided a nice article, Thank you very much for this one. And I hope this will be useful for many people. And I am waiting for your next post keep on updating these kinds of knowledgeable things
RPA Training in Chennai
Selenium Training in Chennai
Robotic Process Automation Certification
RPA Training
Software testing selenium training
Selenium testing training

Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.


Amazon Web Services Training in Velachery, Chennai |AWS Training in Velachery , Besant Technologies

Amazon Web Services Training in Chennai | AWS Training in Chennai

Amazon Web Services Training in Chennai |Best AWS Training in Chennai

Amazon Web Services Training in Chennai | AWS Training in OMR,Chennai

Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Website. Please feel free to visit our site. Thank you for sharing.Well written article Thank You Sharing with Us project management courses in chennai | pmp training class in chennai | pmp training fee | project management training certification | project management training in chennai | project management certification online |


Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
Advanced AWS Interview Questions And Answers, Top 250+AWS Interviews Questions and Answers 2018
Advanced AWS Interview questions and answers | Top 110 AWS Interview Question and Answers
Advanced AWS Training in Bangalore | Best Amazon Web Services Training in Bangalore
Advanced AWS Training in Pune | Best Amazon Web Services Training in Pune
Advanced AWS Online Training | Best Online AWS Certification Course in india


Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
Advanced AWS Interview Questions And Answers, Top 250+AWS Interviews Questions and Answers 2018
Advanced AWS Interview questions and answers | Top 110 AWS Interview Question and Answers
Advanced AWS Training in Bangalore | Best Amazon Web Services Training in Bangalore
Advanced AWS Training in Pune | Best Amazon Web Services Training in Pune
Advanced AWS Online Training | Best Online AWS Certification Course in india

Just stumbled across your blog and was instantly amazed with all the useful information that is on it. Great post, just what i was looking for and i am looking forward to reading your other posts soon!
python training institute in marathahalli
python training institute in btm
Python training course in Chennai

your blog was good and informative ...so keep sharing information with us
air hostess course in chennai

airport courses in chennai

I am sure this post has helped me save many hours of browsing other related posts just to find what I was looking for. Many thanks!
python course in pune
python course in chennai
python Training in Bangalore

Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in

Data Science course in Chennai
Data science course in bangalore
Data science course in pune
Data science online course
Data Science Interview questions and answers
Data Science Tutorial

It's Really A Great Post. Looking For Some More Stuff.



shriram break free

Post was good and really helpful for more stuff click on the link below.


shriram earth plots

This comment has been removed by the author.

Thanks for your informative post.its helpful to understand it.keep blogging.


Apple iPhone Service Center in Chennai Anna Nagar

Very good blog keep sharing like this useful information thanks for sharing.
Apple iPhone Service Center in Chennai Anna Nagar


It is a very informative and useful post thanks it is good material to read this post increases my knowledge.
Apple iPhone Service Center in Chennai Anna Nagar


Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai

Thanks for sharing useful information.

Thanks
Cpa offers

Very nice article. Thanks for sharing such valuable information of java certification.

Realme Mobile Service center in Chennai
Realme Servicing by No. 1 Leaders Awesome Services On-time, Every time!
Realme Mobile Service Center in Chennai

iPhone Service center in Bangalore No. 1 Leaders Awesome Services On-time, Every time!
iPhone Service Center in Bangalore
Apple Service center in Bangalore
Apple Service Center in Bangalore

Nice and useful information.Thanks for the effort and the results,impressed with your post.
big data training in btm layout

This is also a very good post which I really enjoyed reading. It is not every day that I have the possibility to see something like this,

eTechno Soft Solutions is a leading training institute for all kind of the Oracle Training in Bangalore with real-time experienced trainers with 100% Placement Assistance.

This comment has been removed by the author.

OnePlus Service Center in Chennai

OnePlus Service Center in Chennai No. 1 Smart Service On Time, Every Time
OnePlus Service Center in Chennai
OnePlus Service Center in Chennai
OnePlus Service Center in Chennai

Moto service center in Chennai
Leading Motorola Service Center In Chennai. Fast & Reliable Services On-time, Every time!
Moto service center in Chennai
Motorola Service Center In Chennai
Motorola Service Center In Chennai

Iphone service center in Chennai

Iphone service Center in Chennai offers you best quality of Services On-time, Every time!
IPhone Service Center in Chennai
Apple Iphone Service center in Chennai
Apple Service Center in Chennai
Apple Service center in Chennai
IPhone Service Center in Chennai

Sony Mobile Service Center In Chennai
Sony Servicing by No. 1 Leaders Awesome Services On-time, Every time!
Sony Mobile Service Center In Chennai

Honor service center in Chennai

Honor Service Center in Chennai for the Best & Right Place to service your Honor mobile phone.
Honor Service Center in Chennai

Ipad service center in Chennai

Ipad service Center in Chennai offers you best quality of Services On-time, Every time!
Ipad Service Center in Chennai

Apple Imacbook Service Center in Chennai

Apple Laptop Service Center in Chennai offers you best quality of Services On-time, Every time!
Apple Laptop Service Center in Chennai

Acer Service Center in Chennai

Acer Service Center in Chennai Best service for all model acer laptop and desktop Services On-time, Every time!
Acer Laptop Service Center in Chennai
Acer Service Center in Chennai
Acer Service Center in Chennai

Awesome Post!!!

Redmi Service Center in Chennai

Redmi Service Center in Chennai No. 1 Leaders Awesome Services On-time, Every time!
Redmi Service center in chennai
Mi Service Center in Chennai
Mi Service Center in Chennai

WOW Great Post!!!

Apple iPhone Service Center in Chennai

Chennai No-1 Apple iPhone Service Center in Chennai
Apple iPhone Service Center in Chennai
Iphone Service Center in Chennai
Iphone Service Center in Chennai
Apple Service Center In Chennai
Apple Service Center In Chennai

Oneplus Service Center in Bangalore

Oneplus Repair Service Center in Bangalore. For the finest service and fastest delivery for your Oneplus mobile, visit us! Go home a happy customer! Connect with us!!

Oneplus Mobile Service Center in Bangalore

Oppo Service Center in Bangalore

OPPO Service Center in Bangalore is the best OPPO Service Center in Bangalore. We can fix all OPPO devices at low cost with best quality.
Oppo Repair Center in Bangalore- All our OPPO care technicians has 10+ years experience in the mobile care fix industry

Oppo repair center in bangalore

Thank you for sharing this wonderful information. The blog is really helpful...keep sharing.

Python Training In Bangalore

Python Certification Course in Bangalore

I was basically inspecting through the web filtering for certain data and ran over your blog. I am flabbergasted by the data that you have on this blog. It shows how well you welcome this subject. Bookmarked this page, will return for extra.

PMP

I see the best substance on your blog and I unbelievably love getting them.
https://360digitmg.com/course/certification-program-on-big-data-with-hadoop-spark

Set aside my effort to peruse all the remarks, however I truly delighted in the article. It's consistently pleasant when you can not exclusively be educated, yet in addition, engaged!
difference between analysis and analytics

I will truly value the essayist's decision for picking this magnificent article fitting to my matter.Here is profound depiction about the article matter which helped me more.
data science course delhi

Really impressed! Everything is very open and very clear clarification of issues. It contains truly facts. Your website is very valuable. Thanks for sharing.360digitmg

Information was good,i like your post.Looking forward for more on this topic.
Sharepoint Admin training in bangalore

Wow, amazing post! Really engaging, thank you.
ai Course

تصميم المواقع أفضل الممارسات التي يقوم بها أصحاب الأعمال اليوم يمكنك الأن
تصميم مواقع
تصميم متجر الكتروني

Post a Comment