Methods in Object-Oriented Design

后端 未结 4 1566
攒了一身酷
攒了一身酷 2021-02-06 03:32

Q1. In my university studies of object-oriented modelling and design they recommend thinking about what an object can do for its method, and what its responsibilities are for it

4条回答
  •  臣服心动
    2021-02-06 04:06

    Unfortunately, the roadblock you've hit is all too typical in academia. Academic projects tend to start with video rental stores, libraries or student registration systems (yours is a variance of this with a doctor's office) and then inheritance is taught with animals. The guideline you've provided is also very typical

    they recommend thinking about what an object can do for its method, and what its responsibilities are for its attributes

    In fact when beginners ask I usually explain an object's property's are the things it knows about itself and its methods are the things it knows how to do. Which is really just another way of saying exactly what you have there. As you've discovered this way of thinking quickly breaks down when you start discussing more tangible systems and not just examples.

    For instance the guideline works pretty well with this object:

    public class Tree
    {
        public int Height { get; set; }
        public void Grow(int byHowMuch)
        {
            Height += byHowMuch;
        }
    }
    

    While this certainly fits the bill your right to think that it doesn't "feel" right:

    public class Secretary
    {
        public void MakeAppoinment(Patient patient)
        {
            //make the appointment
        }
    }
    

    So what's the solution? It's a matter of taking what you are being taught and applying it. Learning and understanding design patterns will help a lot with developing systems which are more functional than a tree that knows how to grow.

    Recommended reading:

    • Design Patterns: Elements of Reusable Object-Oriented Software (also known as the Gang of Four or GoF)
    • Head First Design Patterns
    • Head First Object-Oriented Analysis and Design

    To solve the issue you're been presented I would probably use a combination of inherited person classes and interfaces, which would perform their actions through a series of service classes. Essentially a secretary, doctor, and patient would all inherit from person and each of these classes could be passed to accompanying service classes. The service classes may or may not do things like SeePatient(). Please don't take this example to mean that person classes wouldn't have methods.

    Stack Overflow has more than a few related questions which may be of use:

    • Is Single Responsibility Principle a rule of OOP?
    • Are there any rules for OOP?
    • why is OOP hard for me?

    Additionally, it would be good to check out:

    • Single responsibility principle
    • Don't repeat yourself
    • PrinciplesOfOod

    Finally, there isn't a single definition of what makes an application object oriented. How you apply patterns, principles etc. will define your program. The fact that you are asking yourself these questions shows that you are on the right track.

提交回复
热议问题