Tag Info

New answers tagged

0

I like the question, my two cents: Your two approaches are radically different: The first one is OO ans strongly typed - but not extensible The second one is weakly typed (string encapsulates anything) In C++, many would use a std::map of boost::variant to achieve a mix of both. Disgression: Note that some languages, such as C#, allows the dynamic ...


4

It is entirely possible although you have to be consistent with your types. The technical term for this is closure. public Action MakeAction(State s) { var me = new Item(); return () => s.Stack.Push(me); }


4

Lets start out with two simple classes: package com.michaelt.so.supers; public class Sup { int methodA(int a, int b) { return a + b; } } and then package com.michaelt.so.supers; public class Sub extends Sup { @Override int methodA(int a, int b) { return super.methodA(a, b); } } Compiling methodA and looking at the ...


7

Option 2. The bytecode is referenced dynamically at runtime: this is why, for example, LinkageErrors occur. For example, assume you compile two classes: public class Parent { public void doSomething(String x) { ... } } public class Child extends Parent { @Override public void doSomething(String x) { super.doSomething(x); ... } } Now ...


0

the code is not copied, it is accessed by reference: the subclass references its methods and the superclass the superclass references its methods compilers may optimize how this is represented/executed in memory, but that's basically the structure


2

This is basically question of Composition vs. inheritance. In your first case, you use inheritance as way to share behavior. In second instance, you use composition. Also, in second case, it is easy to identify a Strategy pattern in IDownloadClient. So some would say the second case is better. But there is also KISS and YAGNI. Your second case is already ...


4

There is no such thing as NoSQL. There is a myriad of new database technologies grouped under that label, and they all work completely different. But when you are talking about document-oriented, schemaless databases like MongoDB, which are one subset of NoSQL, then yes, these are often more suitable for modeling object-oriented hierarchies than relational ...


0

I think this quote from the Polymorphism tutorial on Oracle should give us a clear idea of what the authors were thinking of when they wrote the question: The Java virtual machine (JVM) calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable's type. This behavior is ...


5

None of the above. There is no universal term, it depends on the language and the community, but the correct answer is one of message dispatch (virtual) method dispatch (virtual) method resolution (virtual) method lookup vtable lookup or a similar term.


9

The question asks "which process determines which method should execute?" This is a bad question. But, we can immediately eliminate three of the choices: Is-A, Has-A, and Parent Class, since those are object-oriented, but not certainly not processes. Even if Is-A and Has-A were processes, they would be processes regarding class and composition, as you ...


1

I would go with what you have listed in the Example 2. Reason to do so is simple - It keeps things simple. Rather than naming the files really long and using a class name really long does not help in anyway. To bring in the effect of link i.e. between the abstract and derived classes, please use namespaces or folder structure. IMHO, class names need not be ...


1

You would declare a class abstract when you don't want the developer (probably yourself) to be allowed to instantiate it, because it wouldn't work or wouldn't make sense. For example, consider a game where there are different types of game entities. They all inherit from the base GameEntity class. abstract class GameEntity{ int lifePoint, speed, ...


1

I need a way out of this conundrum, one that has the smallest technical cost in relation to changing the other controllers, I need a sort of "structural polymorphism" for these types, where the type is the same but its internal structure different. You mean like basic inheritance? I'm not sure I see the trouble here. If you have a common set of ...


1

Personally I think this principle should be taken with a pinch of salt. Code is organic, businesses change and code changes according to the needs of a business as time goes on. I find it very difficult to get my head around the fact that abstraction is key. What if the abstraction was originally wrong? What if the business function has changed ...


0

