bridge bridge mode

Hypothetical demand

Are black or white English-speaking whites are Chinese-speaking whites are Chinese-speaking black English-speaking black

As a general method of subclass the parent class could be produced four sub-classes to this is a combination of 2 * 2 If the branch is more of the N * N

Solution

Bridge mode

Separation of the type in which a stable branch with the inheritance system to distinguish between acts of the other branches of interface package behavior (strategy pattern?)

Next, look at the code

Abstract people

public abstract class People {
    protected Language language;
    
    abstract void speak();
}


Black

public class BlackPeople extends People {

    /**
     * 
     */
    public BlackPeople() {
    }
    
    public BlackPeople(Language language){
        this.language=language;
    }    
    
    @Override
    void speak() {
        System.out.println("I'm black");
        language.speak();
    }

}


White

public class WhitePeople extends People {

    /**
     * 
     */
    public WhitePeople() {
    }
    
    public WhitePeople(Language language){
        this.language=language;
    }    
    
    @Override
    void speak() {
        System.out.println("I'm white");
        language.speak();
    }

}


Abstract class act

public interface Language {
    void speak();
}


Chinese

public class Chinese implements Language {

    public void speak() {
        System.out.println(" Hello World  ");
    }

}


English

public class English implements Language {

    public void speak() {
        System.out.println("hello world");
    }

}


Test class

public class Client extends TestCase {
    public void test1() {
        People black=new BlackPeople(new Chinese());
        black.speak();
        People black2=new BlackPeople(new English());
        black2.speak();
        
        System.out.println("=============");
        
        People white=new WhitePeople(new Chinese());
        white.speak();
        People white2=new WhitePeople(new English());
        white2.speak();
    }
}


Output

I'm black
 Hello World  
I'm black
hello world
=============
I'm white
 Hello World  
I'm white
hello world


This greatly reduced the use pattern code Although there are now six categories but it is 1 +1 +2 +2

If the law is in accordance with the original N * N sub-branch will be using the mode 1 +1 + N + N rather than N * N a class

Less chance of error code is less when a change needs to change in places where less is more important is that there is no duplicate code fragments
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of bridge bridge mode

  • hibernate (jpa) composite primary key annotation statement Ways

    hibernate (jpa) composite primary key annotation statement Ways

  • Hibernate Inteceptor

    The end of the project stage, the client suddenly put forward a very troublesome but normal demand, the system records all changes must be carried out. Formats such as: 2004.1.1 12:30 Ikuya wind orders Sales Order Date 2004.1.2-> 2004.1.3 The firs ...

  • jboss ejb3 Message Driven Bean

    Super Medium ejb hate. . . . . . . . . . . . . . . . . . . ================================================ To configure a Message Driven Bean in a different application server parameters are not the same. Currently only passed the test jboss. Messag ...

  • hibernate call stored procedure

    hibernate call stored procedure

  • Some interview questions java

    The first is the company give you a chance to meet, it is necessary to know to meet from time to equal the interview, and have a lot of companies to see you at the first time will give you a ready point of doing something trivial, these questions, al ...

  • JAVA Class naming convention

    1. Entity Layer: Inheritance: All categories inherited from BasicEntity, one of BasicEntity implementation java.io.Serializable interface; Naming rules: Class Name = Object + type suffix, one of type suffix for Bean, such as: SalesOrderBean 2. Form l ...

  • hibernate generic generic DAO

    hibernate generic generic DAO

  • Great collection of java interview topics

    1, object-oriented features of what has 1. Abstract: Abstract is that it has overlooked a subject has nothing to do with the current goal of those aspects in order to more fully with the current objectives of the attention-related aspects. Abstract d ...

Leave a Reply

Recent
Recent Entries
Tag Cloud
Random Entries