Click here to Skip to main content
15,891,513 members
Home / Discussions / Java
   

Java

 
GeneralRe: How do I decide for class responsibilities? Pin
Nagy Vilmos9-Jul-12 8:23
professionalNagy Vilmos9-Jul-12 8:23 
GeneralRe: How do I decide for class responsibilities? Pin
Pete O'Hanlon9-Jul-12 8:31
mvePete O'Hanlon9-Jul-12 8:31 
GeneralRe: How do I decide for class responsibilities? Pin
Nagy Vilmos9-Jul-12 8:33
professionalNagy Vilmos9-Jul-12 8:33 
GeneralRe: How do I decide for class responsibilities? Pin
Pete O'Hanlon9-Jul-12 8:41
mvePete O'Hanlon9-Jul-12 8:41 
GeneralRe: How do I decide for class responsibilities? Pin
Nagy Vilmos9-Jul-12 8:45
professionalNagy Vilmos9-Jul-12 8:45 
GeneralRe: How do I decide for class responsibilities? Pin
Pete O'Hanlon9-Jul-12 8:53
mvePete O'Hanlon9-Jul-12 8:53 
GeneralRe: How do I decide for class responsibilities? Pin
Nagy Vilmos9-Jul-12 9:00
professionalNagy Vilmos9-Jul-12 9:00 
AnswerRe: How do I decide for class responsibilities? Pin
Stingrae7899-Jul-12 10:00
Stingrae7899-Jul-12 10:00 
Now to actually give you an alternate answer...

While this is overly complicated for the question:

Body and Title could be considered types of text components and your Post could be a 'Base Object' type that consists of a component list. In this 'silly' system, your 'Post' can easily be given any number of components because Post supports having components that have their own functions.

Why not have the components as members? Well because with this idea of base objects having a list you only have to change whether you are giving the component to the object rather than modifying the member in the class. e.g. I want a new 'StupidEnemy' that has a different 'AIComponent' to the 'Enemy'. I don't have to have a new class for my new type of enemy I simply give them a different AIComponent at instantiation.

I don't think this is a great approach in most cases (As you 'could' end up using a data structure to hold a single component, this seems illogical), I’ve used it a little before and it has its pros and cons but it does mean your code fits SOLID. It works quite well for game development though as it keeps the project tidy (Relative to just using straight composition).

Java
//This is all just to represent the use of a components
BaseObject post = new Post(); //for some reason our post needs to be a subclass, again this is just a silly example.
TextComponent Header = new Header("Header"); //Best to use a uID here instead so you can potentially have multiple headers or a multimap over a vector
post.Add(Header); //Add would initialise the component to have a pointer to the Base Object
Post->getComponent("Header").SetMessage("Hello World") //get our component
//in event cycle
Post->Execute();//what ever you want to call on all components Update(), Render() etc.


/rant I didn't do English at University, it has no real place in a BSc also it's more of a sociology/business idea and while it does occur, I want to aim to have a better work ethic constantly for myself!/endrant

modified 9-Jul-12 16:09pm.

QuestionInstrument Automation Pin
CMA215-Jul-12 10:45
CMA215-Jul-12 10:45 
AnswerRe: Instrument Automation Pin
TorstenH.5-Jul-12 19:22
TorstenH.5-Jul-12 19:22 
AnswerRe: Instrument Automation Pin
jschell6-Jul-12 11:20
jschell6-Jul-12 11:20 
Questionjava with text file Pin
RORY175-Jul-12 9:35
RORY175-Jul-12 9:35 
AnswerRe: java with text file Pin
TorstenH.5-Jul-12 19:13
TorstenH.5-Jul-12 19:13 
GeneralRe: java with text file Pin
RORY175-Jul-12 23:15
RORY175-Jul-12 23:15 
GeneralRe: java with text file Pin
RORY175-Jul-12 23:37
RORY175-Jul-12 23:37 
GeneralRe: java with text file Pin
TorstenH.5-Jul-12 23:40
TorstenH.5-Jul-12 23:40 
GeneralRe: java with text file Pin
RORY176-Jul-12 0:51
RORY176-Jul-12 0:51 
GeneralRe: java with text file Pin
Richard MacCutchan6-Jul-12 1:07
mveRichard MacCutchan6-Jul-12 1:07 
Questiondisable minimize button in JFrame Pin
Member 78380275-Jul-12 5:19
Member 78380275-Jul-12 5:19 
AnswerRe: disable minimize button in JFrame Pin
TorstenH.5-Jul-12 19:10
TorstenH.5-Jul-12 19:10 
Questionjava database Pin
Member 82109845-Jul-12 5:17
Member 82109845-Jul-12 5:17 
AnswerRe: java database Pin
jschell5-Jul-12 6:20
jschell5-Jul-12 6:20 
QuestionProvider Pin
JimAce5-Jul-12 0:53
JimAce5-Jul-12 0:53 
AnswerRe: Provider Pin
TorstenH.5-Jul-12 1:19
TorstenH.5-Jul-12 1:19 
GeneralRe: Provider Pin
JimAce5-Jul-12 4:12
JimAce5-Jul-12 4:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.