Decoration is about adding subtle behavioural changes to an underlying class. I think your intention is to actually Extend a class. Think "Separation of Concerns". Also, the "Open/Closed" principle springs to mind (which I personally don't entirely agree with) The point of decoration is that you pass an object around which supports a given interface. If ...


2

The decorator pattern is typically used to avoid an explosion of subclasses. A common example involves ui windows that you may want to have any combination of n attributes (scrollbar, titlebar, resizable, movable, etc.). Supporting all combinations of those n attributes would involve making 2^n subclasses. The decorator pattern prevents the explosion by ...


0

Dynamic binding is another name for Late Binding. That's where the language ties a data element or method to an object after compilation time. Wikipedia equates dynamic & late binding: Dynamic binding (computing), also known as late binding links to http://en.wikipedia.org/wiki/Dynamic_binding_(computing) Javascript was my first exposure ...


2

If anyone else finds this question relevant, I'm following-up with my chosen solution. Bottom line: when you can't find a simple solution to a seemingly simple problem, it may be a sign that analysis and refactoring of the existing design is needed. Analyzing my code, I realized that the Sequence class had fairly overloaded accessors that as designed could ...


1

In PHP you cannot have a class that extends two base classes - see the diamond problem. Your UML diagram shows a base class and an interface. In PHP you could implement one of the subclasses in your UML like so: class Customer extends p-info implements i-control { // Concrete implementation... }


2

Multiple inheritance is only supported in few languages, notably C++. It can lead to some very strange effects if the same methods are defined in two different classes that are both extended by a subclass. Instead, you should use interfaces (http://www.php.net/manual/en/language.oop5.interfaces.php). You will have to define (implement) the methods in those ...


2

An extended class is always dependent on a single base class, that is, multiple inheritance is not supported. Also, in the UML example you posted there is nothing that has 2 parent classes, just some classes that all extend one class and implement an interface. Furthermore, most UML tools are not bound to one specific language's syntax and quirks, so not ...


2

Disadvantages are that: it is not obvious where it is used when you use something that uses it, you cannot supply alternatives (esp. when testing, you do not want to use eg. real credit card payment) when you decide that you want two contexts in which it should be unique you must rewrite every usage. These disadvantages are sometimes acceptable, ...


3

The default XML Serializer is quite flexible. Ignores (by default) what doesn't know (that's it information on the xml file/stream for which there is no member variable), and what it knows and it is not present (in the xml file/stream), it gives defaults. So, a very dirty way is just to add what you need where you need it. It will work. It will not be ...


1

First JavaScript doesn't have Classes. Second, your third option seems more rational to me, but it highly depends to your requirements as well. Also you should not be much worried about exposing the helper function. The pros of the solution totally justifies the compromise to me. Third, your time as a developer is valuable; Don't make trivial tasks hard to ...


3

Avoid the Gang of Four Singleton pattern, for reasons cited in the other answers. Mainly it is an anti-pattern based on difficulties it creates for testing. Factory and Dependency Injection made Singleton obsolete. The best answer is to use a Factory that decides whether to instantiate one instance, or many, of a given class. That way, the class and its ...


1

Singletons objects made so there can only be 1 instance at any given time and can be used application wide. Objects that handle stuff like connection pools are a good candidate to be Singletons; You only want 1 instance in the entire application you must be able to access it from different parts of the application the data it holds has to be persisted ...


4

A case where a static class might be a good idea is when you want to collect related pieces of functionality, but you don't need to have any internal state in any object. An example could be the Math class in Java. It contains a whole bunch of related functions that are accessed outside the context of any specific object instance. I've done similar things ...


0

I agree completely with Chao's answer and so I will not repeat that. I think that the real problem here is that you need to ask yourself some more fundamental questions about the problem domain. Remember that all code is written to solve a problem. Why are we writing a base Documentclass in the first place? Why are we deriving from the Document class? ...


2

Now, I can call both swan.fly() and dove.fly(), but swan gets its implementation from FlyingBird. The idiom "Favor composition over inheritance" tells me to favor the Dove implementation over Swan, but "subtype polymorphism" doesn't seem to make that distinction. Does subtype polymorphism distinguish between inheriting behavior, or inheriting an ...


0

In your example, Flyable and FlyingBird are not related. Therefore there's no real distinction to be made for the compiler, it just sees two completely different objects that happens to each have a fly() method. If you declare a Flyable variable and call its fly method, no matter what the instance is, it shall be the instance method matching the interface's ...


0

I already have posted an answer on Stackoverflow. Basically, an aggregation is stronger than a simple association but aggregated objects can go on "living" without each other as with a simple association. A composition is even stronger than an aggregation because the aggregated class cannot be aggregated by other classes. Its "life" depends on the ...


6

The three links Association, Aggregation and Composition form a kind of scale on how closely two classes are related to each other. On the one end of the scale, there is Association, where objects of the two classes can know about each other, but they do not affect each others lifetime. The objects can exist independently and which class A object knows ...


6

If you look at UML of strategy pattern you mentioned, you will notice Option 1 is correct. To explain it properly. In UML, the association relation (eg. the arrow) usually means the entity has attribute that is of type the arrow is pointing at. Which is exactly your case of association between SomeClass and SuperType. Option 2 would mean SomeClass has 3 ...


0

Very simple example I can think of is, if you want to do some processing on input arguments (or some manipulation) which are passed to main function. So in this case if processing is big and same functionality will not be used anywhere else it will make sense to have a private function as it will not be used/called from anywhere else + static as main is ...


0

In my experience:- Large classes are exponentially more difficult to maintain as they get larger. Small classes are easier to maintain and the difficulty of maintaining a collection of small classes increases arithmetically to the number of classes. Sometimes large classes are unavoidable a big problem sometimes needs a big class, but, they are much more ...


2

If you have reversed engineered code into a class diagram and your class diagram is too complex, that probably indicates a very complex design. There's really two approaches - rethink your design and implementation or reduce what you are showing on your model. You could produce alternate views of the software, using multiple class diagrams to show pieces of ...


4

I think I have an unusual perspective in that I've worked in both finance and games. Many of the programmers I met in games were terrible at Software Engineering - but they had no need for practices like SOLID. Traditionally, they put their game in a box - then they're done. In finance, I've found developers can be really sloppy and undisciplined because ...


3

The pattern you describe is what I know as MVP, Model View Presenter. This is an alternative to the MVC in that the Presenter contains the logic for updating the UI code without the View requesting the data from the Model. The traditional MVC pattern does indeed allow for the View to know of the Model. When to favor one over the other? Purists will ...


3

The whole idea is that you have object defined in one way, eg. using width and length and you can calculate other parameters, eg. diagonal and area from them. First your constructor/init only accepts length and width, but the class could also have static createFromDiagonal method, that accept diagonal and area, calculates the width and length, creates the ...


0

The first approach seems totally off to me. You're clearly violating the Single Responsibility Principle in your Model design. Maintaining and extending the Model will be harder and less efficient. Also writing meaningful Unit Tests could be quite painful. In your second design you're implementing the Strategy Pattern and it's a good mix with MVC. I prefer ...


0

In short: you are on a right track and second design is more suitable to MVC pattern. Because in MVC pattern we should keep business logic in the Model. Also having 2 controllers with single view is also violation of single responsibility principle. It would require a rework if there would be any change on UI (either if encryption or decipher changes) I ...


2

1: Some wording: We don't instantiate objects at all, we instantiate classes, and the product of a class instantiation is an object (also called an instance). Of course we can instantiate a class as many times as required to create a new and different object with each instantiation. 2: Whatever, you cannot have the code you suggest: Test test = new Test(); ...


0

What you're describing is the Template Method Pattern. The benefit is to describe an overall process (like log in, do something, log out) while the actual steps (or some of them) are implemented by concrete sub-classes.


0

UML has some nice parts, but it's not good for everything. There are better notations to communicate certain information (e.g. high level software architecture). It can also be very complicated. I found that it's often better to invent notation on-the-fly while drawing a diagram. If I carefully define meanings for my boxes and arrows, diagram is cleaner and ...


2

UML is just a communication tool. Knowing UML would help you to communicate with other persons who know it too, but only in that case. If you work with peers who use UML daily, then go for it. UML is a great tool which helps communicating clearly, presenting things in a visual way and reducing verbosity. It's like using technical terms with people who ...


1

There are two ways that UML can be used: To specify a system or module To describe a system or module In the first case, the UML will likely be used to automatically generate code from, so you need to know enough of the details of UML that you get the expected results from your code generation tool. In the second case, the UML diagrams are only used to ...


9

The short answer is no, monads are not an alternative to inheritance hierarchies (also known as subtype polymorphism). You seem to be describing parametric polymorphism, which monads make use of but are not the only thing to do so. As far as I understand them, monads have essentially nothing to do with inheritance. I would say that the two things are ...


4

Here's my opinion: Although SOLID principles aim for a non-redundant and flexible codebase, this might be a trade-off in readability and maintenance if there are too many classes and layers. It's especially about the number of abstractions. A large number of classes might be ok if they are based on few abstractions. Since we don't know the size and the ...


11

Are there cases in OOP where some or all of the SOLID principles do not lend themselves to clean code? In general, no. History has shown that the SOLID principles all largely contribute to increased decoupling, which in turn has been shown to increase flexibility in code and thus your ability to be accommodating of change as well as making the code ...


1

In my early developing days, I began to write a Roguelike in C++. As I wanted to apply the good object-oriented methodology I learned from my education, I immediately saw the game items as being C++ objects. Potion, Swords, Food, Axe, Javelins etc. all derived from a basic core Item code, handling name, icon, weight, that kind of stuff. Then, I coded bag ...



Top 50 recent answers are included