1. Are you ready for the Galaxy S20? Here is everything we know so far!

Design Patterns

Discussion in 'Android Development' started by moumen shobaky, Mar 14, 2020.

  1. moumen shobaky

    Thread Starter

    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
    Code (Java):
    1.  
    2. public class A {
    3.     private String Name;
    4.     private String ID;
    5.     private int age;
    6.  
    7.     public String getName() {
    8.         return Name;
    9.     }
    10.  
    11.     public void setName(String name) {
    12.         Name = name;
    13.     }
    14.  
    15.     public String getID() {
    16.         return ID;
    17.     }
    18.  
    19.     public void setID(String ID) {
    20.         this.ID = ID;
    21.     }
    22.  
    23.     public int getAge() {
    24.         return age;
    25.     }
    26.  
    27.     public void setAge(int age) {
    28.         this.age = age;
    29.     }
    30. }
    31.  

    Code (Java):
    1. public class B {
    2.     private String mail;
    3.     private String nickname;
    4.     private int yearOfBirth;
    5.  
    6.     public String getMail() {
    7.         return mail;
    8.     }
    9.  
    10.     public void setMail(String mail) {
    11.         this.mail = mail;
    12.     }
    13.  
    14.     public String getNickname() {
    15.         return nickname;
    16.     }
    17.  
    18.     public void setNickname(String nickname) {
    19.         this.nickname = nickname;
    20.     }
    21.  
    22.     public int getYearOfBirth() {
    23.         return yearOfBirth;
    24.     }
    25.  
    26.     public void setYearOfBirth(int yearOfBirth) {
    27.         this.yearOfBirth = yearOfBirth;
    28.     }
    29. }
    30.  

    Code (Java):
    1. public class AtoBadapter extends B {
    2.     private String mail;
    3.     private String nickName;
    4.     private int year;
    5.     private A a;
    6.     public AtoBadapter(A a){
    7.         this.a = a;
    8.         convert();
    9.     }
    10.  
    11.     @Override
    12.     public String getMail() {
    13.         return mail;
    14.     }
    15.  
    16.     @Override
    17.     public void setMail(String mail) {
    18.         this.mail = mail;
    19.     }
    20.  
    21.     @Override
    22.     public String getNickname() {
    23.         return nickName;
    24.     }
    25.  
    26.     @Override
    27.     public void setNickname(String nickname) {
    28.         this.nickName = nickname;
    29.     }
    30.  
    31.     @Override
    32.     public int getYearOfBirth() {
    33.         return year;
    34.     }
    35.  
    36.     @Override
    37.     public void setYearOfBirth(int yearOfBirth) {
    38.         this.year = yearOfBirth;
    39.     }
    40.     private void convert(){
    41.         this.mail = a.getName().concat("@yahoo.com");
    42.         this.year = 2020 - a.getAge();
    43.         this.nickName = a.getID();
    44.     }
    45. }
    Code (Java):
    1. public class MainActivity extends AppCompatActivity {
    2.  
    3.     @Override
    4.     protected void onCreate(Bundle savedInstanceState) {
    5.         super.onCreate(savedInstanceState);
    6.         setContentView(R.layout.activity_main);
    7.  
    8.         A a = new A();
    9.         a.setAge(20);
    10.         a.setID("z0-4");
    11.         a.setName("A");
    12.         B b = new AtoBadapter(a);
    13.         analyze(b);
    14.     }
    15.     public void analyze(B b){
    16.         //Do what you need with B class
    17.  
    18.     }
    19.  
    20. }
    21.  


     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...