Created
June 5, 2011 23:01
-
-
Save renestein/1009518 to your computer and use it in GitHub Desktop.
Image_Decorator
This file contains 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
interface IImagePersistor | |
{ | |
void Save(Image image, string name); | |
} | |
class FilePersistor : IImagePersistor | |
{ | |
public void Save(Image image, string name) | |
{ | |
var stream = File.Open(name); | |
stream.Write(RawData, 0, Length); | |
stream.Close(); | |
} | |
} | |
class FormatAwarePersistorDecorator : IImagePersistor | |
{ | |
private IImagePersistor m_innerPersitor; | |
public FormatAwarePersistorDecorator(IImagePersistor persistor) | |
{ | |
if (persistor == null) | |
{ | |
throw new ArgumentNullException(); | |
} | |
m_innerPersitor = persistor; | |
} | |
void IImagePersistor.Save(Image image, string name) | |
{ | |
//vyber image.RawData/Transformuj podle přípony souboru (klidně další pomocné objekty v pozadí) | |
Image newImageWithSpecialFormat = Convert(image.RawData, name); | |
m_innerPersitor.Save(newImageWithSpecialFormat); | |
} | |
} | |
class Image | |
{ | |
private IImagePersistor m_imagePersistor; | |
public Image() | |
{ | |
m_imagePersistor = new FormatAwarePersistorDecorator(new FilePersistor()); | |
} | |
public IImagePersistor ImagePersistor | |
{ | |
get | |
{ | |
return m_imagePersistor; | |
} | |
set | |
{ | |
m_imagePersistor = value ?? FormatAwarePersistorDecorator(new FilePersistor()); | |
} | |
} | |
public int Length | |
{ | |
get; | |
private set; | |
} | |
public Byte[] RawData | |
{ | |
get; | |
set; | |
} | |
public void Save(string name) | |
{ | |
m_imagePersistor.Save(this, name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment