Last active
February 12, 2024 14:20
-
-
Save parthdesai1208/3a4b75be9624c46129cd241f20aceb33 to your computer and use it in GitHub Desktop.
Design Patterns
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 Components{ | |
void showPrice(); | |
} | |
class Composite implements Components{ | |
String name; | |
List<Components> componentsList = new ArrayList<>(); | |
public Composite(String name){ | |
super(); | |
this.name = name; | |
} | |
public void addComponents(Components c){ | |
componentsList.add(c); | |
} | |
@Override | |
public void showPrice(){ | |
System.out.println(name); | |
for(comp c : componentsList){ | |
c.showPrice(); | |
} | |
} | |
} | |
class Leaf implements Components{ | |
int price; | |
String name; | |
public Leaf(int price, String name){ | |
this.price = price; | |
this.name = name; | |
} | |
@Override | |
public void showPrice(){ | |
System.out.println(name + " : " + price); | |
} | |
} | |
public static void main(){ | |
Components hardDrive = new Leaf(4000, "HardDrive"); | |
Components mouse = new Leaf(500, "Mouse"); | |
Components monitor = new Leaf(300, "Monitor"); | |
Components ram = new Leaf(5000, "RAM"); | |
Components cpu = new Leaf(800, "CPU"); | |
Composite peripheral = new Composite("Peripheral"); | |
Composite cabinet = new Composite("Cabinet"); | |
Composite motherBoard = new Composite("MotherBoard"); | |
Composite computer = new Composite("Computer"); //root | |
peripheral.addComponents(mouse); | |
peripheral.addComponents(monitor); | |
cabinet.addComponents(hardDrive); | |
motherBoard.addComponents(cpu); | |
motherBoard.addComponents(ram); | |
cabinet.addComponents(motherBoard); | |
computer.addComponents(peripheral); | |
computer.addComponents(cabinet); | |
ram.showPrice(); //5000 | |
peripheral.showPrice(); | |
//Peripheral | |
//Mouse : 500 | |
//Monitor : 300 | |
} | |
so with the single method showPrice() we can calculate price of single leaf & composite node with its leaf |
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 TextViewComponent { | |
void draw(); | |
} | |
class ConcreteTextView implements TextViewComponent { | |
@Override | |
public void draw() { | |
System.out.println("Drawing basic TextView"); | |
} | |
} | |
// Decorator class | |
abstract class TextViewDecorator implements TextViewComponent { | |
protected TextViewComponent textViewComponent; | |
public TextViewDecorator(TextViewComponent textViewComponent) { | |
this.textViewComponent = textViewComponent; | |
} | |
@Override | |
public void draw() { | |
textViewComponent.draw(); // Delegate drawing to the wrapped component | |
} | |
} | |
// Concrete decorators | |
class BorderDecorator extends TextViewDecorator { | |
public BorderDecorator(TextViewComponent textViewComponent) { | |
super(textViewComponent); | |
} | |
@Override | |
public void draw() { | |
super.draw(); | |
// Add border functionality | |
System.out.println("Adding border to TextView"); | |
} | |
} | |
class BackgroundColorDecorator extends TextViewDecorator { | |
public BackgroundColorDecorator(TextViewComponent textViewComponent) { | |
super(textViewComponent); | |
} | |
@Override | |
public void draw() { | |
super.draw(); | |
// Add background color functionality | |
System.out.println("Adding background color to TextView"); | |
} | |
} | |
// Usage example | |
public class Main { | |
public static void main(String[] args) { | |
// Create a basic TextView | |
TextViewComponent textView = new ConcreteTextView(); | |
// Decorate it with border | |
textView = new BorderDecorator(textView); | |
// Decorate it with background color | |
textView = new BackgroundColorDecorator(textView); | |
// Draw the decorated TextView | |
textView.draw(); | |
} | |
} |
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
// Facade class for the music player subsystems | |
public class MusicPlayerFacade { | |
private MediaPlayer mMediaPlayer; | |
private AudioManager mAudioManager; | |
private NotificationManager mNotificationManager; | |
public MusicPlayerFacade(Context context) { | |
mMediaPlayer = new MediaPlayer(); | |
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); | |
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | |
} | |
public void playMusic(String musicUrl) { | |
try { | |
// Prepare media player with the provided music URL | |
mMediaPlayer.setDataSource(musicUrl); | |
mMediaPlayer.prepare(); | |
mMediaPlayer.start(); | |
// Request audio focus | |
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); | |
// Show notification | |
showNotification("Now playing..."); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void stopMusic() { | |
// Stop media player | |
if (mMediaPlayer.isPlaying()) { | |
mMediaPlayer.stop(); | |
mMediaPlayer.release(); | |
} | |
// Abandon audio focus | |
mAudioManager.abandonAudioFocus(null); | |
// Cancel notification | |
mNotificationManager.cancel(1); | |
} | |
private void showNotification(String message) { | |
// Code to create and display a notification | |
Notification notification = new Notification.Builder(mContext) | |
.setContentTitle("Music Player") | |
.setContentText(message) | |
.setSmallIcon(R.drawable.ic_music_note) | |
.build(); | |
mNotificationManager.notify(1, notification); | |
} | |
} |
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
// Visitor interface | |
interface ConfigurationChangeListener { | |
void onOrientationChange(); | |
void onLanguageChange(); | |
} | |
class ConfigurationChangeHandler implements ConfigurationChangeListener { | |
@Override | |
public void onOrientationChange() { | |
// React to orientation change | |
} | |
@Override | |
public void onLanguageChange() { | |
// React to language change | |
} | |
} | |
// ViewModel class | |
public class MyViewModel extends ViewModel { | |
private ConfigurationChangeListener configurationChangeListener; | |
// Method to set the configuration change listener | |
public void setConfigurationChangeListener(ConfigurationChangeListener listener) { | |
this.configurationChangeListener = listener; | |
} | |
// Method to be called when a configuration change occurs | |
public void onConfigurationChange(Configuration newConfig) { | |
if (configurationChangeListener != null) { | |
if (newConfig.orientation != Configuration.ORIENTATION_UNDEFINED) { //<---------visitor pattern | |
configurationChangeListener.onOrientationChange(); | |
} | |
if (!newConfig.locale.equals(Locale.getDefault())) { //<---------visitor pattern | |
configurationChangeListener.onLanguageChange(); | |
} | |
// Add conditions for other types of configuration changes | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment