funmain() {
val users =listOf(User(name ="Android User", mobileNumbers =listOf("987654321", "9999999999")))
val adapter =Adapter(users)
}
classAdapter(privatevalusers:List<User>) {
/** Calling onBindViewHolder here just for a demo purpose. In a RecyclerView adapter, * onBindViewHolder will be triggered as part of adapter life cycle. **/init {
onBindViewHolder(0)
}
funonBindViewHolder(position:Int) {
val user = users[position]
println("Name has been set as ${user.name} to TextView")
/** * I didn't receive the proper data to display it in the UI.. * So let me convert it. **/val mobileNumber = user.mobileNumbers.joinToString()
println("Mobile numbers has been set as $mobileNumber to TextView")
}
}
data classUser(valname:String, valmobileNumbers:List<String>)
Good
funmain() {
val users =listOf(User(name ="Android User", mobile ="987654321, 9999999999"))
val adapter =Adapter(users)
}
classAdapter(privatevalusers:List<User>) {
/** Calling onBindViewHolder here just for a demo purpose, * onBindViewHolder will be triggered as part of adapter life cycle. **/init {
onBindViewHolder(0)
}
funonBindViewHolder(position:Int) {
val user = users[position]
println("Name has been set as ${user.name} to TextView")
println("Mobile number has been set as ${user.mobile} to TextView")
}
}
data classUser(valname:String, valmobile:String)
funmain() {
val adapter =Adapter()
adapter.select(RadioButton())
}
classAdapter {
funselect(clickListener:ClickListener) {
when {
clickListener isListItem-> {
clickListener.changeTheBackground()
}
clickListener isRadioButton-> {
clickListener.check()
}
}
clickListener.onClick(1)
}
}
interfaceClickListener {
funonClick(position:Int)
}
classListItem: ClickListener {
overridefunonClick(position:Int){
println("Clicked ListItem $position")
}
funchangeTheBackground() {
println("Change the background color of the item view")
}
}
classRadioButton: ClickListener {
overridefunonClick(position:Int){
println("Clicked RadioButton $position")
}
funcheck() {
println("Enable the radio button")
}
}
Good
funmain() {
val adapter =Adapter()
adapter.select(RadioButton())
}
classAdapter {
funselect(clickListener:ClickListener) {
clickListener.onClick(1)
}
}
interfaceClickListener {
funonClick(position:Int)
}
classListItem: ClickListener {
overridefunonClick(position:Int){
changeTheBackground()
println("Clicked ListItem $position")
}
funchangeTheBackground() {
println("Change the background color of the item view")
}
}
classRadioButton: ClickListener {
overridefunonClick(position:Int){
check()
println("Clicked RadioButton $position")
}
funcheck() {
println("Enable the radio button")
}
}
I — The Interface Segregation Principle (ISP):
Bad
funmain() {
val adapter =Adapter()
adapter(object:Adapter.OnClickListener {
overridefunonItemClick(position:Int) {
// Yes, I have received a callback, go to the next activity.println("Clicked position is $position")
}
overridefunonRadioButtonClick(position:Int) {
// This is no longer needed for this activity, but still I have been implemented for no use...
}
})
adapter.execute()
}
classAdapter {
privatevar onClickListener:OnClickListener?=nulloperatorfuninvoke (onClickListener:OnClickListener) {
this.onClickListener = onClickListener
}
funexecute() {
onClickListener?.onItemClick(4)
}
interfaceOnClickListener {
funonItemClick(position:Int)
funonRadioButtonClick(position:Int)
}
}
Good
funmain() {
val adapter =Adapter()
adapter(object:Adapter.OnItemClickListener {
overridefunonItemClick(position:Int) {
// Yes, I have received a callback, go to the next activity.println("Clicked position is $position")
}
})
adapter.execute()
}
classAdapter {
privatevar onItemClickListener:OnItemClickListener?=nulloperatorfuninvoke (onItemClickListener:OnItemClickListener) {
this.onItemClickListener = onItemClickListener
}
funexecute() {
onItemClickListener?.onItemClick(4)
}
interfaceOnItemClickListener {
funonItemClick(position:Int)
}
interfaceOnRadioClickListener {
funonRadioButtonClick(position:Int)
}
}
D - The Dependency Inversion Principle (DIP)
Bad
funmain() {
val user =User()
user.getMyData(3)
}
classUser {
fungetMyData(requestCode:Int) {
when (requestCode) {
1-> {
val firebase =Firebase()
firebase.fetchData()
}
2-> {
val restClient =RestClient()
restClient.fetchData()
}
else->print("I dont care about the user. Don't do anything. Keep quiet!")
}
}
}
classFirebase {
funfetchData(){
print("Syncing the data from the firebase storage")
}
}
classRestClient {
funfetchData(){
print("Hitting the api and getting back the response")
}
}
Good
funmain() {
/* As a end user, I don't care about how will I retrieve the data. * Do whatever you want and simply get my data. */val user =User(dataFetcher =Firebase())
user.getMyData()
}
classUser(privatevaldataFetcher:DataFetcher) {
fungetMyData() {
dataFetcher.fetchData()
}
}
interfaceDataFetcher {
funfetchData()
}
classFirebase: DataFetcher {
overridefunfetchData(){
print("Syncing the data from the firebase storage")
}
}
classRestClient: DataFetcher {
overridefunfetchData(){
print("Hitting the api and getting back the response")
}
}