• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Design Patterns

A lot of people who rushed into android world , Didn't take their time in Java , Actually i am one from these people , So, We didn't learn some important topics , One of them is design pattern.
I am reading a book of design patterns and i'll try to walk through it with you i'll just add the flavor of android , I hope i'll will
So let's start with Adapter pattern

What is Adapter Pattern?
It's like the name suggests , Converting one class to another one keeping only the name of its methods name but functionality of the other's.

Why would i need it?
Sometimes you use a class for a while,Then, You find out that you need to switch to another one ,But, It will take you much time and efforts to edit all the code.

How to implement it?
Create a new class "Adapter" which has the name of the class you want to switch to "By extending it" and has an object of type of the old class , Use the data you passed to the old class in the new class.

Example
I'll get enough to show the concept , And, i'll wait your opinion if you need a kind of real example.

We need ,For example, Switch
class A to class B
Java:
public class A {
    private String Name;
    private String ID;
    private int age;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Java:
public class B {
    private String mail;
    private String nickname;
    private int yearOfBirth;

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public int getYearOfBirth() {
        return yearOfBirth;
    }

    public void setYearOfBirth(int yearOfBirth) {
        this.yearOfBirth = yearOfBirth;
    }
}
Java:
public class AtoBadapter extends B {
    private String mail;
    private String nickName;
    private int year;
    private A a;
    public AtoBadapter(A a){
        this.a = a;
        convert();
    }

    @Override
    public String getMail() {
        return mail;
    }

    @Override
    public void setMail(String mail) {
        this.mail = mail;
    }

    @Override
    public String getNickname() {
        return nickName;
    }

    @Override
    public void setNickname(String nickname) {
        this.nickName = nickname;
    }

    @Override
    public int getYearOfBirth() {
        return year;
    }

    @Override
    public void setYearOfBirth(int yearOfBirth) {
        this.year = yearOfBirth;
    }
    private void convert(){
        this.mail = a.getName().concat("@yahoo.com");
        this.year = 2020 - a.getAge();
        this.nickName = a.getID();
    }
}

Java:
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        A a = new A();
        a.setAge(20);
        a.setID("z0-4");
        a.setName("A");
        B b = new AtoBadapter(a);
        analyze(b);
    }
    public void analyze(B b){
        //Do what you need with B class

    }

}


 
Back
Top Bottom