Observer pattern used with decorator pattern
I want to make a program that does an order entry system for beverages. ( i will probably do description, cost)
I want to use the Decorator pattern and the observer pattern. I made a UML drawing and saved it as a pic for easy viewing. This site wont let me upload as a word doc so i have to upload a pic - i hope its easily viewable....
I need to know if i am doing the UML / design patterns correctly before moving on to the coding part.
Beverage is my abstract component class. Espresso, houseblend, darkroast are my concrete subject classes..
I also have a condiment decorator class milk,mocha,soy,whip. would be my observer? because they would be interested in data changes to cost?
Now, would the espresso,houseblend etc, be my SUBJECT and the condiments be my observer? My theory is that Cost is a changes and that the condiments need to know the changes?
So, subject = esspresso,houseblend,darkroast,etc.. // they hold cost()
Observer = milk,mocha,soy,whip? // they hold cost()
would be the concrete components and the milk,mocha,soy,whip? would be the decorator!
So, following good software engineering practices "design to an interface and not implementation" or "identify things that change from those that dont"
would i need a costbehavior interface?
If you look at the UML you will see where i am going with this and see if i am implementing observer + Decorator pattern correctly? I think the decorator is correct.
since, the pic is not very viewable i will detail the classes here:
Beverage class(register observer, remove observer, notify observer, description)
these classes are the concrete beverage classes
espresso, houseblend,darkroast, decaf(cost,getdescription,setcost,costchanged)
interface observer class(update) // cost?
interface costbehavior class(cost) // since this changes?
condiment decorator class( getdescription)
concrete classes that are linked to the 2 interface s and decorator are: milk,mocha,soy,whip(cost,getdescription,update) these are my decorator/ wrapper classes.
Thank you..
Is there a way to make this picture bigger?
I can see decorator comes into play here, but I am not too sure about using observer here. It seems forced.
Several things:
My suggestion is to read up again on the Head First Design Pattern book (which I think where you get this examples :), very similar domain) and get a better understanding on both patterns and when to use them.
Below is an example of decorator and observer working together. The drink combination is an implementation of decorator. The ordering system is an observer (where the order will notify the pager and the pager will do something). The scenario here is a StarBuck coffee shop where they'll give you a pager so you can go around doing something while your drink is being processed and you will get notified by the pager once the drink is ready.
Save the sample as drink.cs and you can easily compile this using csc ( C:WindowsMicrosoft.NetFrameworkv3....csc /target:exe /out:drink.exe drink.cs) and run it or use VS or whatever :).
using System;
using System.Collections.Generic;
public interface IBeverage
{
string GetDescription();
decimal GetCost();
}
public abstract class Beverage : IBeverage
{
protected string _name;
protected decimal _cost;
public Beverage(string name, decimal cost)
{
_name = name;
_cost = cost;
}
public virtual string GetDescription()
{
return _name;
}
public virtual decimal GetCost()
{
return _cost;
}
}
public class Macchiato : Beverage
{
public Macchiato() : base("Macchiato", 3.50m) {}
}
public abstract class BeverageDecorator : Beverage
{
IBeverage _baseBeverage;
public BeverageDecorator(IBeverage baseBeverage) : base("", 0m)
{
_baseBeverage = baseBeverage;
}
public override string GetDescription()
{
return _name + " " + _baseBeverage.GetDescription();
}
public override decimal GetCost()
{
return _cost + _baseBeverage.GetCost();
}
}
public class Caramel : BeverageDecorator
{
public Caramel(IBeverage baseBeverage) : base(baseBeverage)
{
_name = "Caramel";
_cost = 0.50m;
}
}
public class Venti : BeverageDecorator
{
public Venti(IBeverage baseBeverage) : base(baseBeverage)
{
_name = "Venti";
_cost = 1.00m;
}
}
public class Iced : BeverageDecorator
{
public Iced(IBeverage baseBeverage) : base(baseBeverage)
{
_name = "Iced";
_cost = 0.25m;
}
}
public class Order
{
IBeverage _beverage;
IPager _pager;
public Order(IBeverage beverage, IPager pager)
{
_beverage = beverage;
_pager = pager;
}
public IPager Pager
{
get { return _pager; }
}
public IBeverage Beverage
{
get { return _beverage; }
}
}
public class OrderProcessing
{
Queue<Order> orders = new Queue<Order>();
public void NewOrder(IBeverage beverage, IPager pager)
{
orders.Enqueue(new Order(beverage, pager));
}
public void ProcessOrder()
{
if (orders.Count > 0)
{
var order = orders.Dequeue();
order.Pager.Update(order);
}
}
}
public interface IPager
{
void Update(Order order);
}
public class VibratingPager : IPager
{
string _number;
public VibratingPager(string number)
{
_number = number;
}
public void Update(Order order)
{
Console.WriteLine("BUZZZ");
Console.WriteLine("Your {0} is ready. Please pay {1} at the cashier after picking it up.", order.Beverage.GetDescription(),order.Beverage.GetCost());
}
}
public class Program
{
public static void Main(string[] args)
{
var orders = new OrderProcessing();
var pager1 = new VibratingPager("1");
var pager2 = new VibratingPager("2");
orders.NewOrder(new Iced(new Venti(new Caramel(new Macchiato()))), pager1);
orders.NewOrder(new Venti(new Macchiato()), pager2);
orders.ProcessOrder();
orders.ProcessOrder();
}
}
I am definitely not a design pattern expert but I feel here that you are making this more complex than it needs to be. Also, I'm no UML expert but is it that you have each of your coffee types as a separate class? I think it would make more sense to have a beverage class with a type property/attribute and then to inject a 'cost-detail' class into this which will allow these beverages to report correct prices.
No. It's not right at all. Sorry, but it isn't. That drawing says that EVERYTHING is a beverage, in some cases many times over. Clearly some of those white triangles should be diamonds, most of them probably black. The only relationship you've used is inheritance and it's obviously not your intention.
I also have serious problems with the design idea, period, but I guess that's for another discussion. Nothing you've done is out of ordinary for someone just starting with design (everyone starts out seriously overdesinging every damn thing), so I wouldn't feel bad about it though. At least you're thinking about design, and that's a pretty darn good thing.
Reply to comment:
Nothing, when applied where it's needed. Quite a bit when it's applied where it's not. It's simply not needed here. Furthermore, you'd be doing it wrong. Cream is not a decorator for coffee (I think that's what you're trying to do) Coffee with cream could be...but that's still overdesign.
A decorator adds behavior to the thing it's decorating. Say you had a message class and a print function that displayed the output:
struct message
{
virtual void print() = 0;
};
struct concrete_message : message
{
void print() { std::cout << my_message << std::endl; }
};
Now lets say you wanted to have some of them indented. You could do that by using a "decorator", which subclasses AND contains a message:
struct tabbed_message
{
tabbed_message(message * msg) : my_message(msg) {}
void print() { std::cout << "t; my_message->print(); }
};
See how that changes the behavior of any concrete message without having to change the original?
What you've got with your coffee/condiment thing isn't like this. Condiments are just things you put in coffee. There's no difference in behavior between a cup of coffee with or without cream (for example). If you want to charge for cream added then you simply associate it with the cup of coffee and run a total when you ask the cup how much it is.
链接地址: http://www.djcxy.com/p/32004.html上一篇: Decorator方法,Java中的一个Decorator类型
下一篇: 使用装饰模式的观察者模式