原创文章,如有转载,请注明出处:http://blog.csdn.net/myth13141314/article/details/78267978
动态的给一个对象添加一些额外的职责。就增加功能来说,装饰模式比生成子类更为灵活
public class DecoratorActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initData() {
//初始化数据
}
private void initView() {
//初始化页面
}
}
public abstract class Notify {
protected Context context;
protected NotificationManager notificationManager;
protected NotificationCompat.Builder builder;
public Notify(Context context) {
this.context = context;
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(PendingIntent.getActivity(context, 0,
new Intent(context, NotifyActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT));
}
/**
* 发送一条通知
*/
public abstract void send();
/**
* 取消一条通知
*/
public abstract void cancel();
}
public class NotifyNormal extends Notify {
public NotifyNormal(Context context) {
super(context);
}
@Override
public void send() {
Notification notification = builder.build();
notificationManager.notify(0, notification);
}
@Override
public void cancel() {
notificationManager.cancel(0);
}
}
public abstract class NotifyDecorator extends Notify {
private Notify notify;
public NotifyDecorator (Context context, Notify mNotify) {
super(context);
this.notify = mNotify;
}
@Override
public void send() {
notify.send();
}
@Override
public void cancel() {
notify.cancel();
}
}
public class NotifyNormalDecorator extends NotifyDecorator {
public NotifyNormalDecorator (Context context, Notify notify) {
super(context, notify);
}
@Override
public void send() {
builder.setContent(new RemoteViews(context.getPackageName(), R.layout.layout_notify_normal));
super.send();
}
}
public class NotifyBigDecorator extends NotifyDecorator {
public NotifyBigDecorator(Context context, Notify notify) {
super(context, notify);
}
@Override
public void send() {
builder.setContent(new RemoteViews(context.getPackageName(), R.layout.layout_notify_normal));
builder.setCustomBigContentView(new RemoteViews(context.getPackageName(), R.layout.layout_notify_normal));
super.send();
}
}
public class NotifyHeadsUpDecorator extends NotifyDecorator {
public NotifyHeadsUpDecorator(Context context, Notify notify) {
super(context, notify);
}
@Override
public void send() {
builder.setContent(new RemoteViews(context.getPackageName(), R.layout.layout_notify_normal));
builder.setCustomBigContentView(new RemoteViews(context.getPackageName(), R.layout.layout_notify_normal));
builder.setCustomHeadsUpContentView(new RemoteViews(context.getPackageName(), R.layout.layout_notify_normal));
super.send();
}
}
public class NotifyProxy extends Notify{
private NotifyDecorator notifyDecorator;
public NotifyProxy (Context context) {
super(context);
Notify notify = new NotifyNormal(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notifyDecorator = new NotifyHeadsUpDecorator(context, notify);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
notifyDecorator = new NotifyBigDecorator(context, notify);
} else {
notifyDecorator = new NotifyNormalDecorator(context, notify);
}
}
@Override
public void send() {
notifyDecorator.send();
}
@Override
public void cancel() {
notifyDecorator.cancel();
}
}
new NotifyProxy(MainActivity.this).send();