pada tanggal
Car
car design
- Dapatkan link
- X
- Aplikasi Lainnya
In OOP (Object Oriented Programming), it’s important to remember why I’m building an application using objects, not just functions (procedural programming). Programmers sometimes treat objects like functions, completely defeating the purpose of objects in the first place. The purpose of this post is to explore the true benefits of OOP and how to properly structure the model.
What is an isolated object?
Contrary to the belief of novice OOP programmers, objects are more than a collection of data members and associated methods. It’s important to remember that an object embodies data and methods that are only relevant to itself. The term “decoupling” is used to identify the separation of software blocks that must not depend on each other.
Why is it important to separate objects?
Let’s say we have car Class using method driveForward (), Stop(), Order (), honkHorn (),and changeLanes (). This object is poorly designed because it is one of the methods. changeLanes(), May depend on Street class. What if I try to reuse this class for a car that only drives off-road? in this case, changeLanes () The method makes no sense in instantiating an object. further, Order () The method was to refer changeLanes () Using the method makes the entire object too specific to instantiate and manipulate off-road vehicles. Also, Street Class, it’s very likely car You also need to change the class.Since then car There is a method that depends on another object, and this object is said to be “joined” (this is what we are trying to avoid).
How to separate objects
To create what I call a “refined object”, I need to completely separate them so that all those fields and methods are unique to what the object can do in any situation. ..To separate car Class, you will want to move changeLanes () Method to another object that interacts with car, favorite CityDriving.. This new object car A class for special situations without compromising its pure definition.
When designing an object model, ask yourself, “Are these objects cleansed or separated?” If you stick to this question when you create a new object, you’ll not only write cleaner code, but you’ll spend less time refactoring. good luck!
Komentar
Posting Komentar