Bridge Pattern in Java 3 : Bridge Pattern « Design Pattern « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Design Pattern » Bridge PatternScreenshots 
Bridge Pattern in Java 3
Bridge Pattern in Java 3

//[C] 2002 Sun Microsystems, Inc.---
import java.util.ArrayList;

public class RunBridgePattern {
    public static void main(String [] arguments){
        System.out.println("Example for the Bridge pattern");
        System.out.println();
        System.out.println("This example divides complex behavior among two");
        System.out.println(" classes - the abstraction and the implementation.");
        System.out.println();
        System.out.println("In this case, there are two classes which can provide the");
        System.out.println(" abstraction - BaseList and OrnamentedList. The BaseList");
        System.out.println(" provides core funtionality, while the OrnamentedList");
        System.out.println(" expands on the model by adding a list character.");
        System.out.println();
        System.out.println("The OrderedListImpl class provides the underlying storage");
        System.out.println(" capability for the list, and can be flexibly paired with");
        System.out.println(" either of the classes which provide the abstraction.");
        
        System.out.println("Creating the OrderedListImpl object.");
        ListImpl implementation = new OrderedListImpl();
        
        System.out.println("Creating the BaseList object.");
        BaseList listOne = new BaseList();
        listOne.setImplementor(implementation);
        System.out.println();
        
        System.out.println("Adding elements to the list.");
        listOne.add("One");
        listOne.add("Two");
        listOne.add("Three");
        listOne.add("Four");
        System.out.println();
        
        System.out.println("Creating an OrnamentedList object.");
        OrnamentedList listTwo = new OrnamentedList();
        listTwo.setImplementor(implementation);
        listTwo.setItemType('+');
        System.out.println();
        
        System.out.println("Creating an NumberedList object.");
        NumberedList listThree = new NumberedList();
        listThree.setImplementor(implementation);
        System.out.println();
        
        System.out.println("Printing out first list (BaseList)");
        for (int i = 0; i < listOne.count(); i++){
            System.out.println("\t" + listOne.get(i));
        }
        System.out.println();
        
        System.out.println("Printing out second list (OrnamentedList)");
        for (int i = 0; i < listTwo.count(); i++){
            System.out.println("\t" + listTwo.get(i));
        }
        System.out.println();
        
        System.out.println("Printing our third list (NumberedList)");
        for (int i = 0; i < listThree.count(); i++){
            System.out.println("\t" + listThree.get(i));
        }
    }
}
interface ListImpl{
    public void addItem(String item);
    public void addItem(String item, int position);
    public void removeItem(String item);
    public int getNumberOfItems();
    public String getItem(int index);
    public boolean supportsOrdering();
}
class BaseList{
    protected ListImpl implementor;
    
    public void setImplementor(ListImpl impl){
        implementor = impl;
    }
    
    public void add(String item){
        implementor.addItem(item);
    }
    public void add(String item, int position){
        if (implementor.supportsOrdering()){
            implementor.addItem(item, position);
        }
    }
    
    public void remove(String item){
        implementor.removeItem(item);
    }
    
    public String get(int index){
        return implementor.getItem(index);
    }
    
    public int count(){
        return implementor.getNumberOfItems();
    }
}

class NumberedList extends BaseList{
    public String get(int index){
        return (index + 1". " super.get(index);
    }
}

class OrderedListImpl implements ListImpl{
    private ArrayList items = new ArrayList();
    
    public void addItem(String item){
        if (!items.contains(item)){
            items.add(item);
        }
    }
    public void addItem(String item, int position){
        if (!items.contains(item)){
            items.add(position, item);
        }
    }
    
    public void removeItem(String item){
        if (items.contains(item)){
            items.remove(items.indexOf(item));
        }
    }
    
    public boolean supportsOrdering(){
        return true;
    }
    
    public int getNumberOfItems(){
        return items.size();
    }
    
    public String getItem(int index){
        if (index < items.size()){
            return (String)items.get(index);
        }
        return null;
    }
}

class OrnamentedList extends BaseList{
    private char itemType;
    
    public char getItemType(){ return itemType; }
    public void setItemType(char newItemType){
        if (newItemType > ' '){
            itemType = newItemType;
        }
    }
    
    public String get(int index){
        return itemType + " " super.get(index);
    }
}


           
       
Related examples in the same category
1. Bridge Pattern 1
2. Bridge pattern in Java
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.