動態的給一個對象添加一些額外的職責。就增長功能來講,裝飾模式比生成子類更爲靈活設計模式
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();
複製代碼
歡迎關注個人微信公衆號,期待與你一塊兒學習,一塊兒交流,一塊兒成長! bash