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

Home
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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Design Pattern » Bridge Pattern 




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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.