English 中文(简体)
使用方法和物体时[封闭]
原标题:When to use methods and objects [closed]
Closed. This question is opinion-based. It is not currently accepting answers.

This might be a duplicate, but as I haven t seen anything hopefully it isn t. Basically, I ve never seen a good rule-of-thumb for when to split a chunk of code into separate methods and objects. Does anyone have any good rules they found work well?

最佳回答

I ve found this article (butunclebob.com, S.O.L.I.D. principles for OOP) to be enlightening for designing boundaries and hierarchies of classes. Although it is mostly common sense, putting a name to the goals you want to achieve helps until you gain a feel for "good"ness of a design through experience.

问题回答

如果某一方法在一页上安装,则该方法过于庞大。

That s not an excuse to use a really small font or use a really big monitor

Go and read;

虽然他们谈论如何处理你已经掌握的东西,但如果你们阅读和学习,你可以不制定从来的滞后法!

Disclaimer: I have nothing against OOP, quite the opposite. The following is just a personal opinion

In general I agree with the other answers and I would also try to break up code into methods whenever I "feel" it necessary.

然而,如果你想说,该组织是一种方法,是一种工具。 在采用OOP技术时,仍然有一些其他办法更适合的情况。

简单

echo "Hello World!"

might be faster/better/easier than

class HelloWorld {

    private String text = null;

    public HelloWorld() {
        this.text = "Hello World";
    }

    public void print() {
        echo this.text;
    }

}

(Compare the exaggerated example hello world in patterns)

umb行的规则也一样:

问题:我是否应该把这分为方法和物体?

A: 是。

从业务处的角度来看,“all代码”应作为物体和方法书写。 OOP的货币为objects。 现在,如果你有一大阶层,并想知道如何打破这一阶层,因为它违反了单一的责任,那么umb的统治就是要概括不同的。

班级的设计应具有一个首要目的。 页: 1 简明扼要。

在决定方法时,你应铭记两点。 重点为这一类别建立明确的接口,并制定可测试的方法。





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签