#Composite_Design_Pattern
👉Rules of thumb
1️⃣Composite and Decorator have similar structure diagrams, reflecting the fact that both rely on recursive composition to organize an open-ended number of objects.
2️⃣Composite can be traversed with Iterator. Visitor can apply an operation over a Composite. Composite could use Chain of Responsibility to let components access global properties through their parent. It could also use Decorator to override these properties on parts of the composition. It could use Observer to tie one object structure to another and State to let a component change its behavior as its state changes.
3️⃣Composite can let you compose a Mediator out of smaller pieces through recursive composition.
4️⃣Decorator is designed to let you add responsibilities to objects without subclassing. Composite's focus is not on embellishment but on representation. These intents are distinct but complementary. Consequently, Composite and Decorator are often used in concert.
5️⃣Flyweight is often combined with Composite to implement shared leaf nodes.
@javaCode☕️
👉Rules of thumb
1️⃣Composite and Decorator have similar structure diagrams, reflecting the fact that both rely on recursive composition to organize an open-ended number of objects.
2️⃣Composite can be traversed with Iterator. Visitor can apply an operation over a Composite. Composite could use Chain of Responsibility to let components access global properties through their parent. It could also use Decorator to override these properties on parts of the composition. It could use Observer to tie one object structure to another and State to let a component change its behavior as its state changes.
3️⃣Composite can let you compose a Mediator out of smaller pieces through recursive composition.
4️⃣Decorator is designed to let you add responsibilities to objects without subclassing. Composite's focus is not on embellishment but on representation. These intents are distinct but complementary. Consequently, Composite and Decorator are often used in concert.
5️⃣Flyweight is often combined with Composite to implement shared leaf nodes.
@javaCode☕️
👍1
#Java_Interview_Question
139) What is the difference between List and Set?
List can contain duplicate elements whereas Set contains only unique elements.
@javaCode☕️
139) What is the difference between List and Set?
List can contain duplicate elements whereas Set contains only unique elements.
@javaCode☕️
👍1
#Java_Interview_Question
140) What is the difference between HashSet and TreeSet?
HashSet maintains no order whereas TreeSet maintains ascending order.
@javaCode☕️
140) What is the difference between HashSet and TreeSet?
HashSet maintains no order whereas TreeSet maintains ascending order.
@javaCode☕️
#Java_Interview_Question
141) What is the difference between Set and Map?
Set contains values only whereas Map contains key and values both.
@javaCode☕️
141) What is the difference between Set and Map?
Set contains values only whereas Map contains key and values both.
@javaCode☕️
👍2
#Java_Interview_Question
142) What is the difference between HashSet and HashMap?
HashSet contains only values whereas HashMap contains entry(key,value). HashSet can be iterated but HashMap need to convert into Set to be iterated.
@javaCode☕️
142) What is the difference between HashSet and HashMap?
HashSet contains only values whereas HashMap contains entry(key,value). HashSet can be iterated but HashMap need to convert into Set to be iterated.
@javaCode☕️
#Composite_Design_Pattern
👉Opinions
The whole point of the Composite pattern is that the Composite can be treated atomically, just like a leaf. If you want to provide an Iterator protocol, fine, but I think that is outside the pattern itself. At the heart of this pattern is the ability for a client to perform operations on an object without needing to know that there are many objects inside.
☑️to be continued...
@javaCode☕️
👉Opinions
The whole point of the Composite pattern is that the Composite can be treated atomically, just like a leaf. If you want to provide an Iterator protocol, fine, but I think that is outside the pattern itself. At the heart of this pattern is the ability for a client to perform operations on an object without needing to know that there are many objects inside.
☑️to be continued...
@javaCode☕️
☕️JAVA Language Community
#Composite_Design_Pattern 👉Opinions The whole point of the Composite pattern is that the Composite can be treated atomically, just like a leaf. If you want to provide an Iterator protocol, fine, but I think that is outside the pattern itself. At the heart…
👉Opinion
Being able to treat a heterogeneous collection of objects atomically (or transparently) requires that the "child management" interface be defined at the root of the Composite class hierarchy (the abstract Component class). However, this choice costs you safety, because clients may try to do meaningless things like add and remove objects from leaf objects. On the other hand, if you "design for safety", the child management interface is declared in the Composite class, and you lose transparency because leaves and Composites now have different interfaces.
Smalltalk implementations of the Composite pattern usually do not have the interface for managing the components in the Component interface, but in the Composite interface. C++ implementations tend to put it in the Component interface. This is an extremely interesting fact, and one that I often ponder. I can offer theories to explain it, but nobody knows for sure why it is true.
☑️to be continued...
@javaCode☕️
Being able to treat a heterogeneous collection of objects atomically (or transparently) requires that the "child management" interface be defined at the root of the Composite class hierarchy (the abstract Component class). However, this choice costs you safety, because clients may try to do meaningless things like add and remove objects from leaf objects. On the other hand, if you "design for safety", the child management interface is declared in the Composite class, and you lose transparency because leaves and Composites now have different interfaces.
Smalltalk implementations of the Composite pattern usually do not have the interface for managing the components in the Component interface, but in the Composite interface. C++ implementations tend to put it in the Component interface. This is an extremely interesting fact, and one that I often ponder. I can offer theories to explain it, but nobody knows for sure why it is true.
☑️to be continued...
@javaCode☕️
#Java_Interview_Question
143) What is the difference between HashMap and TreeMap?
HashMap maintains no order but TreeMap maintains ascending order.
@javaCode☕️
143) What is the difference between HashMap and TreeMap?
HashMap maintains no order but TreeMap maintains ascending order.
@javaCode☕️
#Java_Interview_Question
144) What is the difference between HashMap and Hashtable?
1) HashMap is not synchronized. Hashtable is synchronized.
2) HashMap can contain one null key and multiple null values.
@javaCode☕️
144) What is the difference between HashMap and Hashtable?
1) HashMap is not synchronized. Hashtable is synchronized.
2) HashMap can contain one null key and multiple null values.
@javaCode☕️
👍1
#Java_Interview_Question
145) What is the difference between Collection and Collections?
Collection is an interface whereas Collections is a class.
Collection interface provides normal functionality of data structure to List, Set and Queue.
But, Collections class is to sort and synchronize collection elements.
@javaCode☕️
145) What is the difference between Collection and Collections?
Collection is an interface whereas Collections is a class.
Collection interface provides normal functionality of data structure to List, Set and Queue.
But, Collections class is to sort and synchronize collection elements.
@javaCode☕️
☕️JAVA Language Community
👉Opinion Being able to treat a heterogeneous collection of objects atomically (or transparently) requires that the "child management" interface be defined at the root of the Composite class hierarchy (the abstract Component class). However, this choice costs…
#Composite_Design_Pattern
👉Opinion
My Component classes do not know that Composites exist. They provide no help for navigating Composites, nor any help for altering the contents of a Composite. This is because I would like the base class (and all its derivatives) to be reusable in contexts that do not require Composites. When given a base class pointer, if I absolutely need to know whether or not it is a Composite, I will use dynamic_cast to figure this out. In those cases where dynamic_cast is too expensive, I will use a Visitor.
Common complaint: "if I push the Composite interface down into the Composite class, how am I going to enumerate (i.e. traverse) a complex structure?" My answer is that when I have behaviors which apply to hierarchies like the one presented in the Composite pattern, I typically use Visitor, so enumeration isn't a problem - the Visitor knows in each case, exactly what kind of object it's dealing with. The Visitor doesn't need every object to provide an enumeration interface.
☑️to be continued...
@javaCode☕️
👉Opinion
My Component classes do not know that Composites exist. They provide no help for navigating Composites, nor any help for altering the contents of a Composite. This is because I would like the base class (and all its derivatives) to be reusable in contexts that do not require Composites. When given a base class pointer, if I absolutely need to know whether or not it is a Composite, I will use dynamic_cast to figure this out. In those cases where dynamic_cast is too expensive, I will use a Visitor.
Common complaint: "if I push the Composite interface down into the Composite class, how am I going to enumerate (i.e. traverse) a complex structure?" My answer is that when I have behaviors which apply to hierarchies like the one presented in the Composite pattern, I typically use Visitor, so enumeration isn't a problem - the Visitor knows in each case, exactly what kind of object it's dealing with. The Visitor doesn't need every object to provide an enumeration interface.
☑️to be continued...
@javaCode☕️
#Java_Interview_Question
👉The rules for #overriding a method are as follows:
* The argument list must exactly match that of the overridden method. If they don't match, you can end up with an overloaded method you didn't intend.
* The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the superclass.
* The access level can't be more restrictive than the overridden method's.
* The access level CAN be less restrictive than that of the overridden method.
* Instance methods can be overridden only if they are inherited by the subclass. A subclass within the same package as the instance's superclass can override any superclass method that is not marked private or final. A subclass in a different package can override only those non-final methods marked pub- lic or protected (since protected methods are inherited by the subclass).
* The overriding method CAN throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception.
* The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.
* The overriding method can throw narrower or fewer exceptions. Just because an overridden method "takes risks" doesn't mean that the overriding subclass' exception takes the same risks. Bottom line: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
* You cannot override a method marked final.
* You cannot override a method marked static.
* If a method can't be inherited, you cannot override it. Remember that overriding implies that you're reimplementing a method you inherited!
@javaCode☕️
👉The rules for #overriding a method are as follows:
* The argument list must exactly match that of the overridden method. If they don't match, you can end up with an overloaded method you didn't intend.
* The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the superclass.
* The access level can't be more restrictive than the overridden method's.
* The access level CAN be less restrictive than that of the overridden method.
* Instance methods can be overridden only if they are inherited by the subclass. A subclass within the same package as the instance's superclass can override any superclass method that is not marked private or final. A subclass in a different package can override only those non-final methods marked pub- lic or protected (since protected methods are inherited by the subclass).
* The overriding method CAN throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception.
* The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.
* The overriding method can throw narrower or fewer exceptions. Just because an overridden method "takes risks" doesn't mean that the overriding subclass' exception takes the same risks. Bottom line: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
* You cannot override a method marked final.
* You cannot override a method marked static.
* If a method can't be inherited, you cannot override it. Remember that overriding implies that you're reimplementing a method you inherited!
@javaCode☕️
#Java_Interview_Question
👉The rules for #overloading a method are as follows:
■ Overloaded methods MUST change the argument list.
■ Overloaded methods CAN change the return type.
■ Overloaded methods CAN change the access modifier.
■ Overloaded methods CAN declare new or broader checked exceptions.
■ A method can be overloaded in the same class or in a subclass. In other words, if class A defines a doStuff(int i) method, the subclass B could define a doStuff(String s) method without overriding the superclass version that takes an int. So two methods with the same name but in different classes can still be considered overloaded, if the subclass inherits one version of the method and then declares another overloaded version in its class definition.
@javaCode☕️
👉The rules for #overloading a method are as follows:
■ Overloaded methods MUST change the argument list.
■ Overloaded methods CAN change the return type.
■ Overloaded methods CAN change the access modifier.
■ Overloaded methods CAN declare new or broader checked exceptions.
■ A method can be overloaded in the same class or in a subclass. In other words, if class A defines a doStuff(int i) method, the subclass B could define a doStuff(String s) method without overriding the superclass version that takes an int. So two methods with the same name but in different classes can still be considered overloaded, if the subclass inherits one version of the method and then declares another overloaded version in its class definition.
@javaCode☕️
👍2
#Java_Interview_Question
☑️NOTICE:
Which overridden version of the method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type, but which overloaded version of the method to call is based on the reference type of the argument passed at compile time.
@javaCode☕️
☑️NOTICE:
Which overridden version of the method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type, but which overloaded version of the method to call is based on the reference type of the argument passed at compile time.
@javaCode☕️