What are the advantages of strictly maintaining separate data | algorithm | interface layers?

I always separate the user interface from other functionality in my programs – its the way I have been taught, and it has obvious advantages since you can change the interface but keep the same functionality.

However, I’ve come across some programmers who strictly separate data, algorithms and interface. So the data objects which hold the data in volatile memory have only fields/properties and their getters and setters. Any algorithms are kept in a separate class.

In my view the main disadvantage of this is that the algorithm classes usually need full access to the data in the data classes. You might want to have a more functional class which you create with lots of data but then objects from other classes can only get at that data in specialised ways.

1

It’s called a Plain Old Data Structure (PODS). They are fairly common in places where data first enters a system from an external source, like a database. In effect their single responsibility is to abstract that external data into a form that’s easier for higher layers to consume.

In other words, it allows programmers of higher layers to work with familiar native objects rather than having to sprinkle accesses to the external source throughout the code. That data is read into the system in one place, and only that one place must change if the interface to the external source changes.

Like any principle, it can be taken too far. If they’re commonly using a PODS on internal-only data, it’s probably misguided in OOP. Note however that passing around a PODS is a fairly common pattern in functional programming, for better or worse.

3

When you encapsulate data, you’re limiting yourself to only being able to perform a limited number of operations on it. But often there’s an unbounded number of things you want to do with data.

Suppose we’re writing a simple role-playing game. Let’s write a Weapon class and examine what happens if we hide all its data.

public class Weapon {
    private int damage;
    private int accuracy;

    public Weapon(int damage, int accuracy) {
        this.damage = damage;
        this.accuracy = accuracy;
    }
}

Naturally, we want to be able to hit people with the weapon. So we need a function that calculates the damage we’ll do to the enemy. But all the weapon’s variables are private, so we’ll have to turn that function into a method.

public int hitEnemy(Enemy theEnemy) {
    return theEnemy.getDefense() - this.damage;
}

We’ll also need a way to determine if we succeeded in our attack. Again, this’ll become a method.

public boolean tryToHit(Enemy theEnemy) {
    return random() > (theEnemy.getEvasion() - this.accuracy);
}

Later on we find that we want to compare two weapons to see which one has more damage. Another method.

public Order compareDamage(Weapon other) {
    if (this.damage > other.damage) {
        return Order.GREATER;
    } else if (this.damage < other.damage) {
        return Order.LESS;
    } else {
        return Order.EQUAL;
    }
}

A few days later we decide that a weapon’s price should be determined based on its damage and accuracy…

public int getPrice() {
    return 10 * this.damage + 0.5 * this.accuracy;
}

The following week we want to give weapons ratings from C to A based on their stats…

public Rating getRating() {
    if (accuracy/10 + damage > 10) {
        return Rating.A;
    } else if (accuracy/10 + damage > 5) {
        return Rating.B;
    } else {
        return Rating.C;
 }

As you can see, every time we need to do anything new with the weapon, we need to throw that into the weapon’s class to get at its variables. That’s bad – it violates the Open/Closed Principle, which states that code should be open to extension and close to modification. If we had turned Weapon into a plain old data object – a simple record with nothing but public data – we’d be able to add functions to use weapons in new ways without having to modify the Weapon class.

When you design an abstraction X, you must distinguish between things X does and things I can do with X. Our weapons don’t do anything, so there’s no implementation details to hide. Hiding all their data behind private variables is about as useful as a list that doesn’t let you access its elements. Our game will definitely need some encapsulation and abstraction, but it should be implemented at a higher layer, in the game logic proper.

13

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật