Skip to main content

Posts

Showing posts with the label oop

OOP the point of interface

Possible Duplicate: Interface vs Abstract Class (general OO) EDIT: I just read the questions and answers to the questions from "possible duplicate" and I feel really sad that someone considers these two questions even similar... but, oh well...

Is it bad to declare an empty class?

In my spare time, I am building a Sudoku solver to try to get the hang of OOP in PHP. A Sudoku puzzle, for those of you who don't know, is in its most common form a 9x9 matrix of numbers from 1 to 9, with 3x3 squares delineated in a tic-tac-toe like pattern. Some numbers are filled in in advance. The goal of the puzzle is to fill in the remaining numbers, so that no row, column or 3x3 square contains the same number more than once.

Access parent"s parent from javascript object

Somthing like var life= { users : { guys : function(){ this.SOMTHING.mameAndDestroy(this.girls); }, girls : function(){ this.SOMTHING.kiss(this.boys); }, }, mameAndDestroy : function(group){ }, kiss : function(group){ } }; this.SOMTHING is what I imagine the format is, but it might not be. What will step back up to the parent of an object? Source: Tips4all

How can you organize the code for a game to fit the MVC pattern?

I'm a freshman in college going for my computer science degree... I've programmed plenty the last several years but just lately I've been getting more into theoretical ideas about organizing code, design patterns, differences in languages, etc. I have a Java class, so I've dropped my C++ research/development and moved into Java and JOGL (Java OpenGL). It's wonderful! But that's beside the point. I want to make a small role-playing game, but this question really applies to any sort of game. How do you organize the game objects in a way that is structured, like the Model-View-Controller pattern? It looks to be an amazing pattern, very widely used and makes a lot of sense, but I'm having trouble figuring out how to implement it. For instance, I need to keep track of a GL object for drawing to the screen. I have to have classes that implement MouseListener, MouseMotionListener, MouseWheelListener, and KeyListener (or one class, an all-in-one input manage

Is it bad practice to make a setter return "this”?

Is it a good or bad idea to make setters in java return "this"? public Employee setName(String name){ this.name = name; return this; } This pattern can be useful because then you can chain setters like this: list.add(new Employee().setName("Jack Sparrow").setId(1).setFoo("bacon!")); instead of this: Employee e = new Employee(); e.setName("Jack Sparrow"); ...and so on... list.add(e); ...but it sort of goes against standard convention. I suppose it might be worthwhile just because it can make that setter do something else useful. I've seen this pattern used some places (e.g. JMock, JPA), but it seems uncommon, and only generally used for very well defined APIs where this pattern is used everywhere. Update: What I've described is obviously valid, but what I am really looking for is some thoughts on whether this is generally acceptable, and if there are any pitfalls or related best practices. I know about the Builder pattern

Dynamic binding == late binding in Java or not?

In different sources i've read different things on the topic. For example Wikipedia says: Late binding is often confused with dynamic dispatch, but there are significant differences. But a couple lines later it is popular to use the term late binding in Java programming as a synonym for dynamic dispatch. Specifically, this refers to Java's single dispatch mechanism used with virtual methods. So where's the truth and what are this "significant differences"?

JS Object, how to access objects from within myself

So, i see no reason why this isn't working but i am at a wall and frustrated. Why can't i call this.myself from within the wyr.message.close function? Maybe my understanding of this is scewed but i was sure this is referring to the object itself, not the function. this.myself is undefined Code: wyr.message = { myself: $('.message'), init: function() { if(this.myself.is(':visible')){ setTimeout(this.close, 5000); } }, close: function(){ this.myself.fadeOut(1200,function(){ this.myself.remove(); }); } }

Object Oriented Programming: More than just basic objects

I seem to find all documentation regarding OOP too basic or too advanced. I'm trying to solve a specific problem and I can't get a nice solution without having to use Singletons or global instances or the such. I'm trying to create a DB based application: I have a DB with Boxes that are stored in some Location. So I created to tables: Boxes and Locations BOXES | id | Name | Location | ------------------------ | 1 | AA | 1 | | 2 | AB | 2 | LOCATIONS | id | Name | --------------------- | 1 | Garage | | 2 | Living Room | When trying to bring this to an Java application, I want to have a Box class that I can instantiate for every box and locations that I have in DB and put them in a list. My question is: how do I design Box and Location Classes? The immediate approach would be: class Box { int id; String name; Location location; } Class Location { int id; String name; } But then, how can I operate Locations properly?

Does using method chaining in PHP cause any problems with resources or memory?

I'm talking about methods like this: $object->method()->method1('param')->method2('param'); Those are created by returning the object in the function. return $this; I've seen third-party software use that method, but I'm wondering, wouldn't that cause a bit of a problem with the resources or memory because you're continuously returning the entire object?

Using OO PHP in CSS

tl;dr - I'd like to know if it is possible to pass an object into a PHP file with CSS headers, such that I can use this object to manipulate various CSS attributes. What I'm attempting to do, is allow my PHP/CSS file to interact with the other objects/php files in the webpage, e.g. menu item objects. My ultimate goal is to use PHP in the CSS file to count the number of menu items, and apply the appropriate width value in order to space them out evenly on the page. I use a very simple color based example below to demonstrate my understanding so far... I understand that for basic usage of PHP in a CSS file, one can do something like: <?php header("Content-type: text/css"); $dkgreen = '#008400'; body { background:<?=$white?>; } ?> I also understand that OO PHP can be used to achieve a similar thing, e.g.: class css { function __construct($args=array()) { foreach($args as $key => $field) { $this->{"$key&qu

Determining source of method call at runtime

I'm wondering if there is a "better" way to do this: class Foo { final public function bar() { if (is_subclass_of(get_called_class(), __CLASS__)) { throw new Exception(); } } } class Bar extends Foo { public function baz() { parent::bar(); // shouldn't be allowed } } Essentially, I want certain methods in my parent class to prohibit child classes from calling them. This needs to be bullet-proof, which I doubt this is, so if you know how this could be circumvented, that's what I'm interested in knowing (along with how to prevent it, if possible). Edit: For everyone suggesting private methods, this is not an option, as I need the interface to remain public to be externally accessible. Sorry, I guess I assumed that would be obvious.