In general, Bag doesn't implies a sequenced structure (of course you can use a list to implement the internal structure, but not all the Bag does). Hence, it is possible that after we put a series of objects to the Bag, we cannot get them back in the sequence we expected, in fact, we lost the sequence. Therefore, a Bag may not necessary able to perform all the tasks done by a Stack.
But it's true that many programmers in the market work around with their data structure as you did. They use a list to store objects and implement bag alike methods, with put() where they can specify the exact location to store the object, but this is not simply a Bag anymore.
Summaries of concepts and practices in software engineering and project management
Showing posts with label Object Design. Show all posts
Showing posts with label Object Design. Show all posts
26.4.07
Delegation, Object composition and Has-a relationship
Delegation or Object composition is a way to construct the has-a relationships.
Why the subclass cannot strengthen the precondition?
Subclass can strengthen the invariants and postconditions of the inherited contract and can only weaken its preconditions. This is because if the precondition of the subclass method is strengthened, the method will not able to execute if a superclass is being substituted with the subclass. So we can only weaken its preconditions.
Subscribe to:
Posts (Atom)