Guidelines for when to use Static class over insta

2019-04-29 06:42发布

问题:

Possible Duplicate:
When to Use Static Classes in C#

Can someone please provide guidelines , standard checkpoints on when to create static class and when to create instance class.

Basically many a times I see while writing a code that the same thing could have been done using static class and methods and i get confused with many things.

So far i know below check points :

  1. If object under the consideration can exists only once in entire application then it would come under "static"

  2. If a method does not use any field variable inside it then it can be static method.

回答1:

To my mind the best differentiator is state.

If your class has no state and is just a series of related methods that, when given an input, will give you an output then you have an excellent candidate for a static class.

If your class will be given input and then store it or in some other way will be expected to maintain its current state then you have a regular class.

Furthermore if your class is going to be declared abstract or is going to inherit behaviours from another class or implement behaviours from an interface, then you're moving away from a static class and have a normal class.

In short:

  • If it's a bag of methods or has no state
  • and it isn't abstract, inheriting or implementing an interface

Then you have a good chance it is a static class :)

I hope that helps.

NB (as per the astute comment below) state refers to the class storing some data that represents the current state-of-affairs for an object. Perhaps a class-level variable or a property.



回答2:

Static Classes are best used to create libraries of functions that will be called. Regular Classes are used to create objects that can be manipulated. It allows you to encapsulate the relations of a single bunch of code. Take, for instance, a point in Euclidian space. You could declare two floats and keep track of them individually, or you could use the following contract to declare how a point works.

class Point{
    float x;
    float y;

    Point(float x, float y);

    Point halfway(Point other);

    Line lineThrough(Point other);

    float distanceFrom(Point other);

    ...
}

This is a bit redundant, but it allows you to to write more readable code in the future - better keeping all your data encapsulated and well designed, making you a better programmer, because the equation of a line through two points goes from

float slope = (xone-xtwo)/(yone-ytwo);
float yintercept = yone-slope*xone;

to

Point p1 = new Point(xone, yone);
Point p2 = new Point(xtwo, ytwo);
Line linethrough = p1.lineThrough(p2);

Sorry the pseudocode is a bit Java informed. I have Java on the brain.



回答3:

if it should be a collection of functions, then you'd use static classes. like :

int result = calculation.add(int p1,int p2);
float result = calculation.divide(int p1,int p2);

you can't say this on static classes:

Calculation mycalculator = new Calculation();

because they can not be instantiated. They can only contain static members.

Taken from here.

but if it'd be a member of a code, like apple in a tree, you'd use instance classes. each apple would have its own characteristics but it doesn't change the fact that it is an apple. and it's a part of the tree which contains multiple instances of the apple.

Apple apple1 = new Apple();
apple1.color = green;
apple1.age = newborn;
apple1.fallfromtree(speed);

Apple apple2 = new Apple();
apple2.color = red;

etc.

You can look here.