问题
Right now I am studying design Pattern, and I was wandering about the following... why not to code all my methods in 1 class, each method perform 1 task, so my client can access all the methods from within 1 file and I dont need to create new classes and move methods to.
so my questions is, is this good or bad, or shall I delegate the methods into different class each hold corresponding methods?
for example: I want to control a "control room" this is the methods I need:
1- open camera stream
2- close camera stream
3- record camera stream
4- open voice stream
5- close voice stream
6- record voice
7- print files
is it better to put all methods in 1 class (mainController) or shall I make many classes and put methods in it like (cameraController, voiceController...etc) and why this is better?
回答1:
What is better? Depends on the scope of your application, but, in general terms, as mentioned by @blazy, you should always focus on creating a robust, maintainable and well-structured application. For sure, you can implement the whole code in one single class, but it is always better to get used to the best practices and respect the patterns according to your needs.
In this case, creating a class to handle Voice functionality and Camera functionality will be the best, and make them communicate in an efficient through a SecurityCoordinationController (for sure you can use a better name), because someday, you might need to implement another "Control Room", something like "Management Control Room" or maybe "Specific Control Room" that should have similar functionality and could reuse both of your classes that were created to handle Voice and Camera. You can also consider using an interface to create something like "Room" that implements basic logic to "create a room" in general terms, then you could create your own specific "Control Room" or "Living Room" or something like that, then implement the "Room" interface and achieve functionality according to your needs. Maybe I am going too deep, but just an idea.
In general terms, you should guide yourself for one of the principles based on the "divide and conquer", is better for you to achieve re-usability and is better for self-organization. Who knows, maybe you should need to reuse some component in the future for another course or a future project and if you have everything in one single class, you will make double-effort.
At the end, it is just up to you, depending on your needs and your comfort programming; however, it is always better to get used to the best practices which involves a well organized, functional and efficient code, which is also reusable.
Hope this can help you someway, best regards :)
回答2:
You should dissociate them if you are looking to give a robust solution. A separate class should be used to handle your voice stream
, another to handle your camera stream
and a MainController to coordinate their activities.
If at any point you see yourself copying and pasting methods or parts of methods in your different classes, then it's a sign you need to create other generic classes.
回答3:
You have to use judgement and only you know the design of your system and how it will evolve.
Putting all 7 methods in a single controller might be tempting (and might even be the right thing to do in your application), however you need to be aware that it could lead you down a path that might cause problems in the future. You don't want to create a God Object*.
Your methods should be grouped in a logical way so that they make sense to the consumer of your service and so you yourself can easily maintain them as your system evolves.
*As an aside, you might want to look at some anti-patterns in OO design, they're often just as useful as design patterns!
回答4:
What you're doing would be considered bad practice in Java (and other OOP languages). You should 'delegate the methods into different class each hold corresponding methods' to keep the nature of OOP.
As to why this is better, for a more complete answer I'd suggest you read more into OOP. But for a quick and dirty version here you go:
- Maintenance - By keeping things separated into general classes and methods, if you find a bug or have to change something due to spec changes in the future, you only have to make a change in one place, versus as many times as it's used in the program.
- Readability - By having objects and simple methods, you can make your code far more readable. This will help in debugging and will help if you ever have to come back to the code after an extended period of time.
- Expansion - By using polymorphism, you can make your code easily expandable in the future by simply implementing interfaces you use or extending abstract classes you've created.
For the purposes of your project, you may want to look into creating a scheme similar to this:
class Camera
- hold information you need to interact with cameraclass Microphone
- holds information you need to interact with voice capture devicesinterface MediaStream
- describes interactions with media streams (probably would have a way to get data from the streams)class CameraStream implements MediaStream
- interacts with a camera (this is whereopenCameraStream
,closeCameraStream
,recordCameraStream
should probably go)class VoiceStream implements MediaStream
- interacts with a camera (this is whereopenVoiceStream
,closeVoiceStream
,recordVoiceStream
should probably go)interface MediaOutput
- describes interactions with output for your mediaclass MediaPrinter implements MediaOutput
- interacts with printing mechanism (whereprintFiles
should probably go)class ControlRoom
- should take inputs ofMediaStream
's andMediaOutput
's- Some main class to instantiate all these objects
来源:https://stackoverflow.com/questions/23113374/why-not-to-code-in-one-class