Presentation is loading. Please wait.

Presentation is loading. Please wait.

Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.

Similar presentations


Presentation on theme: "Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component."— Presentation transcript:

1 Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component 4.Composite design pattern 5.Composite method typically invoke component methods 6.E.g. Container.getPreferredSize invokes getPreferredSize of components Consider an Example : p1.add(b1); // Add Button b1 to Japnel p1 p1.add(b2); // Add Button b2 to Japnel p1 p1.add(b3); // Add Button b3 to Japnel p1 C1.add(p1) // Add method will be invoked on all primitive objects. b1,b2,b3 -  Primitive objects p1 -  Composite Object

2 Composite Pattern Context Primitive objects can be combined to composite objects Clients treat a composite object as a primitive object Interface for composite object and primitive object should be same

3 Solution 1.Define an interface type that is an abstraction for the primitive objects 2.Composite object collects primitive objects 3.Composite and primitive classes implement same interface type. 4.When implementing a method from the interface type, the composite class applies the method to its primitive objects and combines the results JButtonJPanel, JFrame

4 Name in Design PatternActual Name (AWT components) PrimitiveComponent CompositeContainer Leafa component without children (e.g. JButton) method()a method of Component (e.g. getPreferredSize)

5 Scroll Bars 1.Scroll Bars can be added to components to display more information that can be shown on screen. 2.Adding a Scroll bar JTextArea area = new JTextArea(10,25); JScrollPane scroller = new JScrollPane(area); 3. Scroll bars adds/enhances functionality to the underlying component. 4. Adding/Enhancing Functionality is called Decoration. 5. JScrollPane Decorates a Component and is again a Component.

6 Decorator Pattern Context 1.Component objects can be decorated (visually or behaviorally enhanced) 2.The decorated object can be used in the same way as the undecorated object 3.The component class does not want to take on the responsibility of the decoration 4.There may be an open-ended set of possible decorations Solution 1.Define an interface type that is an abstraction for the component 2.Concrete component classes realize this interface type. 3.Decorator classes also realize this interface type. 4.A decorator object manages the component object that it decorates 5.When implementing a method from the component interface type, the decorator class applies the method to the decorated component and combines the result with the effect of the decoration.

7 Name in Design PatternActual Name (scroll bars) Component ConcreteComponentJTextArea DecoratorJScrollPane method()a method of Component (e.g. paint)

8 Example Line Item Interface public interface LineItem { double getPrice(); String toString(); } class Product implements LineItem { private String description; private double price; Product(String description, double price) { this.description = description; this.price = price; } public double getPrice() { return price;} public String toString() { return descrition ;} } Primitive Object Composite Object

9 import java.util.*; public class Bundle implements LineItem { private ArrayList items; public Bundle() { items = new ArrayList (); } public void add(LineItem item) { items.add(item); } public double getPrice() { double price = 0; for (LineItem item : items) price += item.getPrice(); return price; } public String toString() { String description = "Bundle: "; for (int i = 0; i < items.size(); i++) { if (i > 0) description += ", "; description += items.get(i).toString(); } return description; } }


Download ppt "Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component."

Similar presentations


Ads by Google