Created
February 2, 2018 03:18
-
-
Save bearprada/d7976b4b8ad487ce3a3e647e9f09d1ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GenericApp { | |
private DataSource<FacebookItem> mFbSource = new FacebookDataSource(); | |
private DataSource<InstagramItem> mIgSource = new InstagramDataSource(); | |
public DataSource<? extends Item> getDataSource(int section) { | |
switch (section) { | |
case 0: | |
return mIgSource; | |
case 1: | |
default: | |
return mFbSource; | |
} | |
} | |
public Item getItem(int section) { | |
DataSource<? extends Item> source = getDataSource(section); | |
Item item = source.getItem(0); | |
item.getWidth(); | |
return item; | |
} | |
static class FacebookItem extends Item { | |
} | |
static class InstagramItem extends Item { | |
} | |
static abstract class Item { | |
public int getWidth() { | |
return 100; | |
} | |
public int geHeight() { | |
return 100; | |
} | |
} | |
interface DataSource<T extends Item> { | |
T[] getItems(); | |
T getItem(int idx); | |
int getCount(); | |
} | |
static class FacebookDataSource implements DataSource<FacebookItem> { | |
@Override | |
public FacebookItem[] getItems() { | |
return new FacebookItem[0]; | |
} | |
@Override | |
public FacebookItem getItem(int idx) { | |
return null; | |
} | |
@Override | |
public int getCount() { | |
return 0; | |
} | |
} | |
static class InstagramDataSource implements DataSource<InstagramItem> { | |
@Override | |
public InstagramItem[] getItems() { | |
return new InstagramItem[0]; | |
} | |
@Override | |
public InstagramItem getItem(int idx) { | |
return null; | |
} | |
@Override | |
public int getCount() { | |
return 0; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GenericApp { | |
let fbSource = FacebookDataSource() | |
let igSource = InstagramDataSource() | |
// Error : Cannot specialize non-generic type 'DataSource' | |
private func getSource<T>(section : Int) -> DataSource<Any> { | |
switch section { | |
case 0: | |
return fbSource | |
default: | |
return igSource | |
} | |
} | |
func getItem(section : Int, index : Int) -> PickerItem? { | |
return getSource(section:section).getItem(index) | |
} | |
func getCount(section : Int) -> Int { | |
return getSource(section:section).count | |
} | |
} | |
class PickerItem { | |
func getWidth() -> Int { | |
return 100 | |
} | |
} | |
class FacebookPickerItem : PickerItem { | |
} | |
class InstagramPickerItem : PickerItem { | |
} | |
protocol DataSource { | |
associatedtype Item : PickerItem | |
var count: Int { get } | |
func getItem(idx: Int) -> Item | |
} | |
class FacebookDataSource: DataSource { | |
var items = [FacebookPickerItem]() | |
typealias Item = FacebookPickerItem | |
var count: Int { | |
return items.count | |
} | |
func getItem(idx: Int) -> FacebookPickerItem { | |
return items[idx] | |
} | |
} | |
class InstagramDataSource: DataSource { | |
var items = [InstagramPickerItem]() | |
typealias Item = InstagramPickerItem | |
var count: Int { | |
return items.count | |
} | |
func getItem(idx: Int) -> InstagramPickerItem { | |
return items[idx] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment