Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
What is the difference between wrappers and adapters?
Since they both wrap around another object and provide additional functionallity why are there two separate names?
Also the STL smart pointers are wrappers or adapters?
Wrappers typically store a primitive type and add operations that the type does not support / support poorly-erroneously.
Adapters are used when the interface of a class is not exactly what is needed and provide a wrapping effect while changing the interface.
A way to distinguish the two, is that often an adapter is implemented by private inheritance. This kind of implementation is not usually available for wrappers that wrap a primitive.
Based on this I would call smart pointers wrappes
It is almost the same. The only difference is that if your class provides new interface with the same (or almost the same) functionality as base one it is better to call it adapter.
If you class adds some new functionality (for example some additional processing of base class output) it is better to call it wrapper.
Smart pointers are wrappers because they add additional functionality (reference counting) to base class.
The word "wrapper" is extremely overloaded in software engineering. You use it when you don't want to (or cannot) be more specific. Basically, every time you add an additional layer around something so that this "something" cannot (or should not) be accessed directly anymore, you "wrap" it, whatever the reason may be. The wrapped component may be too low-level, it may be incomplete, you may want to add caching to it, you may simply want to reduce compile time... or whatever reason you can think of. The word "wrapper" alone does not tell you more than that.
Therefore, nobody will object when you say that a smart pointer is a wrapper around a raw pointer. A smart pointer prevents direct access to the raw pointer (or allows it if you need to but reminds you that you should not do it normally). That makes it a wrapper.
"Adapter" is a slightly more specific word. It means that a software component has exactly the features you need but exposes them in a formally wrong way, so you add a layer around the original component just to make its interface conform to your needs. Adapting something is a special case of wrapping. I would not call a smart pointer an adapter. "Adapting" implies that the functionality is basically unchanged but only the names and formalities of the interface are different -- which is not true for smart pointers.
Summary:
"Wrapper" = Adding a layer around a software component for any reason.
"Adapter" = Wrapping with the reason being nothing more than an incompatible
interface.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am writing a game in C++, and I have come upon this design problem:
I have a base class called Entity for all my objects in the game. In order to use inheritance, I am storing the pointers in my octree. In my game, there are objects that will be created by other objects' methods. The problem is: where can I store the objects created this way so that it will stay for the duration of the game so that the pointers I store are valid?
I do not want to use new because I will create probably hundreds or even thousands of objects this way and I heard new is very slow.
The only way I have come up so far is to have a big vector that stores all these objects. The vector will probably be initialized with a huge size so that it won't have to resize itself and messes with my pointers. This seems rather dumb though.
First: don't worry too much about performance. Adhere to some basic rules for that and you'll be fine:
avoid copies of big objects (pass by pointer or reference)
prefer vector over map and set when you have < 50 entries, or do not need ordering
have a look at what the std::algorithm library gives you - these functions are usually fast and tested
most important thing for your project is: think about structure and design
use clear interfaces
have single responsibility of objects
prefer composition over inheritance
as for your concrete problem:
It's fine to have a "ObjectManager" class. as a first implementation a vector is fine, too, just hide that as a implementation detail so you can change it later on if the need arises.
One way of improving the new is to define custom allocators for your object, that take advantage of knowledge about your classes to write a better new than the standard one.
The good news is that you can write your code immediately using new and later create your custom allocator. So don't get lost in the dangers of premature optimization, which is, we all know:
Premature optimisation is the the root of all evil
-- Donald Knuth
If Entity is the base class for all objects, I suppose that you'll make heavy use of polymorphism. Unfortunately, you can't manage easily polymorphic objects in vectors (which would be the more convenient and safer way) because of the slicing issue. So pointer is ok in this case. Maybe you could consider shared_ptr to avoid memory leaks.
This being said, you may be interested in this article about Entity-Component-System pattern in games, and memory allocation.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I found the following definitions from the internet and both sound similar to me :
Abstraction : Abstraction is another good feature of OOPS. Abstraction means to show only the necessary details to the client of the object. Do you know the inner details of the Monitor of your PC? What happen when you switch ON Monitor? Does this matter to you what is happening inside the Monitor? No Right, Important thing for you is weather Monitor is ON or NOT. When you change the gear of your vehicle are you really concern about the inner details of your vehicle engine? No but what matter to you is that Gear must get changed that’s it!! This is abstraction; show only the details which matter to the user.
Let’s say you have a method "CalculateSalary" in your Employee class, which takes EmployeeId as parameter and returns the salary of the employee for the current month as an integer value. Now if someone wants to use that method. He does not need to care about how Employee object calculates the salary? An only thing he needs to be concern is name of the method, its input parameters and format of resulting member, Right?
So abstraction says expose only the details which are concern with the user (client) of your object. So the client who is using your class need not to be aware of the inner details like how you class do the operations? He needs to know just few details. This certainly helps in reusability of the code.
Interface : An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X". Again, as an example, anything that "ACTS LIKE" a light, should have a turn_on() method and a turn_off() method. The purpose of interfaces is to allow the computer to enforce these properties and to know that an object of TYPE T (whatever the interface is ) must have functions called X,Y,Z, etc.
Interfaces in Object Oriented Programming Languages
An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class). For example, say we have a car class and a scooter class and a truck class. Each of these three classes should have a start_engine() action. How the "engine is started" for each vehicle is left to each particular class, but the fact that they must have a start_engine action is the domain of the interface.
Doesn't both the explanations say the same thing? So are they same or different?
An interface tells you what you can do with something. Abstract(ion) might additionally tell you how you do some of these. Thus an interface is always a kind of abstraction, but an abstraction can carry more information than an interface.
In C++-world, unlike e.g. Java, there's no explicit declaration of an interface; instead, your class automatically provides all the interfaces that the base classes provide. Some of us tend to call classes with only pure virtual methods (and, possibly, a non-pure virtual destructor) and interface. Note that, strictly speaking, it's not the only way do specify an interface and new/upcoming C++ features (like Concepts) will likely change this scene. Similarly we usually say that a class is abstract when it has at least one pure virtual method, albeit there might be different definitions when you use template/traits based composition and fulfilling and interface instead of virtuals and inheritance for the same.
Abstraction is to move away from the details, to 'zoom out', if you will. You tend to abstract away from the implementation by creating structures to lay out your code. As an example, rather than thinking in terms of individual cells in a body, you could abstract away to thinking about the person as a whole, or go even further and think about groups of people.
An interface is just that; how you interface with your code. This is normally in the form of public functions in your classes, though not necessarily. Ideally, the interface should describe what something can do, without being affected by how it does it. For example, you might have a function to get a person to walk, but not one to move their individual muscles.
In the context of , say, a C++ function:
The interface describes how a feature is used which is what a function prototype does.
A client calling the function need not worry how the function is implemented (ie how it go about doing things). In short you have a layer of abstraction.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I know that on C pointers can do pretty neat stuff.
But C++ is object oriented. I can refer to an object instead of using pointers. Am I right? So why having pointers in C++?
I can understand that pointers might be implemented for compatibility reasons. Pointers were the power of C. Okay. But really, is there something you can do in C++ and the only (or best) way is using pointers?
Can you give me a good example?
To make it more clear:
Is there something in C++ that I can be done only by using pointer?
Can I avoid pointers and still do everything someone who uses pointers can do in C++?
For starters, polymorphism only works when accessing through
pointers or references. More generally, any sort of a dynamic
structure (graphs, etc.) will require pointers, and it's almost
always necessary to be able to navagate between objects, which
also requires pointers. In fact, the desire to use OO
techniques is often the main reason behind a lot of pointers in
C++.
You might note that “pure” OO languages generally
require that everything be a pointer, for exactly these reasons.
The question is a bit broad and philosophical. In fact, it is impossible to completely omit pointers, as usually the used API demands it (e.g. the arguments of the main function). If pointers are not necessary for a specific fixed technical reason, theoretically it would not be necessary to use pointers.
However, this statement is to be taken cum grano salis; if no pointers are used, it is impossible to have dynamic memory management, allocation and deallocation. On the other hand, if usage of the STL is permitted, all of this is nicely encapsulated; pointers would not be involved except for the used APIs, inside the STL and in the definition of the main function.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'll get to the point and explain below.
What, if any, are the benefits of...
template<class T>
class myStack : public myList<T>...// my stack
over
template<class T, Container = deque<T> >
class stack...// C++ stack
Recently I was writing some code and was faced with an inheritance issue where I was exposing aspects of the base class that I would rather not. The specific example isn't important so I'll relate it to few semesters ago when I took a data structures class where we implemented our own list, stack, queue and others.
In this class we were to design a stack which was to inherit from a list. The problem with this was I was exposing public methods of the base that could potentially damage the stack's integrity. It may be that I'm a bit of a purist but having the insert() and remove() lying around in the stack was bothersome for me. I didn't have time to investigate it then but this time I thought I would consult the C++ standard to see how the stack was defined there. Low and behold I found the code above; it was an obvious solution that I overlooked.
Here's my view...
The C++ implementation is "better" because it allows the user the freedom to choose the underlying structure if desired and maintains a more pure stack in that it is clear only stack functionality available to the user and can be more guarded from unintended corruption. Are there more substantial, non-subjective reasoning behind the design choice, or inherit flaws in it?
The obvious benefit of mine is code re-use which is par for the course, I don't see that as an additional benefit the way I personally see the benefit of the freedom with the C++ implementation. I do see the over exposure (my words) of the base class as a con though. Are there more substantial, non-subjective reasoning behind the design choice, or inherit flaws in it?
Again, I'm not concerned with languages, I'm more concerned with weighing the pros/cons for my own designs.
C++ collections and Java collections are very different. Java collections have an obvious type hierarchy, whereas most C++ collection types do not extend any other class, and templates are extensively used to support multiple collection types.
Although I don't know for sure, I imagine that the Java library developers made Stack a subclass of Vector because a stack is a collection of elements in a well defined order, so acts like a list, and by subclassing Vector they could get most of the implementation of the stack for free. This also has the benefit that you can use stacks in places where you need a list or a vector, for example you could pass a stack to a function that takes a list and iterates over it. Of course, c++ stacks are not iterable, so there is (intentianal or not) a very different semantics between the two stacks.
Finally, for your own code, if you are considering whether B should inherit or contain A, first ask yourself if B is an A, or more specifically, if you would ever want to treat a B as an A by passing it to a function that expects an A, or returning it from a function that needs to return an A. If so you should use inheritance, otherwise you should probably use composition.
The Stack class, as well as Vector, are legacy containers. They are left-overs of JDK1.0, they are based on an older design of the utils library, and are inefficient because of synchronization.
The preferred implementation of Stack in Java is given by implementations of the Deque interface (mainly ArrayDeque and LinkedList). You get the difference: in C++ one says that an stack has a given implementation. In Java one declares a class implementing the desired interface:
class ArrayDeque<E> extends AbstractCollection<E>
implements Collection<E>, Deque<E>, Queue<E> //etc
When using such classes, always take the less specialized interface possible, for instance:
Deque<String> stack = new LinkedList<String>();
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
http://msdn.microsoft.com/en-us/library/es7h5kch.aspx
This feature looks pretty sweet. This would be great because you wouldn't need getters any more. You could make member variables public; later, if you change your mind, you can turn them into property.
Is this something that the standard committee has considered? Is this something they would likely accept or reject? Does this already exist and I just don't know about it yet?
Since this is already possible in standard-compliant C++ using a library-only approach (proxy objects), it's unlikely to see language changes and new keywords. Unlike lambdas (for instance), the possible syntactic sugar here isn't all that sweet (doesn't change the level of abstraction much).
BTW, changing fields (wrapperless member variables) into properties is always a breaking change, because you can form pointers and references to fields and read/write them directly. With a property, you'd need a wrapper in order to ensure that the getters and setters are used.
The reason for the Microsoft extension is that it doesn't simply provide properties (interception of read and write to getter and setter functions) in C++ code. It generates .NET metadata or WinRT metadata (in C++/CLI and C++/CX, respectively) which is used by reflection. Standard C++ doesn't have reflection or metadata.
Also note that C++ developers tend not to use the proxy object pattern very much, which indicates to the committee that there isn't much demand for a "cleaner" syntactic sugar, either.
This is old. Borland added it first, back in 90s, with C++ Builder if I'm not mistaken. Microsoft followed suit, they were highly competitive back then. COM Automation was a pretty major motivator.
Given that this had three C++ language versions to stew, along with the controversy around it, the odds that this will be considered for a future revision are about zilch.
Properties have their use but they cause considerable implementation difficulties. Passing a property by reference for example is a very difficult problem to solve. The callee would need to know that an accessor needs to be called, instead of just dereferencing a pointer, and would need to know whether a getter or setter even exists. The only practical solution is to just forbid it, not exactly attractive in C++. Additional problems add up when you start to support default properties, very hard to pass up, introducing syntax ambiguity.