Reputation:
I'm trying to learn how to make my application has the same "face" in all files, the same pattern, but I do not know if this is the right thing to do.
I want know if the more correct in software development, is to limit the application to only one type of pattern.
For example, that's how my application looks like (just a piece)
Api.js:
'use strict'
var Server = require('./server');
var Api = {
init: function() {
Server.init('start');
}
};
Server.js:
'use strict'
var Server = {
init: function(command) {
this.command = command;
if (command === 'start') {
this.start();
}
}
};
I'm using the "initializer object pattern" in all my application. So i'm avoiding decorator pattern, facade, everything.
Should i avoid or i can use more than one? If the right answer is use several, depending on the needs, i have other question. This doesn't make the maintenance more difficult? Is not make the code like a spaghetti?
Thanks.
Update.
I'm going to try explain again, this time with a bounty.
i already try to ask but seems that no one really can give me a concise answer.
I would like to know what are the secrets from making a good app, a good web app, talking about code appearance. I want know if I should maintain the same standard code throughout my hole application, if this is GOOD, or really doesn't matter.
For example, i have two EXAMPLES files
app.js:
var server = require('./server');
var app = {
init: function() {
server.init('DEVELOPMENT');
}
};
module.exports = app;
server.js:
var server = {
init: function(env) {
if (env === 'DEVELOPMENT') {
// CODE
} else {
// CODE
}
}
}
module.exports = server;
In this case, i'm using one object with method init, which is the pattern that i'm using in my hole app..
Or it doesn't matter, i should write anything:
first object:
var server = require('./server');
var app = {
init: function() {
server('DEVELOPMENT');
}
};
module.exports = app;
than server as a function:
var server =function(env) {
if (env === 'DEVELOPMENT') {
// CODE
} else {
// CODE
}
}
module.exports = server;
I can give 100 of my points. it's incredible how i can't find a good answer for this particular issue.
Thanks.
Upvotes: 0
Views: 164
Reputation: 18005
Should I using other patterns?
No, you should not insist on a single pattern.
No design pattern books will ever advise you to use a single pattern. Just like you cannot chop all ingredients in one single way (are you going to dice the spaghetti?), you cannot organise all logic in one single pattern.
Sure, you can make all your Objects use the initialiser pattern, and don't use constructors at all. This is ok. Been there, done that. I like it.
But these objects can be used with Builder or Abstract Factory (if it make things simpler). As long as the builders/factories themselves have initialiser, and that they properly initialise the created objects, then your use of the initialiser pattern will be consistent. Outside of creational patterns, it is usually good to organise objects with structural and behavioural patterns. They do not conflict with initialiser at all.
For example, look at DOM. All nodes are created by the Document object - elements, text nodes, comments, even events. This is the Factory pattern.
Yet the Document object is also a Facade! From it you access the whole system's status, objects, data, you can even write to it! Every DOM operation starts from the Document, it is the Facade of the DOM system.
DOM nodes also implements multiple patterns. They are organised in Composite, let you listen to events with Observer and Command, and handle events in a Chain of Responsibility. They are certainly parsed by an Interpreter, DocumentFragment is a Proxy, svg elements are implemented as Decorators, and createNodeIterator obviously gives you an Iterator.
The point is, good object-oriented design will yield multiple design patterns as a result, intentional or not.
What are the secrets for good code appearance
I think the best looking code is the one that is easiest to understand to you, and the way you read code changes as you gain more experience.
For example my style is too condensed for most programmers, but to me it strikes a good balance. So do develop your own style - you are not me, and you are not yesterday's you either.
Remember this as we go through the styles.
At the lowest level we have coding style - most importantly indent and bracket.
This one is simple, pick the one you like and stick with it. There are language specific styles, and they are often good starting points. Configure your IDE's formatter so that you can format all your code with hotkey.
Above the code syntax we have comment style and naming convention.
Setting rules on comment is fine, sometimes it is necessary for documenting tools. Avoid too much comment in practice. You may also want to decide your namespace and your stand on naming function expressions.
Above these structures, we have logic conventions.
The same code logic can often be done in many ways, some more 'beautiful' than the others in your eyes. Look at this example.
I picked the second style on first sight: no duplicate, logic is sectioned cleanly, format is not my style but reasonable. But many programmers would prefer the first style: logic is plain as day, a few duplications is worth it. While abstract, this level is quite deep - present your logic the wrong way actually increase the chance an experienced programmer read it wrong.
Finally, we arrives at the level of design pattern, about as far as code beauty goes.
The key to keep your code structure beautiful, is using the right patterns at right level to consistently accomplish loose coupling and code reuse, while avoiding pitfalls and over-design.
There are quite some books about beautiful code, and then there are even more books about designing and implementing beautiful software. (Decide for yourself which are beyond your level.) Knowledge is as important as experience, and you can gain them only by spending your time to study, to write, and to revise/refactor your apps.
Feel free to change your mind as you explore and experiment with your code. Changing your mind is a good sign of learning.
But first, familiarise yourself with design patterns. Just don't forget, they are the generic result of applying object-oriented principals to common tasks. It is still up to you to do the design.
Design Patterns Are Not Silver Bullets.
Upvotes: 4
Reputation: 972
I think the nature of this problem is one that will lend you to have a lot of open ended answers. Ultimately you'll need to choose the best aspects of existing paradigms and either work in full or in part with its methodologies. In my opinion, a lot of your API structure (and therefore design pattern) will come down to how you anticipate people accessing your endpoints for your application. Your proposed method of having a constructor object is a common and very flexible approach; it allows for prototyping for inevitable rewrites of the code and a easy to observe key and scope of the application. It you're ready to support EMCA6, switch this over to a Class
based constructor and you'll find yourself having a very manageable code base that can grow, is object orientated and has a very accurate level of function scoping.
Typically, I have developed applications to follow this style of initalisation (trying to make it as human readable as possible):
var Application = {
initialise: function (config) {
config = config || {} // defaults;
....
this.contactServer(config.serverConfiguration);
},
contactServer: function (configServer) {
....
},
renderServerPayload: function (payLoad) {}
};
Application.manageServerResponse = function (serverResponse) {
this.renderServerPayload(serverResponse);
};
Good luck with your application. Hopefully this was somewhat useful.
Upvotes: 0